Skip to content

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

Description

@sa9saQ

Preflight Checklist

  • I have searched existing issues and this hasn't been reported yet
  • This is a single bug report (please file separate reports for different bugs)
  • 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):
    # Take a JPEG screenshot and save as .png, or:
    cp some_image.jpg fake_image.png
  2. Start a Claude Code session
  3. Paste or reference the fake PNG image in the conversation
  4. 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:

file image.png  # Check actual type
mv image.png image.jpg  # Rename if needed

Metadata

Metadata

Assignees

No one assigned

    Labels

    duplicateThis issue or pull request already exists

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions