Skip to content

anthropic: handleAIMessage only processes Parts[0], drops subsequent tool calls #1468

Description

@ElecTwix

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions