fix: deduplicate tool results and merge consecutive tool_result blocks for Anthropic API#1793
Merged
alexhoshina merged 1 commit intosipeed:mainfrom Mar 23, 2026
Conversation
…sult blocks for Anthropic API Anthropic API returns 400 when multiple tool_result blocks share the same tool_use_id, or when consecutive tool results are sent as separate user messages. This fix: 1. Adds ToolCallID deduplication in sanitizeHistoryForProvider (context.go) to drop duplicate tool results before sending to any provider. 2. Merges consecutive tool result messages into a single user message with multiple tool_result content blocks in Anthropic's buildRequestBody, for both "user" (with ToolCallID) and "tool" role messages. 3. Adds tests for both behaviors. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This was referenced Mar 22, 2026
andressg79
pushed a commit
to andressg79/picoclaw
that referenced
this pull request
Mar 30, 2026
…sult blocks for Anthropic API (sipeed#1793) Anthropic API returns 400 when multiple tool_result blocks share the same tool_use_id, or when consecutive tool results are sent as separate user messages. This fix: 1. Adds ToolCallID deduplication in sanitizeHistoryForProvider (context.go) to drop duplicate tool results before sending to any provider. 2. Merges consecutive tool result messages into a single user message with multiple tool_result content blocks in Anthropic's buildRequestBody, for both "user" (with ToolCallID) and "tool" role messages. 3. Adds tests for both behaviors. Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
ra1phdd
pushed a commit
to ra1phdd/picoclaw-pkg
that referenced
this pull request
Apr 12, 2026
…sult blocks for Anthropic API (sipeed#1793) Anthropic API returns 400 when multiple tool_result blocks share the same tool_use_id, or when consecutive tool results are sent as separate user messages. This fix: 1. Adds ToolCallID deduplication in sanitizeHistoryForProvider (context.go) to drop duplicate tool results before sending to any provider. 2. Merges consecutive tool result messages into a single user message with multiple tool_result content blocks in Anthropic's buildRequestBody, for both "user" (with ToolCallID) and "tool" role messages. 3. Adds tests for both behaviors. Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
armmer016
pushed a commit
to cryptoquantumwave/khunquant
that referenced
this pull request
Apr 14, 2026
…sult blocks for Anthropic API (sipeed#1793) Anthropic API returns 400 when multiple tool_result blocks share the same tool_use_id, or when consecutive tool results are sent as separate user messages. This fix: 1. Adds ToolCallID deduplication in sanitizeHistoryForProvider (context.go) to drop duplicate tool results before sending to any provider. 2. Merges consecutive tool result messages into a single user message with multiple tool_result content blocks in Anthropic's buildRequestBody, for both "user" (with ToolCallID) and "tool" role messages. 3. Adds tests for both behaviors. Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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.
Fixes #1792
📝 Description
修复使用 Anthropic 提供商时,LLM 调用返回 400 错误:
each tool_use must have a single result. Found multiple tool_result blocks with id的问题。根因有两个:
sanitizeHistoryForProvider未对重复的ToolCallID进行去重,导致相同 ID 的 tool result 被多次发送。buildRequestBody中将每个 tool result 作为独立的user消息发送,而 Anthropic API 要求连续的 tool result 必须合并到同一个user消息的content数组中。本 PR 的修改:
sanitizeHistoryForProvider(pkg/agent/context.go)的第二遍扫描中增加seenToolCallIDmap,遇到相同ToolCallID的 tool 消息时仅保留第一个,跳过后续重复项。buildRequestBody(pkg/providers/anthropic_messages/provider.go)中,将连续的 tool result(无论是role: "user"+ToolCallID还是role: "tool"格式)合并到前一个user消息的content数组中,而非创建新的独立消息。🗣️ Type of Change
🤖 AI Code Generation
🔗 Related Issue
📚 Technical Context (Skip for Docs)
tool_use对应唯一一个tool_result,且同一轮对话中的多个 tool result 必须放在同一个user消息的content数组内。违反这两个约束时 API 会返回 400 错误。🧪 Test Environment
📸 Evidence (Optional)
Click to view Logs/Screenshots
修复前错误日志:
修复后单元测试通过:
☑️ Checklist