Skip to content

feat: convert Anthropic tool result media like user files - #73

Merged
ethanndickson merged 3 commits into
coder_2_33from
codagt-1430-tool-media-core
Nov 3, 2026
Merged

ethanndickson merged 3 commits into
coder_2_33from
codagt-1430-tool-media-core

Conversation

@ethanndickson

@ethanndickson ethanndickson commented Oct 26, 2026 •

Copy link
Copy Markdown
Member

Tool result media had no filename, and each provider converted it with its own code rather than the code it already uses for user file attachments. Those copies drifted apart. On Anthropic, tool media always became an image block, so a PDF from an MCP tool got a 400, which is why coder/coder currently swaps PDF tool results for a note before they reach the provider.

This adds an optional Filename to ToolResultOutputContentMedia and ToolResponse, and an internal filepart.File type that holds either a user file's bytes or tool media's base64, so a provider can run tool media through its user file converter without decoding it first. Stored results without a filename still decode as before.

On Anthropic, we'll now do just that. A PDF becomes a document block inside the tool_result, titled with the filename, text/* becomes a text document, and images stay images. The nested blocks don't get cache_control, as the breakpoint stays on the tool_result and Anthropic only allows four. Anything we can't convert becomes a short note saying the content can't be displayed, plus upstream's warning, rather than an invalid block.

I sent a PDF tool result to Claude Haiku through our AI gateway with this stack, and it read the PDF fine. I've since run it through chatd on a local coderd, where PDF and PNG results from an MCP server (and from a workspace agent's MCP server) reached Claude as document and image blocks. That included a turn where Claude thought before calling the tools, so the signed thinking block was replayed next to the PDF tool_result, and Anthropic accepted it. #74 does the same for OpenAI.

Part of CODAGT-1430

@linear-code

linear-code Bot commented Oct 26, 2026

Copy link
Copy Markdown

CODAGT-1430

@ethanndickson

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-28T01:02:10.039887Z 0a6f9c5 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: e7fe298736

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@ethanndickson
ethanndickson marked this pull request as ready for review October 26, 2026 03:21
Base automatically changed from codagt-1430-upstream-sync to coder_2_33 October 26, 2026 05:21
@ethanndickson
ethanndickson force-pushed the codagt-1430-tool-media-core branch from e7fe298 to 574b1ff Compare October 26, 2026 05:21
@ethanndickson

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: 3e303b90b1

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

ToolResultOutputContentMedia and ToolResponse gain an optional Filename,
and the agent copies it into the media tool result. FilePart() decodes the
base64 data so providers can convert tool-result media with the same rules
they use for user files.
Tool-result media always became an image block, so a PDF from a tool got a
400 from the API. Tool media now goes through the same file converter as
user files: a PDF becomes a document block titled with the filename, text
becomes a text document, and images stay images. Blocks inside tool_result
get no cache_control; the breakpoint stays on tool_result.

Media that cannot be converted becomes a text note plus upstream's
warning, "tool result media type %s not supported, sending text
placeholder only". The note uses "file" when the media type is empty.
Tool-result media data is already base64, but FilePart() decoded it and
the Anthropic converter encoded it again. For a 4 MiB image this costs
about 4 ms and 15 MB of extra allocation per request.

providers/internal/filepart.File now keeps the content in the encoding
of its source: raw bytes for user file parts, base64 for tool-result
media. Images and PDFs send the base64 unchanged. Only text documents
decode it. FilePart() is removed.

Invalid base64 in an image or PDF now goes to the API unchanged, the
same as upstream. Invalid base64 in text media still becomes a text
notice and a warning.
@ethanndickson
ethanndickson force-pushed the codagt-1430-tool-media-core branch from 3e303b9 to 0a6f9c5 Compare October 28, 2026 01:00
@ethanndickson

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: 0a6f9c5099

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@ethanndickson
ethanndickson merged commit 6daa4f4 into coder_2_33 Nov 3, 2026
6 checks passed
ethanndickson added a commit to coder/coder that referenced this pull request Nov 10, 2026
Part of [CODAGT-1430](https://linear.app/codercom/issue/CODAGT-1430).
This is the first of four stacked PRs.

Today a PDF in an MCP tool result reaches no provider. On Anthropic we
strip it in chatd. On OpenAI Chat Completions fantasy swaps it for a
"cannot be displayed" note, and on Responses it sends a placeholder that
points at a user message it never sends.

coder/fantasy#73 and coder/fantasy#74 make fantasy convert tool result
media with the converter it already uses for user files: an Anthropic
document inside `tool_result`, an `input_file` inside
`function_call_output` on Responses, and a user file part straight after
the tool message on Chat Completions. Both have merged, and this PR pins
`charm.land/fantasy` to the coder_2_33 tip that has them. Chat
Completions file parts now refuse text files, as Responses already did,
so `AcceptsFilePartMediaType` follows.

The converters take a filename, so an MCP embedded resource blob is now
named after the last element of its URI path, and we store the name with
the tool result so later turns send the same one. OpenAI picks the file
type from the extension, so we append the right one when it's missing or
wrong. We drop names with control characters, as Postgres `jsonb`
rejects NUL, and we redact the name like the rest of the tool response,
since a URI like `https://host/sse/<token>` would otherwise put the
token in the chat history.

There's also a test proving aibridge's Chat Completions re-encode keeps
a user file part, as that's where these PDFs end up.

Anthropic still strips tool result PDFs until the next PR in the stack.
ethanndickson added a commit to coder/coder that referenced this pull request Nov 10, 2026
Closes CODAGT-1425

Stacked on #31386.

coder/fantasy#78, which is already on `main`, fixed the searches with no
reasoning before them by sending them inline without their `id`. Stored
chats still send a search as an `item_reference` when a reasoning item
came right before it, though. The API pairs any stored ID with that
reasoning item and rejects the whole request if it's missing, and a
reference doesn't save any tokens over an inline item anyway.

This bumps fantasy to pick up coder/fantasy#77, which just never
references a search, so stored chats now replay earlier searches inline,
like unstored ones. It also drops the `id` from replayed computer calls,
which have the same problem.

I've had to stack this on the CODAGT-1430 PRs, as coder/fantasy#77
merged after coder/fantasy#73 and coder/fantasy#74, so any pin that has
it has them too. `TestOpenAIResponsesNoStaleWebSearchReplay` now covers
a stored chat, and I deleted
`TestOpenAIResponsesFullReplayPairsReasoningAndWebSearch`, as it
asserted the order of references we no longer send.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant