qm icon indicating copy to clipboard operation
qm copied to clipboard

Inbound image attachments keep the declared or extension type -> PNG named .jpg gets HTTP 400 on every later turn

Open fo82 opened this issue 18 hours ago • 0 comments

Summary

If someone uploads a PNG whose file name ends in .jpg, QM sends it to the model as image/jpeg. Anthropic checks the bytes against the label and rejects the request with a 400. The image then stays in replayed history, so every turn after that in the same session fails the same way. Saying "try again" can't fix it. The session is stuck until the file is gone.

We hit it with an image saved from a mobile app: real PNG bytes, .jpg name.

Root cause

src/core/attachments.ts takes the type from the upload or the file extension and never looks at the bytes:

const mimetype = baseMime(a.mimetype || mimeFromName(name));
...
if (VISION_MIME_TYPES.has(mimetype) && bytes.length > 0 && bytes.length <= MAX_VISION_IMAGE_BYTES) {
  const imageBytes = await downscaleVisionImage(bytes, mimetype);
  images.push({
    name,
    mimeType: mimetype,

Browsers fill File.type from the extension, so a renamed PNG shows up as image/jpeg. That label is stored on the artifact, and artifactImageLoader in src/agent/replay-images.ts hands it back on every later turn. downscaleVisionImage only re-encodes images over 1568 px, and only when ImageMagick or ffmpeg is on PATH. Small images, or hosts without a converter, pass through with the wrong label.

Tool images don't have this problem. viewableImage already sniffs the format with sniffImageDimensions. Inbound attachments just don't use it.

Repro

  1. Take any PNG and rename it to photo.jpg.
  2. Attach it in web chat on a session using an Anthropic model.
  3. Observed:
messages.N.content.1.image.source.base64: The image was specified using the image/jpeg media type, but the image appears to be a image/png image (HTTP 400)
  1. Send any follow-up in that session. It fails with the same error.

I've only reproduced this on our own deployment. I checked the lines above against main at 49256683 and they're unchanged.

Suggested fix

Label images by their bytes at the two places that already have them. sniffImageDimensions already returns the format, and viewableImage already uses it this way:

// materializeInbound in src/core/attachments.ts
const format = sniffImageDimensions(bytes)?.format;
const mimetype = format ? `image/${format}` : baseMime(a.mimetype || mimeFromName(name));

// artifactImageLoader in src/agent/replay-images.ts, after the cached data is loaded
const format = sniffImageDimensions(Buffer.from(data, "base64"))?.format;
return { data, sizeBytes: artifact.sizeBytes, mimeType: format ? `image/${format}` : artifact.mimetype };

The replay change means sessions that already have a mislabeled image in history start working again. The bytes sit in the object store, so a SQL migration can't relabel them. restoreReplayImages then uses loaded.mimeType in place of the ref's type.

We have this as a local patch. The test changes are small: the existing replay-once test stores a PNG as image/jpeg, and the surface-file-id intake test now uses PNG bytes named .jpg.

fo82 avatar Dec 05 '26 00:12 fo82