BerriAI/litellm · error · SSRFError
Too many redirects
Error message
Too many redirects
What it means
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.
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.
Example fix
# before
resp = safe_get(client, url) # site bounces login <-> content forever
# SSRFError: Too many redirects
# after
try:
resp = safe_get(client, url)
except SSRFError as e:
if str(e) == "Too many redirects":
raise HTTPException(status_code=502, detail="URL redirect loop detected")
raise Defensive patterns
Strategy: try-catch
Validate before calling
# cheap pre-flight: refuse chains that already look loopy
import httpx
r = httpx.head(url, follow_redirects=False)
seen = {url}
while r.is_redirect and len(seen) <= 11:
nxt = str(httpx.URL(url).join(r.headers["location"]))
if nxt in seen:
raise ValueError("redirect loop detected before fetch")
seen.add(nxt)
r = httpx.head(nxt, follow_redirects=False) Try / catch
from litellm.litellm_core_utils.url_utils import SSRFError
try:
resp = safe_get(client, url)
except SSRFError as e:
if str(e) == "Too many redirects":
raise HTTPException(status_code=502, detail="URL redirect loop detected")
raise Prevention
- 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.
When it happens
Trigger: 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.
Common situations: Cookie-less fetches of sites that bounce between login and content; misconfigured vhosts with rewrite rules; chained URL shorteners.
Related errors
- Redirect response has no Location header
- Origin mismatch on host
- Origin mismatch on port
- Origin mismatch on scheme
- URL scheme is not allowed
AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18).
Data as JSON: /api/errors/51f6552f86d903a0.
Report an issue: GitHub.
Appendix: source
Thrown at litellm/litellm_core_utils/url_utils.py:412
if not getattr(litellm, "user_url_validation", True):
kwargs.setdefault("follow_redirects", True)
return client.get(url, **kwargs)
kwargs.pop("follow_redirects", None)
caller_headers: Final = kwargs.pop("headers", {})
for _ in range(_MAX_REDIRECTS):
validated_url, original_host = validate_url(url)
response = client.get(
validated_url,
headers={**caller_headers, "Host": original_host},
follow_redirects=False,
**kwargs,
)
if not response.is_redirect:
return response
# Resolve the next hop against the ORIGINAL (pre-rewrite) URL so
# relative Location headers keep the original hostname.
url = _extract_redirect_url(response, url)
raise SSRFError("Too many redirects")
async def async_safe_get(client: Any, url: str, **kwargs: Any) -> Any:
"""Async version of safe_get."""
if not getattr(litellm, "user_url_validation", True):
kwargs.setdefault("follow_redirects", True)
return await client.get(url, **kwargs)
kwargs.pop("follow_redirects", None)
caller_headers: Final = kwargs.pop("headers", {})
for _ in range(_MAX_REDIRECTS):
validated_url, original_host = validate_url(url)
response = await client.get(
validated_url,
headers={**caller_headers, "Host": original_host},
follow_redirects=False,
**kwargs,
)
if not response.is_redirect:View on GitHub (pinned to 77b7c6c40c)