psf/requests · error · KeyError
name= , domain= , path=
Error message
name={name!r}, domain={domain!r}, path={path!r} What it means
Raised by RequestsCookieJar.__getitem__ (via the cookie-lookup loop) when no cookie matches the requested name, and optionally the domain and path filters. The KeyError's string contains the exact name/domain/path triple queried so you can see which key was missing. It is the standard 'cookie not found' signal for jar lookups.
Solutions
- Use session.cookies.get(name, default) or the get(name, domain=, path=) method instead of subscript access to avoid the KeyError.
- Verify the cookie exists with name in jar or by iterating the jar before accessing.
- Confirm domain/path filters match the server's Set-Cookie attributes exactly (including leading dot).
Example fix
// before
value = session.cookies['sid']
// after
value = session.cookies.get('sid')
if value is None:
# handle missing cookie
... Defensive patterns
Strategy: validation
Validate before calling
from requests.cookies import RequestsCookieJar
def get_cookie_or_none(jar: RequestsCookieJar, name, domain=None, path=None):
# Use .get() instead of [] to avoid KeyError
return jar.get(name, domain=domain, path=path) Type guard
from requests.cookies import RequestsCookieJar
def cookie_exists(jar: RequestsCookieJar, name, domain=None, path=None) -> bool:
for c in jar:
if c.name == name and (domain is None or c.domain == domain) and (path is None or c.path == path):
return True
return False Try / catch
from requests.cookies import RequestsCookieJar
try:
value = session.cookies['sid']
except KeyError as e:
# e.args[0] contains 'name=..., domain=..., path=...'
value = None Prevention
- Prefer session.cookies.get(name) over subscript access when absence is possible.
- Check 'name' in session.cookies before indexing.
- Match domain/path filters to the server's Set-Cookie attributes exactly.
When it happens
Trigger: Indexing a session.cookies jar with a cookie name that was never set (session.cookies['sid']) or was deleted/expired; passing domain= or path= kwargs to jar['name'] that do not match any stored cookie; looking up a cookie whose domain attribute differs (e.g. '.example.com' vs 'example.com').
Common situations: Trying to read a session cookie before the server has set it; cookie scoping differences (the jar stores domain/path-qualified entries, so the same name can exist under multiple scopes); relying on a cookie set on a subdomain but querying with the apex domain.
Related errors
- Cookie headers should be added with…
- There are multiple cookies with name
- You can only merge into CookieJar
- Auth hooks must be callable.
- create_cookie() got unexpected keyword arguments
AI-assisted analysis of psf/requests@8068356288 (2026-08-11).
Data as JSON: /api/errors/2db9511a196b24c2.
Report an issue: GitHub.
Appendix: source
Thrown at src/requests/cookies.py:421
) -> str | None:
"""Requests uses this method internally to get cookie values.
If there are conflicting cookies, _find arbitrarily chooses one.
See _find_no_duplicates if you want an exception thrown if there are
conflicting cookies.
:param name: a string containing name of cookie
:param domain: (optional) string containing domain of cookie
:param path: (optional) string containing path of cookie
:return: cookie.value
"""
for cookie in iter(self):
if cookie.name == name:
if domain is None or cookie.domain == domain:
if path is None or cookie.path == path:
return cookie.value
raise KeyError(f"name={name!r}, domain={domain!r}, path={path!r}")
def _find_no_duplicates(
self, name: str, domain: str | None = None, path: str | None = None
) -> str:
"""Both ``__get_item__`` and ``get`` call this function: it's never
used elsewhere in Requests.
:param name: a string containing name of cookie
:param domain: (optional) string containing domain of cookie
:param path: (optional) string containing path of cookie
:raises KeyError: if cookie is not found
:raises CookieConflictError: if there are multiple cookies
that match name and optionally domain and path
:return: cookie.value
"""
toReturn = None
for cookie in iter(self):
if cookie.name == name:View on GitHub (pinned to 8068356288)