Mintplex-Labs/anything-llm · error · Error

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

Error message

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

What it means

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.

Source

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

        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 526360e320)

Solutions

  1. Retry the generation/download — expired signed URLs are often transient.
  2. Request response_format=b64_json from providers that support it to skip the second fetch.
  3. Ensure egress to the provider's image-hosting domain is allowed in your network/container policy.
  4. Lengthen or remove an aggressive AbortController timeout that kills the download mid-flight.

Example fix

// before
const res = await fetch(image.url, { signal: signal ?? null });
if (!res.ok) throw new Error(`Failed to fetch generated image: ${res.status}`);

// after: retry transient 5xx download failures
let res;
for (let i = 0; i < 3; i++) {
  res = await fetch(image.url, { signal: signal ?? null });
  if (res.ok) break;
  if (res.status < 500) throw new Error(`Failed to fetch generated image: ${res.status}`);
  await new Promise(r => setTimeout(r, 500 * (i + 1)));
}
Defensive patterns

Strategy: retry

Validate before calling

// No pre-call check possible for a not-yet-returned URL.
// Ensure egress and prefer b64_json where the provider supports it:
const prefersBase64 = ['openai'].includes(process.env.IMAGE_GEN_PROVIDER);

Type guard

/** @param {unknown} e */
function isGeneratedImageFetchError(e) {
  return e instanceof Error && /^Failed to fetch generated image: \d+$/.test(e.message);
}

Try / catch

async function downloadGenerated(url, signal) {
  for (let i = 0; i < 3; i++) {
    const r = await fetch(url, { signal: signal ?? null });
    if (r.ok) return Buffer.from(await r.arrayBuffer());
    if (r.status < 500) throw new Error(`Failed to fetch generated image: ${r.status}`);
    await new Promise(res => setTimeout(res, 500 * (i + 1)));
  }
  throw new Error('Generated image download failed after retries');
}

Prevention

When it happens

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

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

Related errors


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