Mintplex-Labs/anything-llm · error

Failed to fetch generated image

Error message

Failed to fetch generated image: ${imgRes.status}

What it means

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.

Solutions

  1. Check imgRes.status in the message: 404 usually means an expired or host-relative URL; 401/403 means auth is required.
  2. Verify the LocalAI base URL is reachable from the app (not a container-internal hostname).
  3. Ensure the API key is configured so the image fetch includes the Authorization header.
  4. Test fetching the returned image URL manually with curl to see the raw response.
  5. If the provider can return base64 data instead of a URL, prefer that mode to avoid the second fetch.
Defensive patterns

Strategy: try-catch

Validate before calling

// precheck: provider base URL reachable
const probe = await fetch(`${localAiBaseURL}/v1/models`);
if (!probe.ok) throw new Error(`LocalAI unreachable: ${probe.status}`);

Type guard

function isHttpError(status) {
  return typeof status === 'number' && (status < 200 || status >= 300);
}

Try / catch

try {
  const { buffer, mime } = await imageGen.generateImage(prompt);
} catch (e) {
  if (/Failed to fetch generated image: (404)/.test(e.message)) {
    // image URL expired or host-relative: prefer base64/b64_json responses
  } else if (/Failed to fetch generated image: (401|403)/.test(e.message)) {
    // image fetch needs the API key: configure the provider key
  } else { throw e; }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "API error: {status}" and "HTTP 401/403/404/429/5xx" errors: non-2xx HTTP responses explained — this error's family across 27 libraries.

Related errors


AI-assisted analysis of Mintplex-Labs/anything-llm@f92433b4ea (2026-09-22). Data as JSON: /api/errors/37e095a1d3b1fe40. Report an issue: GitHub.

Appendix: source

Thrown at server/utils/ImageGenerators/localAi/index.js:107

        `Image ${images.length ? "edit" : "generation"} failed (${res.status}): ${body || res.statusText}`
      );
    }

    const payload = await res.json();
    const image = payload?.data?.[0];
    let result;
    if (image?.b64_json) {
      result = { buffer: Buffer.from(image.b64_json, "base64") };
    } else if (image?.url) {
      const imageFetchHeaders = {};
      if (this.client.apiKey)
        imageFetchHeaders.Authorization = `Bearer ${this.client.apiKey}`;
      const imgRes = await fetch(image.url, {
        headers: imageFetchHeaders,
        signal: signal ?? null,
      });
      if (!imgRes.ok)
        throw new Error(`Failed to fetch generated image: ${imgRes.status}`);
      result = { buffer: Buffer.from(await imgRes.arrayBuffer()) };
    } else {
      throw new Error("Image provider returned no image data.");
    }
    this._sendImageTelemetry("image_generated", {
      withReferences: images.length > 0,
    });
    return result;
  }
}

module.exports = { LocalAiImageGenerator };

View on GitHub (pinned to f92433b4ea)