- Notifications
You must be signed in to change notification settings - Fork2.6k
Fix extended thinking bug by reordering tool messages#1798
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Conversation
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.
💡 Codex Review
Here are some automated review suggestions for this pull request.
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
elifrole=="tool": | ||
# Skip - these will be handled as part of tool pairs above | ||
used_indices.add(i) | ||
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.
Preserve unmatched tool_result messages
In_fix_tool_message_ordering()
therole == "tool"
branch simply marks the index as used and skips emitting the message. When a tool message has no matchingtool_call_id
or the corresponding tool call is absent from the trimmed history, this logic drops the tool result entirely. Previously those messages were forwarded to the provider; now they silently disappear, which removes tool outputs from the conversation and can change agent behaviour. Consider emitting unmatched tool messages (or raising) rather than discarding them.
Useful? React with 👍 / 👎.
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.
Thanks!
46d0d2f
intoopenai:mainUh oh!
There was an error while loading.Please reload this page.
Resolves#1797
This fixes the issue where conversation histories with extended thinking could have tool_result messages appearing before their corresponding tool_use messages, causing Anthropic API rejections.
What was happening:
The fix:
_fix_tool_message_ordering()
method that only activates whenpreserve_thinking_blocks=True
Repro script available in the original issue.