Fix/anthropic stream tool blocks#503
Closed
abdalla1912mohamed wants to merge 5 commits intostakpak:mainfrom
Closed
Fix/anthropic stream tool blocks#503abdalla1912mohamed wants to merge 5 commits intostakpak:mainfrom
abdalla1912mohamed wants to merge 5 commits intostakpak:mainfrom
Conversation
Collaborator
|
We don't want to change the stakai layer, cause its not its problem it should be in the AgentLoop |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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: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_useblocks.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)← 400Changes
libs/ai/src/providers/anthropic/convert.rsmerge_consecutive_tool_messages()to merge consecutiveRole::Toolmessages into one before converting to Anthropic format.build_messages_with_caching()so it runs for all requests.test_consecutive_tool_messages_merged_for_anthropicto cover merged format.Testing
cargo test -p stakai test_consecutive_tool_messages_mergedANTHROPIC_API_KEYwith a prompt that triggers multiple tool calls in one turn; confirm no 400 errors.Scope
STAKPAK_API_KEY, requests go through Stakpak’s backend; the merge must be implemented there. This PR does not fix the Stakpak cloud path.References