Skip to content

fix(anthropic): iterate all message parts in handleAIMessage and handleToolMessage#1505

Open
superShen0916 wants to merge 1 commit into
tmc:mainfrom
superShen0916:fix/anthropic-multi-part-ai-message
Open

fix(anthropic): iterate all message parts in handleAIMessage and handleToolMessage#1505
superShen0916 wants to merge 1 commit into
tmc:mainfrom
superShen0916:fix/anthropic-multi-part-ai-message

Conversation

@superShen0916

Copy link
Copy Markdown

Summary

Fixes #1468. handleAIMessage and handleToolMessage only checked Parts[0], silently dropping text content when tool calls were present, or dropping subsequent tool calls in multi-part messages.

This caused Anthropic API errors like:

unexpected tool_use_id found in tool_result blocks

Changes

  • llms/anthropic/anthropicllm.go: Rewrote handleAIMessage to iterate all msg.Parts and collect both TextContent and ToolCall parts into a single ChatMessage.Content slice
  • Same fix applied to handleToolMessage for consistency (multiple tool results in one message)

Test plan

  • go build ./llms/anthropic/ — compiles
  • go vet ./llms/anthropic/ — no issues
  • Existing integration tests require API keys and are not affected by this change

Risk

  • Low. The change is backward-compatible: single-part messages (the previous only supported case) produce the same output as before.

…leToolMessage

Previously both functions only checked Parts[0], silently dropping text
content when a tool call was present, or dropping subsequent tool calls
in multi-part messages. This caused Anthropic API errors like
"unexpected tool_use_id found in tool_result blocks" when the AI
response contained both text and tool_use content.

Iterate all parts to support mixed content (text + tool_use) and
multiple parallel tool calls/results in a single message.

Fixes tmc#1468

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
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.

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

1 participant