psf/requests · error · CookieConflictError

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

Error message

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

What it means

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.

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 414f0513c3)

Solutions

  1. Disambiguate the lookup: session.cookies.get('sessionid', domain='app.example.com', path='/').
  2. Iterate the jar and select explicitly: [c for c in session.cookies if c.name == 'sessionid' and c.domain == '...'].
  3. Use separate Session objects per host if cookies should not be shared.
  4. Clear the stale duplicate with session.cookies.clear(domain, path, name) before the lookup.

Example fix

# before
sid = session.cookies['sessionid']  # CookieConflictError

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

Strategy: try-catch

Validate before calling

names = [c.name for c in jar]
is_ambiguous = names.count('sessionid') > 1

Type guard

def has_duplicate_cookie(jar, name):
    return sum(1 for c in jar if c.name == name) > 1

Try / catch

try:
    value = jar['sessionid']
except requests.cookies.CookieConflictError:
    value = jar.get('sessionid', domain='api.example.com', path='/')

Prevention

When it happens

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

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

Related errors


AI-assisted analysis of psf/requests@414f0513c3 (2026-07-31). Data as JSON: /data/errors/0e62d2f5b65c53f9.json. Report an issue: GitHub ↗.