fix(anthropic): handle encrypted_code_execution_result for multi-turn with web_fetch/web_search 20260209#13046
Conversation
… with web_fetch/web_search 20260209 When using web_fetch_20260209 or web_search_20260209, the Anthropic API auto-injects a code_execution server_tool_use block and returns its result as an encrypted_code_execution_result. The SDK previously only handled code_execution_result and code_execution_tool_result_error, so the encrypted result was silently dropped. In multi-turn conversations, the absence of this code_execution_tool_result caused the Anthropic API to return: messages.1: code_execution tool use with id srvtoolu_xxx was found without a corresponding code_execution_tool_result block Fix: - Handle encrypted_code_execution_result in both the streaming and non-streaming code_execution_tool_result processors (anthropic-messages- language-model.ts), emitting it as a tool-result with the encrypted payload - Handle encrypted_code_execution_result in convertToAnthropicMessagesPrompt (convert-to-anthropic-messages-prompt.ts), reconstructing the code_execution_tool_result block for the user message in multi-turn context - Add a test covering the encrypted result round-trip Fixes vercel#13040
felixarntz
left a comment
There was a problem hiding this comment.
@sleitor Thank you for jumping into this - your fix is on point!
I tested the examples and verified things work correctly now. The only additional tweaks needed were to actually support encrypted_code_execution_result in the type definition for the code_execution_20260120 tool. And then I added support to the UI in the example as well, so that these encrypted results are displayed.
Thanks again!
|
One new problem that emerges from these changes is that so far |
|
Thank you for the approval and for looking into the schema incompatibility, @felixarntz! Agreed — once Happy to help with that fix too if useful — just let me know what you find and I can take a pass. |
Problem
When using
web_fetch_20260209orweb_search_20260209, the Anthropic API auto-injects acode_executionserver_tool_useblock in the response and returns its result as anencrypted_code_execution_result. The SDK previously only handledcode_execution_resultandcode_execution_tool_result_errortypes, so the encrypted result was silently dropped.In multi-turn conversations, the absence of this
code_execution_tool_resultin the subsequent user message caused the Anthropic API to return:Repro: use the example pages in
examples/ai-e2e-next:/chat/anthropic-web-fetch-20260209/chat/anthropic-web-search-20260209Then send a follow-up message after a tool call was made in the previous response.
Fixes #13040
Fix
anthropic-messages-language-model.ts(both streaming and non-streaming paths): whencode_execution_tool_result.content.type === 'encrypted_code_execution_result', emit atool-resultevent carrying the encrypted payload so it is preserved in the conversation historyconvert-to-anthropic-messages-prompt.ts: when reconstructing the user message for multi-turn, detectencrypted_code_execution_resultoutput and emit the correctcode_execution_tool_resultblock with the encrypted content (checked before the existingcode_execution_resultbranch)convert-to-anthropic-messages-prompt.test.ts: add a test covering the encrypted result round-trip in a multi-turn context