close
Skip to content

anthropic: preserve all parts in AI message serialization#1494

Open
zxuhan wants to merge 1 commit intotmc:mainfrom
zxuhan:fix/anthropic-multi-part-ai-message
Open

anthropic: preserve all parts in AI message serialization#1494
zxuhan wants to merge 1 commit intotmc:mainfrom
zxuhan:fix/anthropic-multi-part-ai-message

Conversation

@zxuhan
Copy link
Copy Markdown

@zxuhan zxuhan commented Apr 22, 2026

Summary

handleAIMessage in llms/anthropic/anthropicllm.go only inspected msg.Parts[0]. When an assistant message contained both text and one or more tool calls (e.g. [TextContent, ToolCall] or [TextContent, ToolCall, ToolCall] for parallel tool use), every part after the first was silently dropped. The next tool_result message would then be rejected by Anthropic:

messages.N.content.0: unexpected `tool_use_id` found in `tool_result` blocks: toolu_...
Each `tool_result` block must have a corresponding `tool_use` block in the previous message.

This change iterates all parts and emits one Anthropic content block per part, mirroring the structure already used by handleHumanMessage.

Fixes #1468.

Test plan

  • go test ./llms/anthropic/... unit tests pass
  • go vet ./llms/anthropic/... clean
  • New TestHandleAIMessage_MultipleParts covers text + multiple tool calls
  • New TestHandleAIMessage_Errors covers empty parts and invalid tool arguments

handleAIMessage only read msg.Parts[0], silently dropping subsequent
tool calls when a response contained both text and one or more tool
uses. This caused Anthropic to reject follow-up tool_result messages
with 'tool_use block not found in previous message'.

Iterate over all parts and emit a content block per part.

Fixes tmc#1468.
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