encode/httpx · warning · CookieConflict

Multiple cookies exist with name={name}

Error message

Multiple cookies exist with name={name}

What it means

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.

Source

Thrown at httpx/_models.py:1161

    def get(  # type: ignore
        self,
        name: str,
        default: str | None = None,
        domain: str | None = None,
        path: str | None = None,
    ) -> str | None:
        """
        Get a cookie by name. May optionally include domain and path
        in order to specify exactly which cookie to retrieve.
        """
        value = None
        for cookie in self.jar:
            if cookie.name == name:
                if domain is None or cookie.domain == domain:
                    if path is None or cookie.path == path:
                        if value is not None:
                            message = f"Multiple cookies exist with name={name}"
                            raise CookieConflict(message)
                        value = cookie.value

        if value is None:
            return default
        return value

    def delete(
        self,
        name: str,
        domain: str | None = None,
        path: str | None = None,
    ) -> None:
        """
        Delete a cookie by name. May optionally include domain and path
        in order to specify exactly which cookie to delete.
        """
        if domain is not None and path is not None:
            return self.jar.clear(domain, path, name)

View on GitHub (pinned to b5addb64f0)

Solutions

  1. Disambiguate with domain/path: `response.cookies.get('session', domain='example.com', path='/')`.
  2. Clear stale cookies with `client.cookies.delete('session', domain=...)` before the lookup.
  3. Inspect the jar: `for c in response.cookies.jar: print(c.name, c.domain, c.path)` to see duplicates.
  4. Use a per-host cookie scope or a fresh client for isolated flows.

Example fix

// before
session = response.cookies.get('session')  # CookieConflict

// after
session = response.cookies.get('session', domain='example.com', path='/')
Defensive patterns

Strategy: try-catch

Validate before calling

def names_in_jar(jar) -> set:
    from collections import Counter
    c = Counter(co.name for co in jar)
    return {n for n, k in c.items() if k > 1}

# before lookup:
dups = names_in_jar(response.cookies.jar)
if name in dups:
    # disambiguate by domain/path

Type guard

import httpx

def cookie_lookup_is_safe(jar, name: str) -> bool:
    matches = [c for c in jar if c.name == name]
    return len(matches) <= 1

Try / catch

try:
    value = response.cookies.get('session')
except httpx.CookieConflict:
    value = response.cookies.get('session', domain='example.com', path='/')

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of encode/httpx@b5addb64f0 (2026-08-04). Data as JSON: /data/errors/0a8e309c9084ef0d.json. Report an issue: GitHub.