Mintplex-Labs/anything-llm · error · Error

Image edit failed (${res.status}): ${body || res.statusText}

Error message

Image edit failed (${res.status}): ${body || res.statusText}

What it means

Thrown by BaseImageGenerator.editImage after POSTing multipart/form-data to the OpenAI-compatible /images/edits endpoint. The check is !res.ok (any non-2xx HTTP status). It reads the response body as text (best-effort, falling back to empty string) and includes the numeric status plus the body or statusText so the underlying provider error is visible.

Source

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

        this.imageFieldName(images.length),
        new Blob([images[i]], { type: "image/png" }),
        `reference-${i}.png`
      );
    }

    const baseURL = this.client.baseURL.replace(/\/+$/, "");
    const res = await fetch(`${baseURL}/images/edits`, {
      method: "POST",
      headers: {
        Authorization: `Bearer ${this.client.apiKey}`,
      },
      body: formData,
      signal: signal ?? null,
    });

    if (!res.ok) {
      const body = await res.text().catch(() => "");
      throw new Error(
        `Image edit failed (${res.status}): ${body || res.statusText}`
      );
    }

    const payload = await res.json();
    const image = payload?.data?.[0];
    if (image?.b64_json)
      return { buffer: Buffer.from(image.b64_json, "base64") };
    if (image?.url) {
      const imgRes = await fetch(image.url, { signal: signal ?? null });
      if (!imgRes.ok)
        throw new Error(`Failed to fetch edited image: ${imgRes.status}`);
      return { buffer: Buffer.from(await imgRes.arrayBuffer()) };
    }
    throw new Error("Image edit returned no image data.");
  }

  async requestImage(prompt, size, signal) {

View on GitHub (pinned to 526360e320)

Solutions

  1. Read the numeric status in the message: 401/403 → fix IMAGE_GEN_*_API_KEY; 400 → check model supports edits and image format; 429 → wait and retry; 404 → provider has no edits endpoint.
  2. Confirm the selected IMAGE_GEN_MODEL_PREF actually supports image editing on the chosen provider.
  3. Ensure reference images are PNG buffers and within the provider's size/upload limits.
  4. Verify the base path env var points at a provider that implements /images/edits (OpenAI and Lemonade do; some Ollama builds do not).

Example fix

// before
const { buffer } = await generator.editImage({ prompt, images: [pngBuf] });

// after: validate inputs and surface the status
if (!pngBuf || pngBuf.length === 0) throw new Error('reference image is empty');
let result;
try {
  result = await generator.editImage({ prompt, images: [pngBuf] });
} catch (e) {
  if (/\(429\)/.test(e.message)) throw new Error('rate limited — retry later');
  throw e;
}
Defensive patterns

Strategy: try-catch

Validate before calling

function validateEditInputs(prompt, images) {
  if (!prompt || typeof prompt !== 'string') throw new Error('editImage requires a non-empty prompt');
  if (!Array.isArray(images) || images.length === 0) throw new Error('editImage requires at least one reference image Buffer');
  if (!images.every(b => Buffer.isBuffer(b) && b.length > 0)) throw new Error('reference images must be non-empty Buffers');
}

Type guard

/** @param {unknown} e */
function isImageEditHttpError(e) {
  return e instanceof Error && /^Image edit failed \(\d+\):/.test(e.message);
}

Try / catch

try {
  return await generator.editImage({ prompt, images });
} catch (e) {
  const status = (e.message.match(/\((\d+)\)/) || [,])[1];
  if (status === '429') throw new Error('Rate limited by image provider — retry later');
  if (status === '401' || status === '403') throw new Error('Bad image provider API key');
  if (status === '404') throw new Error('Provider does not support /images/edits');
  throw e;
}

Prevention

When it happens

Trigger: Calling editImage when the provider rejects the edit request: 401/403 for bad or missing API key, 400 for unsupported model/size or malformed image input, 413 for oversized image upload, 429 for rate limit, 404 when the provider does not implement /images/edits, or 5xx for upstream failures.

Common situations: Using a model that does not support image edits against the /images/edits endpoint; passing a non-PNG reference image; API key for the image provider expired; hitting the image-generation rate limit; provider (e.g. some Ollama builds) does not implement the edits endpoint at all.

Related errors


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