When an AI message contains both text content and tool calls (e.g., [TextContent, ToolCall]), only the first part is serialized. The tool calls are silently dropped, causing Anthropic API errors when sending tool results.
Location
llms/anthropic/anthropicllm.go in handleAIMessage()
Problem
func handleAIMessage(msg llms.MessageContent) (anthropicclient.ChatMessage, error) {
if toolCall, ok := msg.Parts[0].(llms.ToolCall); ok { // Only checks Parts[0]
// handles single ToolCall
return ...
}
if textContent, ok := msg.Parts[0].(llms.TextContent); ok { // Only checks Parts[0]
// handles single TextContent
return ...
}
return anthropicclient.ChatMessage{}, fmt.Errorf(...)
}
When Anthropic returns a response with both text ("I'll help you...") and a tool call, and we build an assistant message with Parts: [TextContent, ToolCall], only the TextContent is serialized. The ToolCall is dropped.
This causes the error:
messages.2.content.0: unexpected `tool_use_id` found in `tool_result` blocks: toolu_xxx.
Each `tool_result` block must have a corresponding `tool_use` block in the previous message.
Expected Behavior
handleAIMessage should iterate through all Parts and include both text and tool_use content blocks in the Anthropic message.
Workaround
Put ToolCall first in Parts and skip text content when there are tool calls:
assistantMsg := llms.MessageContent{Role: llms.ChatMessageTypeAI}
// Put tool calls first (langchaingo only reads Parts[0])
for _, tc := range toolCalls {
assistantMsg.Parts = append(assistantMsg.Parts, tc)
}
// Skip text content - langchaingo can't handle both
Related
This is similar to #1010 (multi-part tool results) but affects AI/assistant messages instead.
When an AI message contains both text content and tool calls (e.g.,
[TextContent, ToolCall]), only the first part is serialized. The tool calls are silently dropped, causing Anthropic API errors when sending tool results.Location
llms/anthropic/anthropicllm.goinhandleAIMessage()Problem
When Anthropic returns a response with both text ("I'll help you...") and a tool call, and we build an assistant message with
Parts: [TextContent, ToolCall], only the TextContent is serialized. The ToolCall is dropped.This causes the error:
Expected Behavior
handleAIMessageshould iterate through all Parts and include bothtextandtool_usecontent blocks in the Anthropic message.Workaround
Put ToolCall first in Parts and skip text content when there are tool calls:
Related
This is similar to #1010 (multi-part tool results) but affects AI/assistant messages instead.