psf/requests · error · TypeError
create_cookie() got unexpected keyword arguments: {list(bada
Error message
create_cookie() got unexpected keyword arguments: {list(badargs)} What it means
A TypeError raised by requests.cookies.create_cookie() in src/requests/cookies.py. create_cookie builds an http.cookiejar.Cookie from a fixed set of known fields (version, name, value, port, domain, path, secure, expires, discard, comment, comment_url, rest, rfc2109); any keyword argument outside that set is rejected up front rather than silently ignored, since it would otherwise never reach the Cookie constructor correctly.
Source
Thrown at src/requests/cookies.py:518
result: dict[str, Any] = {
"version": 0,
"name": name,
"value": value,
"port": None,
"domain": "",
"path": "/",
"secure": False,
"expires": None,
"discard": True,
"comment": None,
"comment_url": None,
"rest": {"HttpOnly": None},
"rfc2109": False,
}
badargs = set(kwargs) - set(result)
if badargs:
raise TypeError(
f"create_cookie() got unexpected keyword arguments: {list(badargs)}"
)
result.update(kwargs)
result["port_specified"] = bool(result["port"])
result["domain_specified"] = bool(result["domain"])
result["domain_initial_dot"] = result["domain"].startswith(".")
result["path_specified"] = bool(result["path"])
return cookielib.Cookie(**result)
def morsel_to_cookie(morsel: Morsel[Any]) -> Cookie:
"""Convert a Morsel object into a Cookie containing the one k/v pair."""
expires: int | None = None
if morsel["max-age"]:
try:View on GitHub ↗ (pinned to 414f0513c3)
Solutions
- Rename or strip the offending keys to match create_cookie's accepted set: version, port, domain, path, secure, expires, discard, comment, comment_url, rest, rfc2109.
- Express HttpOnly via rest={'HttpOnly': True} and Max-Age by computing expires=int(time.time()) + max_age.
- When importing browser cookies, map fields explicitly instead of **-splatting the browser dict.
Example fix
# before
session.cookies.set('sid', 'abc', httponly=True, max_age=3600)
# after
import time
session.cookies.set('sid', 'abc', rest={'HttpOnly': True},
expires=int(time.time()) + 3600) Defensive patterns
Strategy: validation
Validate before calling
ALLOWED = {'version','name','value','port','domain','path','secure','expires','discard','comment','comment_url','rest','rfc2109'}
kwargs = {k: v for k, v in raw_attrs.items() if k in ALLOWED}
cookie = requests.cookies.create_cookie(**kwargs) Try / catch
try:
cookie = create_cookie(name, value, **attrs)
except TypeError as e:
log.error('bad cookie attrs: %s', e)
raise Prevention
- Filter dynamic attribute dicts against create_cookie's known keyword set before calling — don't forward arbitrary parsed attributes
- Common mistakes: passing 'max_age', 'httponly', or 'samesite' as top-level kwargs; non-standard attributes belong in rest={'HttpOnly': None}
- Pin the requests version in CI so the accepted signature doesn't shift silently
When it happens
Trigger: Calling requests.cookies.create_cookie(name, value, **kwargs) — directly, or indirectly via session.cookies.set(name, value, **kwargs) — with keys like httponly=True, max_age=3600, samesite='Lax', expiry=..., or url=... (e.g. passing Selenium's get_cookies() dicts straight through).
Common situations: Porting cookies from Selenium/Playwright whose cookie dicts use different key names ('httpOnly', 'sameSite', 'expiry'); assuming Set-Cookie attribute names (max-age, samesite) map to constructor kwargs; typos like 'domian'.
Related errors
- max-age: {morsel['max-age']} must be integer
- You can only merge into CookieJar
- Cookie headers should be added with add_unredirected_header(
- name={name!r}, domain={domain!r}, path={path!r}
- There are multiple cookies with name, {name!r}
AI-assisted analysis of psf/requests@414f0513c3 (2026-07-31).
Data as JSON: /data/errors/54e285bf1b53ddd3.json.
Report an issue: GitHub ↗.