{"record":{"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":"exception","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/8068356288978c4f54661ae6f95afe0e0831885e/src/requests/cookies.py#L500-L536","documentation":"Raised by requests.cookies.create_cookie when kwargs contain keys not in the supported cookie attribute set (name, value, version, port, domain, secure, expires, etc.). The function builds a strict result dict and rejects any unknown key, listing the offending names in the message. This protects against silently mistyped attributes that would otherwise be ignored or passed to cookielib.Cookie incorrectly.","triggerScenarios":"Calling create_cookie() with a typo (e.g. expiry= instead of expires=, httponly= instead of rest={'HttpOnly': True}); passing an arbitrary dict of cookie attributes from user input without filtering; version changes that renamed a supported key.","commonSituations":"Building cookies from a config file or API payload that includes keys like 'samesite' or 'priority' which create_cookie does not model; copy-pasting cookie dicts from another library (e.g. httpx) whose schema differs.","solutions":["Check the error message for the listed badargs and rename them to the supported keys (expires, secure, domain, path, comment, comment_url, rest, discard, rfc2109, version, port).","If you need HttpOnly/SameSite semantics, pass them via rest={'HttpOnly': True} (rest is the supported extension dict).","Pre-validate kwargs against the known key set before calling create_cookie."],"exampleFix":"// before\ncreate_cookie(name='sid', value='x', expiry=3600, httponly=True)\n\n// after\ncreate_cookie(\n    name='sid', value='x', expires=int(time.time())+3600,\n    rest={'HttpOnly': True},\n)","handlingStrategy":"validation","validationCode":"from requests.cookies import create_cookie\n\n_ALLOWED = {'name','value','version','port','port_specified','domain','domain_specified',\n            'domain_initial_dot','path','path_specified','secure','expires','discard',\n            'comment','comment_url','rest','rfc2109'}\n\ndef safe_create_cookie(**kwargs):\n    bad = set(kwargs) - _ALLOWED\n    if bad:\n        raise TypeError(f'unsupported cookie keys: {sorted(bad)}')\n    return create_cookie(**kwargs)","typeGuard":"from typing import Any\n\n_ALLOWED_COOKIE_KEYS = {'name','value','version','port','port_specified','domain',\n    'domain_specified','domain_initial_dot','path','path_specified','secure','expires',\n    'discard','comment','comment_url','rest','rfc2109'}\n\ndef is_valid_cookie_kwargs(d: dict[str, Any]) -> bool:\n    return set(d).issubset(_ALLOWED_COOKIE_KEYS) and 'name' in d and 'value' in d","tryCatchPattern":"try:\n    c = create_cookie(**attrs)\nexcept TypeError as e:\n    # filter to known keys and retry, or report\n    attrs = {k: v for k, v in attrs.items() if k in _ALLOWED_COOKIE_KEYS}\n    c = create_cookie(**attrs)","preventionTips":["Keep a constant set of allowed cookie keys and filter user/config input through it.","Map HttpOnly/SameSite via rest={'HttpOnly': True, 'SameSite': 'Lax'} rather than top-level keys.","Spell-check attribute names against cookielib.Cookie.__init__ signature."],"tags":["cookies","typeerror","create-cookie","kwargs","validation"],"backgroundTag":null,"analyzedSha":"8068356288978c4f54661ae6f95afe0e0831885e","analyzedAt":"2026-08-11T20:11:09.238Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}