psf/requests · error · CookieConflictError

There are multiple cookies with name

Error message

There are multiple cookies with name, {name!r}

What it means

Raised by RequestsCookieJar._find_no_duplicates when two or more stored cookies share the same name (and match the optional domain/path filters). Because subscript access (__getitem__) cannot disambiguate which value to return, requests refuses with CookieConflictError. This is a data-integrity guard, not a normal control-flow path.

Solutions

  1. Disambiguate by supplying domain= and path= to _find_no_duplicates via jar.get(name, domain=, path=) or clear_specific() to target one.
  2. Remove the stale duplicate with session.cookies.clear(domain=..., path=...) before re-issuing.
  3. Use jar.iter_cookies() (or iterate the jar) to enumerate all matches and pick the intended one.

Example fix

// before
value = session.cookies['sid']

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

Strategy: validation

Validate before calling

from requests.cookies import RequestsCookieJar, CookieConflictError

def get_unique_cookie(jar, name, domain=None, path=None, default=None):
    matches = [c for c in jar if c.name == name
               and (domain is None or c.domain == domain)
               and (path is None or c.path == path)]
    if len(matches) == 0:
        return default
    if len(matches) > 1:
        raise CookieConflictError(f"multiple cookies named {name!r}; disambiguate via domain/path")
    return matches[0].value

Type guard

from requests.cookies import RequestsCookieJar

def is_unique(jar: RequestsCookieJar, name, domain=None, path=None) -> bool:
    count = 0
    for c in jar:
        if c.name == name and (domain is None or c.domain == domain) and (path is None or c.path == path):
            count += 1
            if count > 1:
                return False
    return count == 1

Try / catch

from requests.cookies import CookieConflictError

try:
    value = session.cookies['sid']
except CookieConflictError:
    value = session.cookies.get('sid', domain='example.com', path='/')

Prevention

When it happens

Trigger: A server sets two Set-Cookie headers with the same name but different domains or paths (e.g. one for '/' and one for '/app'), and the caller does jar['name']; manually merging jars that contain overlapping names; using set() without specifying domain/path while duplicate names already exist.

Common situations: Login flows that issue a fresh token cookie on '/' while an old one persists on a subpath; third-party SSO cookies colliding with first-party names; cross-domain cookies with identical names stored in one jar.

Related errors


AI-assisted analysis of psf/requests@8068356288 (2026-08-11). Data as JSON: /api/errors/0e62d2f5b65c53f9. Report an issue: GitHub.

Appendix: source

Thrown at src/requests/cookies.py:444

        """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:
                if domain is None or cookie.domain == domain:
                    if path is None or cookie.path == path:
                        if toReturn is not None:
                            # if there are multiple cookies that meet passed in criteria
                            raise CookieConflictError(
                                f"There are multiple cookies with name, {name!r}"
                            )
                        # we will eventually return this as long as no cookie conflict
                        toReturn = cookie.value

        if toReturn is not None:
            return toReturn
        raise KeyError(f"name={name!r}, domain={domain!r}, path={path!r}")

    def __getstate__(self) -> dict[str, Any]:
        """Unlike a normal CookieJar, this class is pickleable."""
        state = self.__dict__.copy()
        # remove the unpickleable RLock object
        state.pop("_cookies_lock")
        return state

    def __setstate__(self, state: dict[str, Any]) -> None:
        """Unlike a normal CookieJar, this class is pickleable."""

View on GitHub (pinned to 8068356288)