{"id":"0a8e309c9084ef0d","repo":"encode/httpx","slug":"multiple-cookies-exist-with-name-name","errorCode":null,"errorMessage":"Multiple cookies exist with name={name}","messagePattern":"Multiple cookies exist with name=(.+?)","errorType":"exception","errorClass":"CookieConflict","httpStatus":null,"severity":"warning","filePath":"httpx/_models.py","lineNumber":1161,"sourceCode":"    def get(  # type: ignore\n        self,\n        name: str,\n        default: str | None = None,\n        domain: str | None = None,\n        path: str | None = None,\n    ) -> str | None:\n        \"\"\"\n        Get a cookie by name. May optionally include domain and path\n        in order to specify exactly which cookie to retrieve.\n        \"\"\"\n        value = None\n        for cookie in self.jar:\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 value is not None:\n                            message = f\"Multiple cookies exist with name={name}\"\n                            raise CookieConflict(message)\n                        value = cookie.value\n\n        if value is None:\n            return default\n        return value\n\n    def delete(\n        self,\n        name: str,\n        domain: str | None = None,\n        path: str | None = None,\n    ) -> None:\n        \"\"\"\n        Delete a cookie by name. May optionally include domain and path\n        in order to specify exactly which cookie to delete.\n        \"\"\"\n        if domain is not None and path is not None:\n            return self.jar.clear(domain, path, name)","sourceCodeStart":1143,"sourceCodeEnd":1179,"githubUrl":"https://github.com/encode/httpx/blob/b5addb64f0161ff6bfe94c124ef76f6a1fba5254/httpx/_models.py#L1143-L1179","documentation":"Raised as `CookieConflict` by `Cookies.get()` (and `__getitem__`) when more than one cookie in the jar matches the requested name (and the optional domain/path filters do not disambiguate). httpx delegates to `http.cookiejar.CookieJar`, which can hold multiple cookies of the same name across different domains/paths; an unambiguous lookup is then impossible, so it raises rather than return an arbitrary value.","triggerScenarios":"Calling `response.cookies.get('session')` or `client.cookies['session']` when the jar holds two `session` cookies — e.g. one for `example.com` and one for `api.example.com`, or a stale cookie plus a fresh one after a login flow.","commonSituations":"Sites that set the same cookie name across subdomains; SSO redirects that re-set a cookie under a different domain without clearing the old one; long-lived client instances accumulating cookies across multiple hosts.","solutions":["Disambiguate with domain/path: `response.cookies.get('session', domain='example.com', path='/')`.","Clear stale cookies with `client.cookies.delete('session', domain=...)` before the lookup.","Inspect the jar: `for c in response.cookies.jar: print(c.name, c.domain, c.path)` to see duplicates.","Use a per-host cookie scope or a fresh client for isolated flows."],"exampleFix":"// before\nsession = response.cookies.get('session')  # CookieConflict\n\n// after\nsession = response.cookies.get('session', domain='example.com', path='/')","handlingStrategy":"try-catch","validationCode":"def names_in_jar(jar) -> set:\n    from collections import Counter\n    c = Counter(co.name for co in jar)\n    return {n for n, k in c.items() if k > 1}\n\n# before lookup:\ndups = names_in_jar(response.cookies.jar)\nif name in dups:\n    # disambiguate by domain/path","typeGuard":"import httpx\n\ndef cookie_lookup_is_safe(jar, name: str) -> bool:\n    matches = [c for c in jar if c.name == name]\n    return len(matches) <= 1","tryCatchPattern":"try:\n    value = response.cookies.get('session')\nexcept httpx.CookieConflict:\n    value = response.cookies.get('session', domain='example.com', path='/')","preventionTips":["Always pass domain= and path= when looking up cookies that may span subdomains.","Periodically clear stale cookies from long-lived clients.","Inspect the jar with list(response.cookies.jar) when names overlap."],"tags":["cookies","cookie-conflict","response","client"],"analyzedSha":"b5addb64f0161ff6bfe94c124ef76f6a1fba5254","analyzedAt":"2026-08-04T19:32:56.768Z","schemaVersion":2}