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

Resolved 💬 3 comments Opened Feb 12, 2026 by arwoxb24 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 ↗

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