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

  1. 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.
  2. Express HttpOnly via rest={'HttpOnly': True} and Max-Age by computing expires=int(time.time()) + max_age.
  3. 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

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


AI-assisted analysis of psf/requests@414f0513c3 (2026-07-31). Data as JSON: /data/errors/54e285bf1b53ddd3.json. Report an issue: GitHub ↗.