Skip to content

fix(anthropic): handle encrypted_code_execution_result for multi-turn with web_fetch/web_search 20260209#13046

Merged
felixarntz merged 5 commits intovercel:mainfrom
sleitor:fix-13040
Mar 4, 2026
Merged

fix(anthropic): handle encrypted_code_execution_result for multi-turn with web_fetch/web_search 20260209#13046
felixarntz merged 5 commits intovercel:mainfrom
sleitor:fix-13040

Conversation

@sleitor
Copy link
Copy Markdown
Contributor

@sleitor sleitor commented Mar 4, 2026

Problem

When using web_fetch_20260209 or web_search_20260209, the Anthropic API auto-injects a code_execution server_tool_use block in the response and returns its result as an encrypted_code_execution_result. The SDK previously only handled code_execution_result and code_execution_tool_result_error types, so the encrypted result was silently dropped.

In multi-turn conversations, the absence of this code_execution_tool_result in the subsequent user message caused the Anthropic API to return:

messages.1: `code_execution` tool use with id `srvtoolu_01HopAaXMHo9To3RavUYwqy7` was found without a corresponding `code_execution_tool_result` block

Repro: use the example pages in examples/ai-e2e-next:

  • /chat/anthropic-web-fetch-20260209
  • /chat/anthropic-web-search-20260209

Then 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): when code_execution_tool_result.content.type === 'encrypted_code_execution_result', emit a tool-result event carrying the encrypted payload so it is preserved in the conversation history
  • convert-to-anthropic-messages-prompt.ts: when reconstructing the user message for multi-turn, detect encrypted_code_execution_result output and emit the correct code_execution_tool_result block with the encrypted content (checked before the existing code_execution_result branch)
  • convert-to-anthropic-messages-prompt.test.ts: add a test covering the encrypted result round-trip in a multi-turn context

… 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
@tigent tigent bot added ai/provider related to a provider package. Must be assigned together with at least one `provider/*` label bug Something isn't working as documented provider/anthropic Issues related to the @ai-sdk/anthropic provider reproduction provided labels Mar 4, 2026
Copy link
Copy Markdown
Collaborator

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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!

@felixarntz
Copy link
Copy Markdown
Collaborator

One new problem that emerges from these changes is that so far code_execution_20250825 and code_execution_20260120 have had the same schema, but that is no longer the case. We assume that in several places in the codebase though, so it leads to incompatibility issues. Looking into it

@sleitor
Copy link
Copy Markdown
Contributor Author

sleitor commented Mar 4, 2026

Thank you for the approval and for looking into the schema incompatibility, @felixarntz! Agreed — once code_execution_20250825 and code_execution_20260120 diverge in schema (e.g. the new encrypted_code_execution_result block type), any code that assumes they share the same shape needs to be updated.

Happy to help with that fix too if useful — just let me know what you find and I can take a pass.

@felixarntz felixarntz enabled auto-merge (squash) March 4, 2026 17:40
@felixarntz
Copy link
Copy Markdown
Collaborator

@sleitor Thanks again! I just resolved it in ce7b366, should be good to go now.

@felixarntz felixarntz merged commit 7531e72 into vercel:main Mar 4, 2026
18 of 19 checks passed
@vercel-ai-sdk
Copy link
Copy Markdown
Contributor

vercel-ai-sdk bot commented Mar 4, 2026

🚀 Published in:

Package Version
@ai-sdk/amazon-bedrock 4.0.74
@ai-sdk/anthropic 3.0.55
@ai-sdk/google-vertex 4.0.74

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai/provider related to a provider package. Must be assigned together with at least one `provider/*` label bug Something isn't working as documented provider/anthropic Issues related to the @ai-sdk/anthropic provider reproduction provided

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Anthropic: fix multi-turn not functioning with new web_fetch and web_search tools

2 participants