Read tool: invalid PDF crashes entire session with API 400 error (unrecoverable)
Bug Description
When the Read tool reads a file with .pdf extension that is not a structurally valid PDF (e.g., an HTML file renamed to .pdf, or a file with %PDF- magic bytes but no valid PDF structure), the invalid data gets embedded in the conversation context.
After this happens, every subsequent message from the user returns:
API Error: 400 {"type":"error","error":{"type":"invalid_request_error","message":"messages.28.content.0.pdf.source.base64.data: The PDF specified was not valid."}}
The session becomes completely unrecoverable — the only fix is /clear which destroys all conversation context.
Steps to Reproduce
- Create a file with
.pdfextension but invalid content:
``bash``
echo "%PDF-1.4 not really a pdf" > /tmp/fake.pdf
- Use the
Readtool to read it:
````
Read /tmp/fake.pdf
- The Read succeeds and the invalid PDF data enters the conversation context
- Every subsequent message now fails with API Error 400
- Session is dead — only
/clearfixes it
Expected Behavior
Claude Code should validate PDF files before sending them to the API:
- Check magic bytes (
%PDF-) - Validate PDF structure (e.g., using
pdfinfoor similar) - If invalid → return an error message to the agent instead of embedding bad data in context
Impact
- Session destruction: entire conversation context is lost
- Token waste: user loses all accumulated context (potentially millions of tokens)
- No warning: the crash is silent until the next message
- Cascading: if the agent retries or the user sends messages, each attempt generates a billable API call that immediately fails
Workaround
We implemented a PreToolUse hook (pdf-safety-guard.sh) that validates PDFs before the Read tool executes, using magic bytes + pdfinfo structural validation. This prevents the issue but it should be fixed at the Claude Code level.
Environment
- Claude Code CLI
- Linux (Ubuntu)
- Model: Claude Opus 4.6
3 Comments
Found 3 possible duplicate issues:
This issue will be automatically closed as a duplicate in 3 days.
🤖 Generated with Claude Code
Adding:
this also occasionally happens when pasting a macOS clipboard screenshot to CC in the Desktop app (which shares the same storage files).
Related:
https://github.com/anthropics/claude-code/issues/24387
This issue has been automatically locked since it was closed and has not had any activity for 7 days. If you're experiencing a similar issue, please file a new issue and reference this one if it's relevant.