Skip to content

Conversation

habema
Copy link
Contributor

@habema habema commented Sep 24, 2025

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:

  • Reasoning blocks between tool calls caused the converter to create malformed message sequences
  • Multiple consecutive assistant messages with tool calls weren't getting paired with their results properly
  • Anthropic API requires strict tool_use → tool_result ordering

The fix:

  • Added _fix_tool_message_ordering() method that only activates when preserve_thinking_blocks=True
  • Analyzes the entire conversation to identify tool call/result pairs
  • Reconstructs messages ensuring proper tool_use → tool_result sequencing
  • Handles edge cases like consecutive tool calls and misplaced results
  • Non-breaking - only affects extended thinking scenarios

Repro script available in the original issue.

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a 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 👍.

Comment on lines 482 to 485
elif role == "tool":
# Skip - these will be handled as part of tool pairs above
used_indices.add(i)

Choose a reason for hiding this comment

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

P1 Badge Preserve unmatched tool_result messages

In _fix_tool_message_ordering() the role == "tool" branch simply marks the index as used and skips emitting the message. When a tool message has no matching tool_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 👍 / 👎.

Copy link
Member

@seratch seratch left a comment

Choose a reason for hiding this comment

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

Thanks!

@seratch seratch merged commit 46d0d2f into openai:main Sep 24, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Anthropic Extended Thinking bug: tool_result blocks appear before corresponding tool_use blocks
2 participants