{"record":{"id":"61fcdce98c38f992","repo":"Mintplex-Labs/anything-llm","slug":"failed-to-fetch-generated-image-res-status","errorCode":null,"errorMessage":"Failed to fetch generated image: ${res.status}","messagePattern":"Failed to fetch generated image: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"server/utils/ImageGenerators/base.js","lineNumber":138,"sourceCode":"        prompt,\n        size,\n        n: 1,\n      },\n      { signal: signal ?? undefined }\n    );\n\n    // Some OpenAI-compatible providers (e.g. Ollama) return the body with a\n    // non-JSON content-type (`application/x-ndjson`), so the SDK hands back the\n    // raw string unparsed. Normalize to an object before reading the image.\n    const { safeJsonParse } = require(\"../http\");\n    const payload = typeof result === \"string\" ? safeJsonParse(result) : result;\n    const image = payload?.data?.[0];\n    if (image?.b64_json)\n      return { buffer: Buffer.from(image.b64_json, \"base64\") };\n    if (image?.url) {\n      const res = await fetch(image.url, { signal: signal ?? null });\n      if (!res.ok)\n        throw new Error(`Failed to fetch generated image: ${res.status}`);\n      return { buffer: Buffer.from(await res.arrayBuffer()) };\n    }\n    throw new Error(\"Image provider returned no image data.\");\n  }\n}\n\nmodule.exports = { BaseImageGenerator };\n","sourceCodeStart":120,"sourceCodeEnd":146,"githubUrl":"https://github.com/Mintplex-Labs/anything-llm/blob/526360e320da9d1b36074be5ed64fe76e5bbfbbd/server/utils/ImageGenerators/base.js#L120-L146","documentation":"Thrown in requestImage (the text-to-image path) after images.generate returns a result containing a URL. The code fetches that URL to download the generated image; if the fetch is not ok it throws only the numeric status. This is the download stage of generation: the generation call succeeded but the hosted image could not be retrieved.","triggerScenarios":"images.generate returns a URL (provider did not return base64), and the subsequent fetch of that URL returns non-2xx: expired signed URL, CDN/provider storage outage, 403 on the storage token, region restrictions, or the abort signal firing during download.","commonSituations":"Providers defaulting to URL output (e.g. DALL-E style) where the link is short-lived; deployments with restricted egress to the image-hosting domain; racing an AbortController timeout against a slow image render+download.","solutions":["Retry the generation/download — expired signed URLs are often transient.","Request response_format=b64_json from providers that support it to skip the second fetch.","Ensure egress to the provider's image-hosting domain is allowed in your network/container policy.","Lengthen or remove an aggressive AbortController timeout that kills the download mid-flight."],"exampleFix":"// before\nconst res = await fetch(image.url, { signal: signal ?? null });\nif (!res.ok) throw new Error(`Failed to fetch generated image: ${res.status}`);\n\n// after: retry transient 5xx download failures\nlet res;\nfor (let i = 0; i < 3; i++) {\n  res = await fetch(image.url, { signal: signal ?? null });\n  if (res.ok) break;\n  if (res.status < 500) throw new Error(`Failed to fetch generated image: ${res.status}`);\n  await new Promise(r => setTimeout(r, 500 * (i + 1)));\n}","handlingStrategy":"retry","validationCode":"// No pre-call check possible for a not-yet-returned URL.\n// Ensure egress and prefer b64_json where the provider supports it:\nconst prefersBase64 = ['openai'].includes(process.env.IMAGE_GEN_PROVIDER);","typeGuard":"/** @param {unknown} e */\nfunction isGeneratedImageFetchError(e) {\n  return e instanceof Error && /^Failed to fetch generated image: \\d+$/.test(e.message);\n}","tryCatchPattern":"async function downloadGenerated(url, signal) {\n  for (let i = 0; i < 3; i++) {\n    const r = await fetch(url, { signal: signal ?? null });\n    if (r.ok) return Buffer.from(await r.arrayBuffer());\n    if (r.status < 500) throw new Error(`Failed to fetch generated image: ${r.status}`);\n    await new Promise(res => setTimeout(res, 500 * (i + 1)));\n  }\n  throw new Error('Generated image download failed after retries');\n}","preventionTips":["Request b64_json from providers that support it to avoid the download fetch.","Permit egress to the provider's image-hosting domain.","Don't set an AbortController timeout that kills the download.","Retry 5xx downloads; treat 4xx as permanent."],"tags":["image-generation","download","network","transient","openai-compatible"],"backgroundTag":null,"analyzedSha":"526360e320da9d1b36074be5ed64fe76e5bbfbbd","analyzedAt":"2026-08-13T01:45:47.170Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}