{"id":"0e62d2f5b65c53f9","repo":"psf/requests","slug":"there-are-multiple-cookies-with-name-name-r","errorCode":null,"errorMessage":"There are multiple cookies with name, {name!r}","messagePattern":"There are multiple cookies with name, (.+?)","errorType":"exception","errorClass":"CookieConflictError","httpStatus":null,"severity":"error","filePath":"src/requests/cookies.py","lineNumber":444,"sourceCode":"        \"\"\"Both ``__get_item__`` and ``get`` call this function: it's never\n        used elsewhere in Requests.\n\n        :param name: a string containing name of cookie\n        :param domain: (optional) string containing domain of cookie\n        :param path: (optional) string containing path of cookie\n        :raises KeyError: if cookie is not found\n        :raises CookieConflictError: if there are multiple cookies\n            that match name and optionally domain and path\n        :return: cookie.value\n        \"\"\"\n        toReturn = None\n        for cookie in iter(self):\n            if cookie.name == name:\n                if domain is None or cookie.domain == domain:\n                    if path is None or cookie.path == path:\n                        if toReturn is not None:\n                            # if there are multiple cookies that meet passed in criteria\n                            raise CookieConflictError(\n                                f\"There are multiple cookies with name, {name!r}\"\n                            )\n                        # we will eventually return this as long as no cookie conflict\n                        toReturn = cookie.value\n\n        if toReturn is not None:\n            return toReturn\n        raise KeyError(f\"name={name!r}, domain={domain!r}, path={path!r}\")\n\n    def __getstate__(self) -> dict[str, Any]:\n        \"\"\"Unlike a normal CookieJar, this class is pickleable.\"\"\"\n        state = self.__dict__.copy()\n        # remove the unpickleable RLock object\n        state.pop(\"_cookies_lock\")\n        return state\n\n    def __setstate__(self, state: dict[str, Any]) -> None:\n        \"\"\"Unlike a normal CookieJar, this class is pickleable.\"\"\"","sourceCodeStart":426,"sourceCodeEnd":462,"githubUrl":"https://github.com/psf/requests/blob/414f0513c33883adf6f2b46901d4f0b38a455851/src/requests/cookies.py#L426-L462","documentation":"A requests.cookies.CookieConflictError raised by RequestsCookieJar._find_no_duplicates() in src/requests/cookies.py. Dict-style access (jar['name'] and jar.get('name')) goes through this method, which refuses to guess when two or more cookies share the same name (differing only in domain or path) — unlike the internal _find(), which picks one arbitrarily.","triggerScenarios":"session.cookies['sessionid'] or session.cookies.get('sessionid') when the jar holds that name for multiple domains/paths — e.g. after requests to both 'example.com' and 'app.example.com' that each set 'sessionid', or a server setting the same cookie name on '/' and '/api'.","commonSituations":"Crawlers and multi-tenant API clients reusing one Session across subdomains; login flows where both the apex and a subdomain set the same cookie; servers reissuing a cookie with a different path during redirects.","solutions":["Disambiguate the lookup: session.cookies.get('sessionid', domain='app.example.com', path='/').","Iterate the jar and select explicitly: [c for c in session.cookies if c.name == 'sessionid' and c.domain == '...'].","Use separate Session objects per host if cookies should not be shared.","Clear the stale duplicate with session.cookies.clear(domain, path, name) before the lookup."],"exampleFix":"# before\nsid = session.cookies['sessionid']  # CookieConflictError\n\n# after\nsid = session.cookies.get('sessionid', domain='app.example.com', path='/')","handlingStrategy":"try-catch","validationCode":"names = [c.name for c in jar]\nis_ambiguous = names.count('sessionid') > 1","typeGuard":"def has_duplicate_cookie(jar, name):\n    return sum(1 for c in jar if c.name == name) > 1","tryCatchPattern":"try:\n    value = jar['sessionid']\nexcept requests.cookies.CookieConflictError:\n    value = jar.get('sessionid', domain='api.example.com', path='/')","preventionTips":["When the same cookie name can exist for multiple domains/paths (common with shared parent domains), always pass domain= and path= to jar.get()","Catch requests.cookies.CookieConflictError specifically rather than a bare except","Use per-host sessions to avoid accumulating same-named cookies from different domains","Avoid dict-style jar[name] access on jars populated from multiple hosts"],"tags":["cookies","cookie-conflict","session","duplicates"],"analyzedSha":"414f0513c33883adf6f2b46901d4f0b38a455851","analyzedAt":"2026-07-31T19:24:57.696Z","schemaVersion":2}