{"id":"54e285bf1b53ddd3","repo":"psf/requests","slug":"create-cookie-got-unexpected-keyword-arguments","errorCode":null,"errorMessage":"create_cookie() got unexpected keyword arguments: {list(badargs)}","messagePattern":"create_cookie\\(\\) got unexpected keyword arguments: (.+?)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/requests/cookies.py","lineNumber":518,"sourceCode":"    result: dict[str, Any] = {\n        \"version\": 0,\n        \"name\": name,\n        \"value\": value,\n        \"port\": None,\n        \"domain\": \"\",\n        \"path\": \"/\",\n        \"secure\": False,\n        \"expires\": None,\n        \"discard\": True,\n        \"comment\": None,\n        \"comment_url\": None,\n        \"rest\": {\"HttpOnly\": None},\n        \"rfc2109\": False,\n    }\n\n    badargs = set(kwargs) - set(result)\n    if badargs:\n        raise TypeError(\n            f\"create_cookie() got unexpected keyword arguments: {list(badargs)}\"\n        )\n\n    result.update(kwargs)\n    result[\"port_specified\"] = bool(result[\"port\"])\n    result[\"domain_specified\"] = bool(result[\"domain\"])\n    result[\"domain_initial_dot\"] = result[\"domain\"].startswith(\".\")\n    result[\"path_specified\"] = bool(result[\"path\"])\n\n    return cookielib.Cookie(**result)\n\n\ndef morsel_to_cookie(morsel: Morsel[Any]) -> Cookie:\n    \"\"\"Convert a Morsel object into a Cookie containing the one k/v pair.\"\"\"\n\n    expires: int | None = None\n    if morsel[\"max-age\"]:\n        try:","sourceCodeStart":500,"sourceCodeEnd":536,"githubUrl":"https://github.com/psf/requests/blob/414f0513c33883adf6f2b46901d4f0b38a455851/src/requests/cookies.py#L500-L536","documentation":"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.","triggerScenarios":"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).","commonSituations":"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'.","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."],"exampleFix":"# before\nsession.cookies.set('sid', 'abc', httponly=True, max_age=3600)\n\n# after\nimport time\nsession.cookies.set('sid', 'abc', rest={'HttpOnly': True},\n                    expires=int(time.time()) + 3600)","handlingStrategy":"validation","validationCode":"ALLOWED = {'version','name','value','port','domain','path','secure','expires','discard','comment','comment_url','rest','rfc2109'}\nkwargs = {k: v for k, v in raw_attrs.items() if k in ALLOWED}\ncookie = requests.cookies.create_cookie(**kwargs)","typeGuard":null,"tryCatchPattern":"try:\n    cookie = create_cookie(name, value, **attrs)\nexcept TypeError as e:\n    log.error('bad cookie attrs: %s', e)\n    raise","preventionTips":["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"],"tags":["cookies","typeerror","create-cookie","api-misuse"],"analyzedSha":"414f0513c33883adf6f2b46901d4f0b38a455851","analyzedAt":"2026-07-31T19:24:57.696Z","schemaVersion":2}