GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(core): repair interleaved text/tool-call parts in assistant messages by bil9148 · Pull Request #14456 · anomalyco/opencode

Conversation

@bil9148

@bil9148 bil9148 commented Feb 20, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

Issue for this PR

Closes #10616, #8377, #1662, #2720, #2214, #5750

Related: #4802, #8312, #10673, #7002

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

I kept hitting the tool_use ids were found without tool_result blocks immediately after error 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 convertToModelMessages merges multi-step conversations and can produce assistant messages like:

[text, tool_use, tool_use, text, tool_use]

That second text block sitting between tool_use blocks causes Anthropic's API to think the tool-calling turn ended early, so it sees the first two tool_use IDs as orphaned from their tool_result blocks.

The fix adds repairAssistantMessages() in ProviderTransform.message() that:

  1. Reorders assistant message content so all text/reasoning parts come before all tool-call parts
  2. As a secondary safety net, injects synthetic tool-result blocks for any genuinely missing tool-results (handles edge cases like process crashes mid-tool-execution)

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's requestBodyValues in 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?

  • Reproduced the error in my session, applied the fix, retried the exact same failing message — it worked immediately
  • 5 new test cases covering: interleaved reordering, orphaned tool-call injection, partial patching of existing tool messages, and no-op when messages are already correct
  • 2 existing tests updated to account for the new safety net
  • All 107 tests pass, typecheck clean

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bil9148
fix(core): repair interleaved text/tool-call parts in assistant messages
65d7e92
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 #10616, #8377, #1662, #2720, #2214, #5750
@github-actions github-actions Bot added the needs:compliance This means the issue will auto-close after 2 hours. label Feb 20, 2026
@github-actions

github-actions Bot commented Feb 20, 2026

Copy link
Copy Markdown
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:

  • fix: handle dangling tool_use blocks for LiteLLM proxy compatibility #8497 - "fix: handle dangling tool_use blocks for LiteLLM proxy compatibility"
    • This appears related as it also addresses orphaned/dangling tool_use blocks, though it may be specific to LiteLLM proxy compatibility. The current PR (fix(core): repair interleaved text/tool-call parts in assistant messages #14456) takes a broader approach by repairing interleaved text/tool-call parts in assistant messages as a general safety net.

The other PRs found (#14393, #8888) appear less directly related as they focus on different aspects (thinking blocks and message conversion safeguards respectively).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions github-actions Bot removed the needs:compliance This means the issue will auto-close after 2 hours. label Feb 20, 2026
@github-actions

github-actions Bot commented Feb 20, 2026

Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

alexyaroshuk
alexyaroshuk reviewed Feb 20, 2026
View reviewed changes
Comment thread packages/opencode/src/provider/transform.ts Outdated

if (next?.role === "tool" && Array.isArray(next.content)) {
next.content = [...next.content, ...patches]
} else {

@alexyaroshuk alexyaroshuk Feb 20, 2026

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

We should try to avoid using the 'else' statements:
https://github.com/anomalyco/opencode/blob/dev/CONTRIBUTING.md#style-preferences
Could this be reworked?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bil9148
style: remove else statement per contributing guidelines
f8287e6
@zephy358 zephy358 mentioned this pull request Feb 23, 2026
[Bug]: Prometheus(Claude model) consulting Momus mostly results in "tool_use ids were found without tool_result blocks" error code-yeongyu/oh-my-openagent#2070
Closed
4 tasks done
@github-actions github-actions Bot mentioned this pull request Feb 26, 2026
fix: prevent infinite retry loop from orphaned tool_use blocks #15152
Closed
@github-actions github-actions Bot mentioned this pull request Mar 9, 2026
fix(session): fix root causes and reconstruction of tool_use/tool_result mismatch (#16749) #16751
Closed
3 of 6 tasks
@bil9148 bil9148 closed this by deleting the head repository Apr 1, 2026
@supernovae supernovae mentioned this pull request Apr 24, 2026
Assistant messages in history replay are missing tool_calls field, breaking OpenAI-compatible providers #24090
Closed
@mrosnerr mrosnerr mentioned this pull request Apr 28, 2026
fix(messages-transform): isolate hook failures so tool-pair-validator always runs code-yeongyu/oh-my-openagent#3715
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

1 more reviewer
@alexyaroshuk alexyaroshuk alexyaroshuk left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

messages.87: tool_use ids were found without tool_result blocks immediately

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@bil9148 @alexyaroshuk
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.