Mintplex-Labs/anything-llm · error

Failed to fetch generated image

Error message

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

What it means

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.

Solutions

  1. Retry generation immediately — a new request produces a fresh URL.
  2. Open egress from the AnythingLLM host to the provider's image host/CDN.
  3. Use a model or setting that returns base64 inline (e.g. gpt-image-1) so no download is needed.
  4. Map the status: 403/401 → link access, 404 → expired/evicted URL, 5xx → CDN incident.
Defensive patterns

Strategy: retry

Try / catch

async function generateWithRetry(generator, params, maxAttempts = 2) {
  for (let i = 1; ; i++) {
    try {
      return await generator.generateImage(params);
    } catch (err) {
      if (
        err.message.startsWith("Failed to fetch generated image:") &&
        i < maxAttempts
      ) {
        continue; // regeneration yields a fresh URL
      }
      throw err;
    }
  }
}

Prevention

When it happens

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

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

Related errors


AI-assisted analysis of Mintplex-Labs/anything-llm@f92433b4ea (2026-08-18). Data as JSON: /api/errors/61fcdce98c38f992. Report an issue: GitHub.

Appendix: source

Thrown at server/utils/ImageGenerators/base.js:165

        prompt,
        size,
        n: 1,
      },
      { signal: signal ?? undefined }
    );

    // Some OpenAI-compatible providers (e.g. Ollama) return the body with a
    // non-JSON content-type (`application/x-ndjson`), so the SDK hands back the
    // raw string unparsed. Normalize to an object before reading the image.
    const { safeJsonParse } = require("../http");
    const payload = typeof result === "string" ? safeJsonParse(result) : result;
    const image = payload?.data?.[0];
    if (image?.b64_json)
      return { buffer: Buffer.from(image.b64_json, "base64") };
    if (image?.url) {
      const res = await fetch(image.url, { signal: signal ?? null });
      if (!res.ok)
        throw new Error(`Failed to fetch generated image: ${res.status}`);
      return { buffer: Buffer.from(await res.arrayBuffer()) };
    }
    throw new Error("Image provider returned no image data.");
  }
}

module.exports = { BaseImageGenerator };

View on GitHub (pinned to f92433b4ea)