danny-avila/LibreChat · warning

Image validation failed for ${file.filename}: ${validation.e

Error message

Image validation failed for ${file.filename}: ${validation.error}

What it means

Thrown in encode.js when validateImage() — which checks format, dimensions, byte size against the configured file size limit, and (if configured) endpoint-specific policy — returns `{ isValid: false }` for an image buffer about to be embedded as an image_url part. The wrapped `validation.error` carries the specific failure reason from the validator.

Source

Thrown at api/server/services/Files/images/encode.js:214

      continue;
    }

    /** Validate image buffer against size limits */
    if (file.height && file.width) {
      const imageBuffer = imageContent.startsWith('http')
        ? null
        : Buffer.from(imageContent, 'base64');

      if (imageBuffer) {
        const validation = await validateImage(
          imageBuffer,
          imageBuffer.length,
          effectiveEndpoint,
          configuredFileSizeLimit,
        );

        if (!validation.isValid) {
          throw new Error(`Image validation failed for ${file.filename}: ${validation.error}`);
        }
      }
    }

    const imagePart = {
      type: ContentTypes.IMAGE_URL,
      image_url: {
        url: imageContent.startsWith('http')
          ? imageContent
          : `data:${file.type};base64,${imageContent}`,
        detail,
      },
    };

    if (mode === VisionModes.agents) {
      result.image_urls.push({ ...imagePart });
      result.files.push({ ...fileMetadata });
      continue;

View on GitHub (pinned to 5ff282f900)

Solutions

  1. Inspect `validation.error` for the specific reason (size, format, dimensions) and address that root cause.
  2. Reduce the image's bytes or dimensions before upload (compress/resize client-side).
  3. Confirm the file is a real image (`file` command on the upload) and not a renamed binary.
  4. If the limit is wrong, update the relevant FILE_SIZE_LIMIT / endpoint image config rather than disabling validation.

Example fix

// before: validation surfaces generic
if (!validation.isValid) throw new Error(...);

// after: branch on the specific reason
if (!validation.isValid) {
  if (validation.error?.includes('size')) return res.status(413).json({ error: 'Image too large; max 5 MB.' });
  if (validation.error?.includes('format')) return res.status(415).json({ error: 'Unsupported image format.' });
  return res.status(422).json({ error: validation.error });
}
Defensive patterns

Strategy: validation

Validate before calling

// Run the same validator before embedding
const v = await validateImage(buffer, buffer.length, endpoint, configuredLimit);
if (!v.isValid) {
  return res.status(422).json({ error: `Image rejected: ${v.error}` });
}

Try / catch

try { await encodeAttachments(parts); }
catch (e) {
  if (/Image validation failed/.test(e.message)) {
    return res.status(422).json({ error: e.message });
  }
  throw e;
}

Prevention

When it happens

Trigger: An image attachment's buffer fails format detection (not a real image), exceeds the configured file size limit, exceeds the endpoint's max dimensions, or matches a disallowed MIME type for the active endpoint (`effectiveEndpoint`). Validation runs only when an `imageBuffer` was decoded from base64 — URL-only image parts skip this branch.

Common situations: A user uploads a file with an `.png` extension that is actually a PDF or a renamed executable; the admin lowered FILE_SIZE_LIMIT after larger images were accepted; a model endpoint configured with stricter dimension limits than the global default; a corrupt image that sharp cannot parse.

Related errors


AI-assisted analysis of danny-avila/LibreChat@5ff282f900 (2026-08-12). Data as JSON: /api/errors/4407f48bf552b512. Report an issue: GitHub.