{"record":{"id":"37e095a1d3b1fe40","repo":"Mintplex-Labs/anything-llm","slug":"failed-to-fetch-generated-image-imgres-status","errorCode":null,"errorMessage":"Failed to fetch generated image: ${imgRes.status}","messagePattern":"Failed to fetch generated image: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/utils/ImageGenerators/localAi/index.js","lineNumber":107,"sourceCode":"        `Image ${images.length ? \"edit\" : \"generation\"} failed (${res.status}): ${body || res.statusText}`\n      );\n    }\n\n    const payload = await res.json();\n    const image = payload?.data?.[0];\n    let result;\n    if (image?.b64_json) {\n      result = { buffer: Buffer.from(image.b64_json, \"base64\") };\n    } else if (image?.url) {\n      const imageFetchHeaders = {};\n      if (this.client.apiKey)\n        imageFetchHeaders.Authorization = `Bearer ${this.client.apiKey}`;\n      const imgRes = await fetch(image.url, {\n        headers: imageFetchHeaders,\n        signal: signal ?? null,\n      });\n      if (!imgRes.ok)\n        throw new Error(`Failed to fetch generated image: ${imgRes.status}`);\n      result = { buffer: Buffer.from(await imgRes.arrayBuffer()) };\n    } else {\n      throw new Error(\"Image provider returned no image data.\");\n    }\n    this._sendImageTelemetry(\"image_generated\", {\n      withReferences: images.length > 0,\n    });\n    return result;\n  }\n}\n\nmodule.exports = { LocalAiImageGenerator };\n","sourceCodeStart":89,"sourceCodeEnd":120,"githubUrl":"https://github.com/Mintplex-Labs/anything-llm/blob/f92433b4ea0598492a1e6645ea22addbb4dd1287/server/utils/ImageGenerators/localAi/index.js#L89-L120","documentation":"The LocalAI image generator fetches the generated image URL returned by the provider and throws when that fetch returns a non-ok HTTP status. It means the image generation API call itself succeeded, but downloading the resulting image failed.","triggerScenarios":"generateImage or editImage runs, the provider returns an image URL, but the follow-up fetch of that URL returns 404/403/5xx — expired URL, wrong host in the URL, or missing/invalid Authorization header for the image fetch.","commonSituations":"LocalAI/other provider returning URLs that expire immediately or point to container-internal hosts unreachable from the app; missing API key on the image fetch (imageFetchHeaders.Authorization only set when apiKey exists); URL scheme mismatch (https vs http) in LocalAI config.","solutions":["Check imgRes.status in the message: 404 usually means an expired or host-relative URL; 401/403 means auth is required.","Verify the LocalAI base URL is reachable from the app (not a container-internal hostname).","Ensure the API key is configured so the image fetch includes the Authorization header.","Test fetching the returned image URL manually with curl to see the raw response.","If the provider can return base64 data instead of a URL, prefer that mode to avoid the second fetch."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// precheck: provider base URL reachable\nconst probe = await fetch(`${localAiBaseURL}/v1/models`);\nif (!probe.ok) throw new Error(`LocalAI unreachable: ${probe.status}`);","typeGuard":"function isHttpError(status) {\n  return typeof status === 'number' && (status < 200 || status >= 300);\n}","tryCatchPattern":"try {\n  const { buffer, mime } = await imageGen.generateImage(prompt);\n} catch (e) {\n  if (/Failed to fetch generated image: (404)/.test(e.message)) {\n    // image URL expired or host-relative: prefer base64/b64_json responses\n  } else if (/Failed to fetch generated image: (401|403)/.test(e.message)) {\n    // image fetch needs the API key: configure the provider key\n  } else { throw e; }\n}","preventionTips":["Prefer base64 image responses over provider-hosted URLs when supported.","Configure the API key so image fetches carry Authorization.","Make sure the provider base URL is resolvable from the app (not container-internal).","Fetch and inspect the returned image URL manually when debugging."],"tags":["image-generation","http","network","localai"],"backgroundTag":"http-error-response","analyzedSha":"f92433b4ea0598492a1e6645ea22addbb4dd1287","analyzedAt":"2026-09-22T02:13:38.154Z","contentChangedAt":"2026-09-22T02:13:38.154Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}