Skip to content

fix(anthropic): split parallel tool calls into alternating messages - #2090

Merged
oss-maintainer merged 2 commits into
agentscope-ai:mainfrom
jujn:model/fix-anthropic-parallel-tool-calls
Jul 10, 2026
Merged

oss-maintainer merged 2 commits into
agentscope-ai:mainfrom
jujn:model/fix-anthropic-parallel-tool-calls

Conversation

@jujn

@jujn jujn commented Jul 9, 2026

Copy link
Copy Markdown
Collaborator

Description

Close #2077

Summary

Fixes Anthropic message formatting for parallel tool calls.

When a single assistant message contains multiple ToolUseBlocks and the following messages contain the corresponding ToolResultBlocks, Anthropic requires the request payload to alternate as:

assistant(tool_use_1) -> user(tool_result_1) -> assistant(tool_use_2) -> user(tool_result_2)

This PR updates AnthropicMessageConverter to detect completed parallel tool-call batches and expand them into Anthropic-compatible alternating messages before sending the request.

Checklist

Please check the following items before code is ready to be reviewed.

  • Code has been formatted with mvn spotless:apply
  • All tests are passing (mvn test)
  • Javadoc comments are complete and follow project conventions
  • Related documentation has been updated (e.g. links, examples, etc.)
  • Code is ready for review

@jujn
jujn requested review from a team and Copilot July 9, 2026 12:06

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes Anthropic request payload formatting when an assistant emits multiple parallel tool_use blocks by expanding them into Anthropic-required alternating assistant(tool_use) / user(tool_result) messages, addressing the 400 error described in #2077.

Changes:

  • Add detection of “parallel tool-use batch + consecutive tool-result messages” and expand it into alternating Anthropic messages in AnthropicMessageConverter.
  • Add a unit test covering conversion of two parallel tool calls into alternating messages.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
agentscope-extensions/agentscope-extensions-model/agentscope-extensions-model-anthropic/src/main/java/io/agentscope/extensions/model/anthropic/formatter/AnthropicMessageConverter.java Adds logic to detect completed parallel tool-call batches and rewrite them into alternating assistant/user messages for Anthropic compatibility.
agentscope-extensions/agentscope-extensions-model/agentscope-extensions-model-anthropic/src/test/java/io/agentscope/extensions/model/anthropic/formatter/AnthropicMessageConverterTest.java Adds a regression test validating the alternating-message expansion behavior.

@codecov

codecov Bot commented Jul 9, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 72.54902% with 14 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...anthropic/formatter/AnthropicMessageConverter.java 72.54% 4 Missing and 10 partials ⚠️

📢 Thoughts on this report? Let us know!

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.

[Bug]:Batch parallel tool calls produce invalid tool_use/tool_result message format for Anthropic API, causing 400 error

3 participants