Read tool: invalid PDF crashes entire session with API 400 error (unrecoverable)

Status Fixed / completed
Maintainer reply None cached
Activity 3 comments · opened Feb 12, 2026 · closed Feb 14, 2026

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

  1. Create a file with .pdf extension but invalid content:

``bash
echo "%PDF-1.4 not really a pdf" > /tmp/fake.pdf
``

  1. Use the Read tool to read it:

``
Read /tmp/fake.pdf
``

  1. The Read succeeds and the invalid PDF data enters the conversation context
  2. Every subsequent message now fails with API Error 400
  3. Session is dead — only /clear fixes 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 pdfinfo or 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

View original on GitHub ↗

3 Comments

github-actions[bot] · 8 months ago

Found 3 possible duplicate issues:

  1. https://github.com/anthropics/claude-code/issues/19577
  2. https://github.com/anthropics/claude-code/issues/25202
  3. https://github.com/anthropics/claude-code/issues/24387

This issue will be automatically closed as a duplicate in 3 days.

  • If your issue is a duplicate, please close it and 👍 the existing issue instead
  • To prevent auto-closure, add a comment or 👎 this comment

🤖 Generated with Claude Code

NickSdot · 8 months ago

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

github-actions[bot] · 7 months ago

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.