Skip to content

Fixes #3361 - #3362

Closed
Yeachan-Heo wants to merge 1 commit into
mainfrom
fix/issue-3361-d19w0255
Closed

Yeachan-Heo wants to merge 1 commit into
mainfrom
fix/issue-3361-d19w0255

Conversation

@Yeachan-Heo

Copy link
Copy Markdown
Contributor

Summary

Parallel tool calls in a single assistant message now have their results merged into one following user message, as required by the Anthropic Messages API.

Problem

When an assistant message contains multiple tool_use blocks (parallel tool calls), the convert_messages() function was emitting one user message per tool_result. The API requires all tool_results to arrive in the single message immediately following the assistant message, so the provider rejected requests with HTTP 400.

Example message sequence (before):

  • messages[1]: assistant [tool_use, tool_use]
  • messages[2]: user [tool_result] <- correct
  • messages[3]: user [tool_result] <- ERROR: one message too late

Solution

The convert_messages() function now uses fold() with absorb_tool_result_message() to merge consecutive tool-result-only messages. Both implementations (rusty-claude-cli and tools crate) are updated.

Example message sequence (after):

  • messages[1]: assistant [tool_use, tool_use]
  • messages[2]: user [tool_result, tool_result] <- both in same message ✓

Testing

A new regression test (parallel_tool_result_pairing.rs) exercises the parallel tool call scenario against the mock Anthropic service and verifies the pairing contract.

Merge tool_result messages that follow an assistant message with multiple
tool_use blocks, as required by the Anthropic Messages API contract.

The API requires every tool_use block of an assistant message to be answered
by tool_result blocks in the single message that immediately follows it.
The prior implementation emitted one message per tool_result, so when an
assistant turn issued multiple tool calls, the second and later results
arrived one message too late and triggered HTTP 400.

Convert_messages now uses fold() with absorb_tool_result_message to merge
runs of adjacent tool-result-only messages, preserving the role and content
of any message that follows a tool result.

Fixes #3361
@Yeachan-Heo
Yeachan-Heo deleted the fix/issue-3361-d19w0255 branch October 19, 2026 03:56
@Yeachan-Heo

Copy link
Copy Markdown
Contributor Author

Closing as superseded: the parallel tool-result pairing fix for #3361 already landed on main in b8f93af (from #3360), with the regression test rust/crates/rusty-claude-cli/tests/parallel_tool_result_pairing.rs, and #3361 is closed by that commit. This branch would duplicate it.

—
[repo owner's gaebal-gajae (clawdbot) 🦞]

@XinwangTao

Copy link
Copy Markdown

这个修复很有必要:并行 tool_use 场景下 tool_result 必须全部放在紧随 assistant 消息之后的那条 user message 里,之前逐条拆分会被 Anthropic API 以 HTTP 400 拒绝。用 fold + absorb_tool_result_message 合并两个实现并补了回归测试,覆盖到位。

1 similar comment
@XinwangTao

Copy link
Copy Markdown

这个修复很有必要:并行 tool_use 场景下 tool_result 必须全部放在紧随 assistant 消息之后的那条 user message 里,之前逐条拆分会被 Anthropic API 以 HTTP 400 拒绝。用 fold + absorb_tool_result_message 合并两个实现并补了回归测试,覆盖到位。

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.

2 participants