Skip to content

fix(core): support PDF files in V2 read tool#38797

Open
Shalin-Shah-2002 wants to merge 2 commits into
anomalyco:devfrom
Shalin-Shah-2002:fix/read-tool-pdf
Open

fix(core): support PDF files in V2 read tool#38797
Shalin-Shah-2002 wants to merge 2 commits into
anomalyco:devfrom
Shalin-Shah-2002:fix/read-tool-pdf

Conversation

@Shalin-Shah-2002

Copy link
Copy Markdown

Issue for this PR

Closes #37323

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

The V2 read tool (ReadToolFileSystem) rejects PDF files at its magic-byte check: when the first 4 bytes are %PDF, it throws BinaryFileError ("Cannot read binary file"). This is a regression from V1, whose read tool returned PDFs as data:application/pdf;base64,... attachments that the provider API could consume.

What changed:

  1. packages/core/src/tool/read-filesystem.ts — Added a dedicated PDF ingest path that mirrors the image path: detect %PDF magic bytes, enforce the existing MAX_MEDIA_INGEST_BYTES (20 MiB) cap, read all bytes, and return FileSystem.Content with encoding: "base64" and mime: "application/pdf". The old BinaryFileError rejection for %PDF was replaced.

  2. packages/core/src/tool/read.ts — Updated toModelOutput to emit PDF content ("PDF read successfully" + ToolFileContent) alongside the existing image path. Updated the base64 guard to allow application/pdf through (PDFs skip image normalization since they can't be resized). Updated the tool description to mention PDF support.

Why it works:

The rest of the pipeline already handled application/pdf:

  • Settlement (processor.ts) passes non-image attachments through untouched
  • Message conversion (message-v2.ts) already handles PDFs via isMedia() and supportsMediaInToolResult()
  • Provider lowering (Responses API, Bedrock) already supports application/pdf
  • Model capability gating (unsupportedParts) already gates PDFs for models without PDF capability

How did you verify your code works?

  • Added 2 tests covering the real-filesystem PDF ingest path (small PDF returns base64, oversized PDF returns MediaIngestLimitError)
  • All 26 existing read tool tests pass (no regressions)
  • All 30 packages pass typecheck via turborepo

Screenshots / recordings

N/A — core library change, no UI.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Shalin-Shah-2002 and others added 2 commits July 24, 2026 23:11
The V2 ReadToolFileSystem rejected PDFs at the magic-byte check,
throwing BinaryFileError ('Cannot read binary file'). This was a
regression from V1, which returned PDFs as base64 attachments.

- Added a dedicated PDF ingest path in read-filesystem.ts that
  mirrors the image path: detect %PDF magic bytes, enforce the
  existing MAX_MEDIA_INGEST_BYTES (20 MiB) cap, read all bytes,
  and return FileSystem.Content with encoding: 'base64' and
  mime: 'application/pdf'
- Updated read.ts toModelOutput to emit PDF content alongside
  the existing image path ('PDF read successfully' + ToolFileContent)
- Updated read.ts base64 guard to allow application/pdf through
  (PDFs skip image normalization since they can't be resized)

The rest of the pipeline (settlement, message-v2 media extraction,
provider lowering, and unsupportedParts gating) already handled
application/pdf and required no changes.

Closes anomalyco#37323

Co-Authored-By: Claude <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

V2 read tool does not support PDFs

1 participant