fix: handle binary files in read tool (images/PDFs)#1638
Open
vihaan-kk wants to merge 2 commits into
Open
Conversation
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adds binary-aware handling to the read tool so it can return base64-encoded images/PDFs instead of attempting to decode them as text.
Changes:
- Introduces a binary media type lookup (
_BINARY_MEDIA_TYPES+_binary_media_type). - Updates
readto return base64 “image”/“document” blocks for supported binary types. - Changes
readreturn annotation fromstrtoAny.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+531
to
536
| media = _binary_media_type(target) | ||
| if media is not None: | ||
| data = base64.standard_b64encode(target.read_bytes()).decode("ascii") | ||
| kind = "document" if media == "application/pdf" else "image" | ||
| return [{"type": kind, "source": {"type": "base64", "media_type": media, "data": data}}] | ||
| text = target.read_text() |
Author
There was a problem hiding this comment.
Good point, noted this in the PR description as a follow-up. A separate lower size cap for base64 responses makes sense but felt out of scope for this fix.
Comment on lines
528
to
+535
| f"read: {file_path} is {st.st_size} bytes, exceeds {limit}-byte limit. " | ||
| "Use bash (head/tail/sed) to read a slice." | ||
| ) | ||
| media = _binary_media_type(target) | ||
| if media is not None: | ||
| data = base64.standard_b64encode(target.read_bytes()).decode("ascii") | ||
| kind = "document" if media == "application/pdf" else "image" | ||
| return [{"type": kind, "source": {"type": "base64", "media_type": media, "data": data}}] |
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.
Fixes #1637
Changes
The
beta_read_toolinagent_toolset.pycalledtarget.read_text()on every file, which decodes bytes as UTF-8. Reading a binary file (image or PDF) raises an uncaughtUnicodeDecodeErrorsince onlyToolErrorandOSErrorwere caught,UnicodeDecodeErroris aValueErrorand propagated uncaught to the model as a raw tool error.Fix
Added a
_binary_media_typehelper that detects binary files by extension. When a binary file is detected,readreturns a base64-encodedimageordocumentcontent block instead of attempting text decoding. The existing text path is unchanged.Supported types:
.jpg,.jpeg,.png,.gif,.webp→imageblock.pdf→documentblockTesting
3.9; 3253 on Python 3.14)
./scripts/lintpasses clean (pyright, mypy, ruff)./scripts/formatappliedNotes
view_rangeslicing logic is text-only and is correctly bypassed for binary files since the binary path returns earlyreadwas updated fromstrtoAnyto reflect that binary files return a list content block