{"record":{"id":"51f6552f86d903a0","repo":"BerriAI/litellm","slug":"too-many-redirects","errorCode":null,"errorMessage":"Too many redirects","messagePattern":"Too many redirects","errorType":"exception","errorClass":"SSRFError","httpStatus":null,"severity":"error","filePath":"litellm/litellm_core_utils/url_utils.py","lineNumber":412,"sourceCode":"    if not getattr(litellm, \"user_url_validation\", True):\n        kwargs.setdefault(\"follow_redirects\", True)\n        return client.get(url, **kwargs)\n    kwargs.pop(\"follow_redirects\", None)\n    caller_headers: Final = kwargs.pop(\"headers\", {})\n    for _ in range(_MAX_REDIRECTS):\n        validated_url, original_host = validate_url(url)\n        response = client.get(\n            validated_url,\n            headers={**caller_headers, \"Host\": original_host},\n            follow_redirects=False,\n            **kwargs,\n        )\n        if not response.is_redirect:\n            return response\n        # Resolve the next hop against the ORIGINAL (pre-rewrite) URL so\n        # relative Location headers keep the original hostname.\n        url = _extract_redirect_url(response, url)\n    raise SSRFError(\"Too many redirects\")\n\n\nasync def async_safe_get(client: Any, url: str, **kwargs: Any) -> Any:\n    \"\"\"Async version of safe_get.\"\"\"\n    if not getattr(litellm, \"user_url_validation\", True):\n        kwargs.setdefault(\"follow_redirects\", True)\n        return await client.get(url, **kwargs)\n    kwargs.pop(\"follow_redirects\", None)\n    caller_headers: Final = kwargs.pop(\"headers\", {})\n    for _ in range(_MAX_REDIRECTS):\n        validated_url, original_host = validate_url(url)\n        response = await client.get(\n            validated_url,\n            headers={**caller_headers, \"Host\": original_host},\n            follow_redirects=False,\n            **kwargs,\n        )\n        if not response.is_redirect:","sourceCodeStart":394,"sourceCodeEnd":430,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/litellm_core_utils/url_utils.py#L394-L430","documentation":"SSRFError raised by safe_get (sync) when a user-supplied URL redirects more than _MAX_REDIRECTS (10) times. Redirects are followed manually so every hop can be origin-validated; a chain longer than 10 hops — typically a loop — exhausts the budget and aborts.","triggerScenarios":"Redirect loops (A to B to A, or a host redirecting to itself for auth), long shortener chains, or servers that redirect endlessly when cookies are absent.","commonSituations":"Cookie-less fetches of sites that bounce between login and content; misconfigured vhosts with rewrite rules; chained URL shorteners.","solutions":["Count the hops externally: curl -sI -L -o /dev/null -w '%{num_redirects}' <url>.","Fix the loop on the origin server (often a session/cookie-dependent redirect).","Fetch the final URL directly once it is known.","Catch SSRFError and surface 'too many redirects' as a 4xx/502."],"exampleFix":"# before\nresp = safe_get(client, url)  # site bounces login <-> content forever\n# SSRFError: Too many redirects\n\n# after\ntry:\n    resp = safe_get(client, url)\nexcept SSRFError as e:\n    if str(e) == \"Too many redirects\":\n        raise HTTPException(status_code=502, detail=\"URL redirect loop detected\")\n    raise","handlingStrategy":"try-catch","validationCode":"# cheap pre-flight: refuse chains that already look loopy\nimport httpx\nr = httpx.head(url, follow_redirects=False)\nseen = {url}\nwhile r.is_redirect and len(seen) <= 11:\n    nxt = str(httpx.URL(url).join(r.headers[\"location\"]))\n    if nxt in seen:\n        raise ValueError(\"redirect loop detected before fetch\")\n    seen.add(nxt)\n    r = httpx.head(nxt, follow_redirects=False)","typeGuard":null,"tryCatchPattern":"from litellm.litellm_core_utils.url_utils import SSRFError\n\ntry:\n    resp = safe_get(client, url)\nexcept SSRFError as e:\n    if str(e) == \"Too many redirects\":\n        raise HTTPException(status_code=502, detail=\"URL redirect loop detected\")\n    raise","preventionTips":["Pre-check hop counts with curl -sI -L -w '%{num_redirects}' for URLs you control.","Report redirect loops to the origin operator; the client cannot fix a server-side loop.","Cache final destinations for recurring fetches instead of walking the chain every time."],"tags":["ssrf","redirect","redirect-loop","http"],"backgroundTag":"too-many-redirects","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}