Skip to content

Fix/anthropic stream tool blocks#503

Closed
abdalla1912mohamed wants to merge 5 commits intostakpak:mainfrom
abdalla1912mohamed:fix/anthropic_stream_tool_blocks
Closed

Fix/anthropic stream tool blocks#503
abdalla1912mohamed wants to merge 5 commits intostakpak:mainfrom
abdalla1912mohamed:fix/anthropic_stream_tool_blocks

Conversation

@abdalla1912mohamed
Copy link

Fix Anthropic 400: Merge consecutive tool_result messages for API compliance

Summary

Fixes Anthropic API 400 errors (unexpected tool_use_id found in tool_result blocks) when an assistant turn includes multiple tool calls. Anthropic requires all tool results for a given assistant turn to be in a single user message. The client was sending separate Tool messages (one per result), which produced separate user messages and violated that rule.

Problem

When using direct Anthropic (ANTHROPIC_API_KEY), multi-tool-call turns failed with:

Anthropic API error 400: messages.N.content.0: unexpected `tool_use_id` found in `tool_result` blocks. 
Each `tool_result` block must have a corresponding `tool_use` block in the previous message.

Root cause: Separate tool-result messages make the 2nd+ tool_result reference a user message (the previous tool result) instead of the assistant message with tool_use blocks.

Before (broken) After (fixed)
assistant (tool_use A, B) assistant (tool_use A, B)
user (tool_result A) user (tool_result A, tool_result B)
user (tool_result B) ← 400 ← single merged message

Changes

  • libs/ai/src/providers/anthropic/convert.rs
    • Added merge_consecutive_tool_messages() to merge consecutive Role::Tool messages into one before converting to Anthropic format.
    • Wired into build_messages_with_caching() so it runs for all requests.
    • Added test_consecutive_tool_messages_merged_for_anthropic to cover merged format.

Testing

  • cargo test -p stakai test_consecutive_tool_messages_merged
  • Manual: Use ANTHROPIC_API_KEY with a prompt that triggers multiple tool calls in one turn; confirm no 400 errors.

Scope

  • Direct Anthropic only. When using STAKPAK_API_KEY, requests go through Stakpak’s backend; the merge must be implemented there. This PR does not fix the Stakpak cloud path.
  • Single-tool turns are unchanged; merging is effectively a no-op for one tool result.

References

@ahmedhesham6
Copy link
Collaborator

We don't want to change the stakai layer, cause its not its problem it should be in the AgentLoop

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