fix(core): support PDF files in V2 read tool#38797
Open
Shalin-Shah-2002 wants to merge 2 commits into
Open
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue for this PR
Closes #37323
Type of change
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 throwsBinaryFileError("Cannot read binary file"). This is a regression from V1, whose read tool returned PDFs asdata:application/pdf;base64,...attachments that the provider API could consume.What changed:
packages/core/src/tool/read-filesystem.ts— Added a dedicated PDF ingest path that mirrors the image path: detect%PDFmagic bytes, enforce the existingMAX_MEDIA_INGEST_BYTES(20 MiB) cap, read all bytes, and returnFileSystem.Contentwithencoding: "base64"andmime: "application/pdf". The oldBinaryFileErrorrejection for%PDFwas replaced.packages/core/src/tool/read.ts— UpdatedtoModelOutputto emit PDF content ("PDF read successfully"+ToolFileContent) alongside the existing image path. Updated the base64 guard to allowapplication/pdfthrough (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:processor.ts) passes non-image attachments through untouchedmessage-v2.ts) already handles PDFs viaisMedia()andsupportsMediaInToolResult()application/pdfunsupportedParts) already gates PDFs for models without PDF capabilityHow did you verify your code works?
MediaIngestLimitError)Screenshots / recordings
N/A — core library change, no UI.
Checklist