Description
The @ai-sdk/anthropic adapter (v3.0.67) produces Anthropic API payloads where tool_use blocks have no matching tool_result in the following message, causing the Anthropic API to reject the request with:
messages.57: `tool_use` ids were found without `tool_result` blocks immediately after: toolu_01R4gLPydCvYg7gc5G9rCkKp
Root Cause
The issue occurs when an assistant message contains a tool call in output-error state (a tool that was invoked but failed/was interrupted). The convertToModelMessages function correctly produces both tool-call and tool-result parts at the ModelMessage level — verified with 0 orphaned tool-calls across 63 model messages. However, the @ai-sdk/anthropic adapter's internal conversion from LanguageModelPrompt to the Anthropic API JSON format drops or misplaces the tool_result for the errored tool.
Reproduction
The failing message has this structure at the ModelMessage level:
- Assistant message:
[text, tool-call(output-error, id=A), text, tool-call(output-available, id=B)]
- Tool message:
[tool-result(id=A, error-text), tool-result(id=B, text)]
Both tool-calls have matching tool-results. After the adapter converts this to Anthropic API format, the API reports that tool_use id=A has no matching tool_result.
The error tool part data:
{
"type": "tool",
"callID": "toolu_01R4gLPydCvYg7gc5G9rCkKp",
"tool": "edit",
"state": {
"status": "error",
"input": {},
"error": "Tool execution aborted",
"time": { "start": 1775707868534, "end": 1775707868534 }
}
}
This produces output-error state with errorText: "Tool execution aborted" and input: {} (empty object). The createToolModelOutput correctly returns { type: "error-text", value: "Tool execution aborted" }.
Key diagnostic evidence
convertToModelMessages produces 63 ModelMessages with 0 orphaned tool-calls (verified by collecting all tool-result toolCallIds and checking every tool-call has a match)
ProviderTransform.message() (OpenCode's normalization layer) preserves all 28 tool messages — none removed
- The Anthropic API consistently rejects at
messages.57 with the same orphaned tool_use id
- The error reproduces on every retry because the conversation history is rebuilt identically from the database
Session-poisoning effect
Once this error occurs, the session is permanently stuck:
- Every turn rebuilds the same message history from the database
- The adapter produces the same broken API payload
- The API rejects with 400 (not retryable)
- The error is set on the NEW assistant message, not the historical one
- The historical message has no error flag, so it's never skipped
This is a widespread issue — see also anthropics/claude-code#6836.
Environment
ai: 5.0.124
@ai-sdk/anthropic: 3.0.67
- API endpoint: Azure Anthropic (delphos-general-github-foundry.services.ai.azure.com)
- Model: claude-opus-4-6
Description
The
@ai-sdk/anthropicadapter (v3.0.67) produces Anthropic API payloads wheretool_useblocks have no matchingtool_resultin the following message, causing the Anthropic API to reject the request with:Root Cause
The issue occurs when an assistant message contains a tool call in
output-errorstate (a tool that was invoked but failed/was interrupted). TheconvertToModelMessagesfunction correctly produces bothtool-callandtool-resultparts at the ModelMessage level — verified with 0 orphaned tool-calls across 63 model messages. However, the@ai-sdk/anthropicadapter's internal conversion from LanguageModelPrompt to the Anthropic API JSON format drops or misplaces thetool_resultfor the errored tool.Reproduction
The failing message has this structure at the ModelMessage level:
[text, tool-call(output-error, id=A), text, tool-call(output-available, id=B)][tool-result(id=A, error-text), tool-result(id=B, text)]Both tool-calls have matching tool-results. After the adapter converts this to Anthropic API format, the API reports that
tool_useid=A has no matchingtool_result.The error tool part data:
{ "type": "tool", "callID": "toolu_01R4gLPydCvYg7gc5G9rCkKp", "tool": "edit", "state": { "status": "error", "input": {}, "error": "Tool execution aborted", "time": { "start": 1775707868534, "end": 1775707868534 } } }This produces
output-errorstate witherrorText: "Tool execution aborted"andinput: {}(empty object). ThecreateToolModelOutputcorrectly returns{ type: "error-text", value: "Tool execution aborted" }.Key diagnostic evidence
convertToModelMessagesproduces 63 ModelMessages with 0 orphaned tool-calls (verified by collecting alltool-resulttoolCallIds and checking everytool-callhas a match)ProviderTransform.message()(OpenCode's normalization layer) preserves all 28 tool messages — none removedmessages.57with the same orphaned tool_use idSession-poisoning effect
Once this error occurs, the session is permanently stuck:
This is a widespread issue — see also anthropics/claude-code#6836.
Environment
ai: 5.0.124@ai-sdk/anthropic: 3.0.67