{"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":null,"httpStatus":null,"severity":"error","filePath":"server/utils/ImageGenerators/base.js","lineNumber":165,"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":147,"sourceCodeEnd":173,"githubUrl":"https://github.com/Mintplex-Labs/anything-llm/blob/f92433b4ea0598492a1e6645ea22addbb4dd1287/server/utils/ImageGenerators/base.js#L147-L173","documentation":"In requestImage() — the generation path — when the provider's response carries an image URL, AnythingLLM fetches it and throws 'Failed to fetch generated image: <status>' on any non-2xx. Generation succeeded; the secondary download of the image bytes failed. This mirrors the edited-image path but for client.images.generate() results.","triggerScenarios":"Signed CDN URL expired by the time of the fetch; the server cannot reach the image host (docker networking, egress restrictions, DNS failure); CDN rate limiting (429) or hotlink blocking (403); the provider's object store already evicted the image.","commonSituations":"URL-returning models (dall-e-2/3 style) on locked-down networks; delayed downloads of large images past a short URL TTL; Azure OpenAI short-lived blob URLs.","solutions":["Retry generation immediately — a new request produces a fresh URL.","Open egress from the AnythingLLM host to the provider's image host/CDN.","Use a model or setting that returns base64 inline (e.g. gpt-image-1) so no download is needed.","Map the status: 403/401 → link access, 404 → expired/evicted URL, 5xx → CDN incident."],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"async function generateWithRetry(generator, params, maxAttempts = 2) {\n  for (let i = 1; ; i++) {\n    try {\n      return await generator.generateImage(params);\n    } catch (err) {\n      if (\n        err.message.startsWith(\"Failed to fetch generated image:\") &&\n        i < maxAttempts\n      ) {\n        continue; // regeneration yields a fresh URL\n      }\n      throw err;\n    }\n  }\n}","preventionTips":["Whitelist the provider's image CDN in egress rules alongside the API host.","Use base64-returning models where available to skip the download step.","Fetch the returned URL immediately, not after other processing."],"tags":["image-generation","download","http-status"],"backgroundTag":"image-download-failed","analyzedSha":"f92433b4ea0598492a1e6645ea22addbb4dd1287","analyzedAt":"2026-08-18T10:02:21.017Z","contentChangedAt":"2026-08-18T10:02:21.017Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}