Let `read_file` return images and PDFs, and describe other binary files
FileSystem's read_file answered every binary file with [Binary file: N bytes. Use a binary-aware tool to inspect.], naming a tool that does not exist, so an agent could not look at an image, a PDF, or a binary payload another capability wrote to the workspace.
Now:
- Images (PNG, JPEG, GIF, WebP) and PDFs up to 5 MB come back as the header plus a
BinaryContent. Core then places the file inside the tool result or on the user channel, depending on the model. The type is read from the file's leading bytes, not its extension, so a misnamed file is not sent under the wrong media type. A PDF is often not caught by the null-byte check and used to be decoded as text full of replacement characters; it is now recognized by its signature. - Other binary files, and images and PDFs over 5 MB, are described: size, media type (guessed from the extension), and for non-media the first 64 bytes in hex.
Images and PDFs are what most models with file input take, and the set OpenCode's read tool returns. Audio, video and office documents would end the run on Anthropic and OpenAI Responses, because core's message mapping raises for them. A model that can't take an image or PDF still fails the same way (an image without vision, a PDF on Groq or Hugging Face). #10472 tracks the core fix: profile flags for file input, and degrading a tool-returned file the model can't take into a note per request, with FileUnderstanding (#8453) describing it when configured. read_file widens to every format once that is in.
The 5 MB cap matches PlaywrightBrowser's screenshot cap (the strictest mainstream per-image limit). It is a module constant rather than a new option; it can become an option if someone needs a different value.
The repo's own gh-aw shim (.github/scripts/pydantic_ai_gh_aw_shim/read.py) calls read_file directly. It now passes an image or PDF result through unchanged, and has a test for it.
This also unblocks dropping ToolOutputLimits' read_tool_result for binary spills once read_file covers them (#9410).
[!WARNING]
Compatibility impact: FileSystemToolset.read_file (the direct-call method) and the read_file tool now return str | list[str | BinaryContent] instead of str. Code that calls read_file directly and treats the result as a string gets a list for images and PDFs. The text returned for other binary files has also changed.
Why this can ship in a minor release: Harness is on 0.x, and the old binary result was a placeholder with nothing to act on.
Migration: Check isinstance(result, str) before treating a direct read_file result as text.
- Closes #9591
Checklist
- No breaking changes in accordance with the version policy.
- Any permitted compatibility impact has a label, warning, migration, release note, and exact API-check waiver.
- PR title is fit for the release changelog.
🤖 Generated with Claude Code