Skip to content

Conversation

@lingjiekong
Copy link

Fix message alternation bug where tool results were appended before their corresponding tool_use blocks, violating Claude API's required user/assistant alternation pattern.

Changes:

  • Move assistant_content initialization inside the runner loop to create fresh content for each iteration
  • Append assistant message containing tool_use BEFORE appending the tool_result message
  • This ensures proper message order: USER → ASSISTANT (tool_use) → USER (tool_result) → ASSISTANT (response)

Fixes BadRequestError: "unexpected tool_use_id found in tool_result blocks: Each tool_result block must have a corresponding tool_use block in the previous message."

🤖 Generated with Claude Code

Fix message alternation bug where tool results were appended before
their corresponding tool_use blocks, violating Claude API's required
user/assistant alternation pattern.

Changes:
- Move assistant_content initialization inside the runner loop to create
  fresh content for each iteration
- Append assistant message containing tool_use BEFORE appending the
  tool_result message
- This ensures proper message order: USER → ASSISTANT (tool_use) →
  USER (tool_result) → ASSISTANT (response)

Fixes BadRequestError: "unexpected tool_use_id found in tool_result
blocks: Each tool_result block must have a corresponding tool_use
block in the previous message."

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
@lingjiekong lingjiekong requested a review from a team as a code owner October 27, 2025 13:17
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.

1 participant