{"record":{"id":"69a57ed60b7867b9","repo":"psf/requests","slug":"you-can-only-merge-into-cookiejar","errorCode":null,"errorMessage":"You can only merge into CookieJar","messagePattern":"You can only merge into CookieJar","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/requests/cookies.py","lineNumber":614,"sourceCode":"        names_from_jar = [cookie.name for cookie in cookiejar]\n        for name in cookie_dict:\n            if overwrite or (name not in names_from_jar):\n                cookiejar.set_cookie(create_cookie(name, cookie_dict[name]))\n\n    return cookiejar\n\n\ndef merge_cookies(\n    cookiejar: CookieJar, cookies: dict[str, str] | CookieJar | None\n) -> CookieJar:\n    \"\"\"Add cookies to cookiejar and returns a merged CookieJar.\n\n    :param cookiejar: CookieJar object to add the cookies to.\n    :param cookies: Dictionary or CookieJar object to be added.\n    :rtype: CookieJar\n    \"\"\"\n    if not isinstance(cookiejar, cookielib.CookieJar):  # runtime guard\n        raise ValueError(\"You can only merge into CookieJar\")\n\n    if isinstance(cookies, dict):\n        cookiejar = cookiejar_from_dict(cookies, cookiejar=cookiejar, overwrite=False)\n    elif isinstance(cookies, cookielib.CookieJar):\n        if update_method := getattr(cookiejar, \"update\", None):\n            update_method(cookies)\n        else:\n            for cookie_in_jar in cookies:\n                cookiejar.set_cookie(cookie_in_jar)\n\n    return cookiejar\n","sourceCodeStart":596,"sourceCodeEnd":626,"githubUrl":"https://github.com/psf/requests/blob/8068356288978c4f54661ae6f95afe0e0831885e/src/requests/cookies.py#L596-L626","documentation":"Raised by merge_cookies when its first argument is not an instance of http.cookiejar.CookieJar. The function's contract is to add cookies into an existing jar, so a non-jar (e.g. a dict, list, or None passed as the jar) is rejected. This is a defensive runtime guard added because silently coercing would hide caller bugs.","triggerScenarios":"Passing a dict or list as the first positional arg to merge_cookies (e.g. merge_cookies({}, cookies)); passing None because the caller forgot to initialize a session cookiejar; passing a custom object that does not subclass cookielib.CookieJar.","commonSituations":"Refactor where a session was replaced by a plain dict; threading cookies through a helper that lost the session reference; library wrappers that accept 'a jar-like object' but actually require a real CookieJar.","solutions":["Pass an actual session.cookies (a RequestsCookieJar, which subclasses CookieJar) or create one with requests.cookies.cookiejar_from_dict({}).","If you have a dict, call merge_cookies(session.cookies, my_dict) — the dict belongs in the second argument.","Type-check the first argument with isinstance(jar, http.cookiejar.CookieJar) before calling."],"exampleFix":"// before\nmerge_cookies({}, {'sid': 'x'})\n\n// after\nimport requests.cookies as rc\njar = rc.cookiejar_from_dict({})\nmerge_cookies(jar, {'sid': 'x'})","handlingStrategy":"type-guard","validationCode":"import http.cookiejar\nfrom requests.cookies import merge_cookies\n\ndef safe_merge(jar, cookies):\n    if not isinstance(jar, http.cookiejar.CookieJar):\n        raise TypeError('first argument must be a http.cookiejar.CookieJar')\n    return merge_cookies(jar, cookies)","typeGuard":"import http.cookiejar\n\ndef is_cookiejar(obj) -> bool:\n    return isinstance(obj, http.cookiejar.CookieJar)","tryCatchPattern":"import http.cookiejar\nfrom requests.cookies import merge_cookies, cookiejar_from_dict\n\nif not isinstance(myjar, http.cookiejar.CookieJar):\n    myjar = cookiejar_from_dict({})\nmerge_cookies(myjar, cookies)","preventionTips":["Always pass session.cookies (a RequestsCookieJar) as the first argument.","Use cookiejar_from_dict({}) to create an empty jar when you have no session.","Type-annotate the parameter as CookieJar to catch misuse at static-analysis time."],"tags":["cookies","valueerror","cookiejar","merge","validation"],"backgroundTag":null,"analyzedSha":"8068356288978c4f54661ae6f95afe0e0831885e","analyzedAt":"2026-08-11T20:11:09.238Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}