Skip to content

fix(anthropic): ensure reasoning blocks precede tool_use in assistant messages#10474

Closed
ryan-mt wants to merge 1 commit into
anomalyco:devfrom
ryan-mt:fix/anthropic-thinking-block-order
Closed

fix(anthropic): ensure reasoning blocks precede tool_use in assistant messages#10474
ryan-mt wants to merge 1 commit into
anomalyco:devfrom
ryan-mt:fix/anthropic-thinking-block-order

Conversation

@ryan-mt
Copy link
Copy Markdown
Contributor

@ryan-mt ryan-mt commented Jan 25, 2026

Fixes #9364

Problem

When using Claude models with Extended Thinking enabled, multi-turn conversations with tool use fail with:

messages.X.content.0.type: Expected 'thinking' or 'redacted_thinking', but found 'tool_use'

Root Cause

The normalizeMessages function filters empty content but does not reorder parts to ensure reasoning blocks come before tool-call blocks in assistant messages.

Solution

Added sorting logic for assistant messages in the Anthropic-specific block. Parts are now ordered by priority:

  • reasoning: 0 (first)
  • text: 1
  • tool-call: 2
  • everything else: 3

This ensures compliance with Anthropic's Extended Thinking API requirements.

References

… messages

Fixes #9364

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@github-actions
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:

This could be relevant context for understanding how thinking blocks are handled across the codebase, though it may not be a direct duplicate since your PR specifically addresses the ordering issue in normalizeMessages for Anthropic's API requirements.

@rekram1-node
Copy link
Copy Markdown
Collaborator

When did you encounter this issue?

Can you share the session?

Run opencode export > session.json in your TERMINAL (not the tui) and then send session export of it

@ryan-mt
Copy link
Copy Markdown
Contributor Author

ryan-mt commented Jan 25, 2026

I encountered this problem this afternoon and it hasn't been fixed yet. I'm trying to figure it out, but it doesn't seem to be working. Because I don't know where the problem is coming from.

@ryan-mt
Copy link
Copy Markdown
Contributor Author

ryan-mt commented Jan 25, 2026

session.json

@rekram1-node
Copy link
Copy Markdown
Collaborator

The provided session has no such errors?

@ryan-mt
Copy link
Copy Markdown
Contributor Author

ryan-mt commented Jan 25, 2026

I don't know, but I've been encountering it constantly in recent chat messages.

@ryan-mt
Copy link
Copy Markdown
Contributor Author

ryan-mt commented Jan 25, 2026

session.json

@rekram1-node
Copy link
Copy Markdown
Collaborator

Sorry if I wasn't clear, can you export the session in which you encountered this error? Because the sessions you are linking aren't relevant

@ryan-mt
Copy link
Copy Markdown
Contributor Author

ryan-mt commented Jan 25, 2026

I've fixed it now, it was due to plugins.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Claude Extended Thinking: assistant message content order causes API error

2 participants