{"record":{"id":"e3d832aa9206731a","repo":"vercel/ai","slug":"too-many-redirects-max-maxredirects","errorCode":null,"errorMessage":"Too many redirects (max ${maxRedirects})","messagePattern":"Too many redirects \\(max (.+?)\\)","errorType":"exception","errorClass":"DownloadError","httpStatus":null,"severity":"error","filePath":"packages/provider-utils/src/fetch-with-validated-redirects.ts","lineNumber":151,"sourceCode":"      // redirect that crosses origin. Only stripping Authorization (as the\n      // fetch spec does) is not enough on the server: providers authenticate\n      // with custom headers too (e.g. `x-key`), and without CORS there is\n      // nothing else stopping them from riding to a foreign host.\n      if (currentHeaders !== undefined && !isSameOrigin(nextUrl, currentUrl)) {\n        const userAgent = currentHeaders.get('user-agent');\n        currentHeaders = new Headers(\n          userAgent == null ? undefined : { 'user-agent': userAgent },\n        );\n      }\n\n      currentUrl = nextUrl;\n      continue;\n    }\n\n    return response;\n  }\n\n  throw new DownloadError({\n    url,\n    message: `Too many redirects (max ${maxRedirects})`,\n  });\n}\n","sourceCodeStart":133,"sourceCodeEnd":156,"githubUrl":"https://github.com/vercel/ai/blob/69428b1f8b037e4d118fb4853428d5c4e620493c/packages/provider-utils/src/fetch-with-validated-redirects.ts#L133-L156","documentation":"fetchWithValidatedRedirects caps redirect chains at maxRedirects (default 10) to prevent infinite redirect loops that would exhaust connections or enable DoS. When the chain exceeds the limit, the download is aborted with this DownloadError. Redirect loops are almost always a server- or configuration-side problem, not a client bug.","triggerScenarios":"A file/media URL passed to the SDK (via downloadBlob and downstream provider calls) redirects more than 10 times, or two URLs redirect to each other in a loop (e.g. http→https→http, or with/without trailing slash misconfiguration).","commonSituations":"Misconfigured reverse proxy bouncing between http and https; www vs non-www redirect loops; cookie-gated URLs bouncing between login and asset; CDN misconfiguration; an auth-required URL that keeps redirecting because credentials are stripped on cross-origin hops.","solutions":["Follow the URL manually (curl -IL) to inspect the redirect chain and find where it loops.","Fix the origin: correct http→https or www redirect configuration on the asset host so a single hop reaches the file.","Use the final, direct asset URL instead of a redirecting short link.","If the chain is legitimate but long, supply a custom fetch or raise maxRedirects where the API surface allows it."],"exampleFix":"// before: URL bounces forever between http/https\nconst blob = await downloadBlob('http://cdn.example.com/video.mp4');\n\n// after: use the final https URL directly\nconst blob = await downloadBlob('https://cdn.example.com/video.mp4');","handlingStrategy":"validation","validationCode":"async function countRedirects(url: string, max = 10): Promise<number> {\n  let hops = 0;\n  let current = url;\n  while (hops <= max) {\n    const res = await fetch(current, { redirect: 'manual' });\n    const location = res.headers.get('location');\n    if (!(res.status >= 300 && res.status < 400) || !location) return hops;\n    current = new URL(location, current).toString();\n    hops++;\n  }\n  throw new Error(`URL exceeds ${max} redirects`);\n}","typeGuard":"import { DownloadError } from '@ai-sdk/provider-utils';\nfunction isTooManyRedirects(e: unknown): boolean {\n  return DownloadError.isInstance(e) && e.message.startsWith('Too many redirects');\n}","tryCatchPattern":"try {\n  await downloadBlob(url);\n} catch (error) {\n  if (DownloadError.isInstance(error) && error.message.startsWith('Too many redirects')) {\n    // follow chain with curl -IL, fix the looping origin or use the final URL\n  }\n  throw error;\n}","preventionTips":["Trace suspect URLs with curl -IL before relying on them.","Fix http→https and www redirect loops at the origin/proxy.","Use final canonical asset URLs instead of redirect chains.","Remember cross-origin hops drop credentials — auth-gated redirect chains will loop."],"tags":["network","redirect","download","configuration"],"backgroundTag":"too-many-redirects","analyzedSha":"69428b1f8b037e4d118fb4853428d5c4e620493c","analyzedAt":"2026-08-30T12:32:21.016Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}