fix(core): repair interleaved text/tool-call parts in assistant messages#14456
Open
bil9148 wants to merge 2 commits intoanomalyco:devfrom
Open
fix(core): repair interleaved text/tool-call parts in assistant messages#14456bil9148 wants to merge 2 commits intoanomalyco:devfrom
bil9148 wants to merge 2 commits intoanomalyco:devfrom
Conversation
The AI SDK's convertToModelMessages can produce assistant messages where text blocks are interleaved between tool-call blocks when multi-step conversations have text in between tool calls across steps. For example: [text, tool_use, tool_use, text, tool_use] Anthropic's API interprets the second text block as ending the tool-calling turn, causing the preceding tool_use blocks to appear orphaned from their tool_result blocks in the next message. This produces the error: tool_use ids were found without tool_result blocks immediately after This adds repairAssistantMessages() as a safety net in ProviderTransform.message() that: 1. Reorders content so all text/reasoning parts come before tool-call parts 2. Injects synthetic tool-result blocks for any genuinely orphaned tool-calls Fixes anomalyco#10616, anomalyco#8377, anomalyco#1662, anomalyco#2720, anomalyco#2214, anomalyco#5750
Contributor
|
The following comment was made by an LLM, it may be inaccurate: Based on my search, I found one potentially related PR: Related PR:
The other PRs found (#14393, #8888) appear less directly related as they focus on different aspects (thinking blocks and message conversion safeguards respectively). |
Contributor
|
Thanks for updating your PR! It now meets our contributing guidelines. 👍 |
|
|
||
| if (next?.role === "tool" && Array.isArray(next.content)) { | ||
| next.content = [...next.content, ...patches] | ||
| } else { |
Contributor
There was a problem hiding this comment.
We should try to avoid using the 'else' statements:
https://github.com/anomalyco/opencode/blob/dev/CONTRIBUTING.md#style-preferences
Could this be reworked?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue for this PR
Closes #10616, #8377, #1662, #2720, #2214, #5750
Related: #4802, #8312, #10673, #7002
Type of change
What does this PR do?
I kept hitting the
tool_use ids were found without tool_result blocks immediately aftererror in my sessions. Once it happens, every subsequent message fails too — the only workaround is reverting the last message or starting a new chat. Extremely disruptive.I dug into the actual API request payload from the logs and found that the tool_use/tool_result pairing was actually correct — all IDs matched. The real problem was the ordering of content parts within assistant messages.
The AI SDK's
convertToModelMessagesmerges multi-step conversations and can produce assistant messages like:That second
textblock sitting betweentool_useblocks causes Anthropic's API to think the tool-calling turn ended early, so it sees the first twotool_useIDs as orphaned from theirtool_resultblocks.The fix adds
repairAssistantMessages()inProviderTransform.message()that:It also logs a warning when it makes repairs so the issue is observable in
~/.local/share/opencode/log/.I verified the root cause by extracting
messages[29]from the error'srequestBodyValuesin the log file. The content parts were[text, tool_use, tool_use, text, tool_use]— the interleaving was clearly visible. After the fix, the same session works fine.How did you verify your code works?
Checklist