[BUG] Image MIME type detection should use magic bytes instead of file extension

Resolved 💬 3 comments Opened Dec 8, 2025 by sa9saQ Closed Dec 12, 2025

Preflight Checklist

  • [x] I have searched existing issues and this hasn't been reported yet
  • [x] This is a single bug report (please file separate reports for different bugs)
  • [x] I am using the latest version of Claude Code

What's Wrong?

When sending an image with mismatched extension and content (e.g., a JPEG file saved with .png extension), Claude Code determines
the MIME type from the file extension and sends it to the Anthropic API as image/png. The API detects the mismatch and returns a
400 error, which crashes the entire session.

This commonly occurs with screenshot tools like "Nano Banana Pro" that save JPEG content with .png extension.

What Should Happen?

Claude Code should detect the actual image format using magic bytes (file signature) instead of relying on the file extension. This
would allow correct MIME type detection regardless of the file extension.

For example:

  • PNG magic bytes: 89 50 4E 47 0D 0A 1A 0A
  • JPEG magic bytes: FF D8 FF
  • WebP magic bytes: 52 49 46 46 ... 57 45 42 50

Error Messages/Logs

API Error: 400 {"type":"error","error":{"type":"invalid_request_error","message":"messages.1.content.24.image.source.base64.data:        
  Image does not match the provided media type image/png"},"request_id":"req_011CVuYmAGosipwSBTKU9Xv7"}

Steps to Reproduce

  1. Create a fake PNG file (JPEG content with .png extension):

```bash
# Take a JPEG screenshot and save as .png, or:
cp some_image.jpg fake_image.png

  1. Start a Claude Code session
  2. Paste or reference the fake PNG image in the conversation
  3. Session crashes with "Image does not match the provided media type" error

Claude Model

Opus

Is this a regression?

I don't know

Last Working Version

_No response_

Claude Code Version

2.0.61

Platform

Other

Operating System

Windows

Terminal/Shell

WSL (Windows Subsystem for Linux)

Additional Information

Suggested fix:
Read the first 12 bytes of the image file and detect MIME type from magic bytes before sending to the API.

Workaround:
Rename the file to match its actual content type before sending:
```bash
file image.png # Check actual type
mv image.png image.jpg # Rename if needed

View original on GitHub ↗

This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗