[BUG] Read tool: non-image file passed as image causes cryptic API Error 400

Resolved 💬 4 comments Opened Mar 28, 2026 by 7blacky7 Closed May 19, 2026

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 a file has an image extension (e.g. .jpg) but actually contains JSON data
(e.g. from an API response saved with wrong extension), the Read tool sends it to
the API as an image. The API returns:

API Error: 400 {"type":"error","error":{"type":"invalid_request_error",
"message":"Could not process image"}}

This crashes both the main Claude Code session AND background subagents with no
recovery possible.

Expected behavior: Validate the file's magic bytes before sending to the API.
If it's not a real image, return a helpful error like "File is not a valid image
(detected: JSON text data)" so the agent can handle it gracefully.

Reproduction:

  1. Save a JSON API response with .jpg extension:

curl -s "https://api.example.com/data" -o /tmp/fake.jpg

  1. Use Read tool on /tmp/fake.jpg
  2. → API Error 400 "Could not process image"

Impact: In multi-agent setups with browser automation, screenshot APIs sometimes
return JSON instead of binary image data. This silently kills both the main session
and background agents with no way to recover.

What Should Happen?

Validate the file's magic bytes before sending to the API. If the file extension
suggests an image but the content is not (e.g. JSON, HTML, plain text), return a
clear error message like "File is not a valid image (detected: JSON text data)"
instead of forwarding it to the API. This allows agents to handle the error
gracefully instead of crashing.

Error Messages/Logs

API Error: 400 {"type":"error","error":{"type":"invalid_request_error","message":"Could not process
  image"},"request_id":"req_011CZV2Jf9vdyZpVUEm3WeAx"}

Steps to Reproduce

  1. Save a non-image response with an image extension:

curl -s "https://httpbin.org/json" -o /tmp/fake-image.jpg

  1. Verify it's not an image:

file /tmp/fake-image.jpg # → "JSON text data"

  1. In Claude Code, use the Read tool on /tmp/fake-image.jpg
  2. Result: API Error 400 "Could not process image"
  3. Session/subagent crashes with no recovery

Claude Model

Opus

Is this a regression?

No Idea.

Last Working Version

_No response_

Claude Code Version

2.1.86 (Claude Code)

Platform

Anthropic API

Operating System

Other Linux

Terminal/Shell

Other

Additional Information

_No response_

View original on GitHub ↗

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