Skip to content

llms/anthropic: handle multiple parts in AI messages#1513

Open
Rony-ZenAlden wants to merge 1 commit into
tmc:mainfrom
Rony-ZenAlden:fix/anthropic-handleaimessage-multipart
Open

llms/anthropic: handle multiple parts in AI messages#1513
Rony-ZenAlden wants to merge 1 commit into
tmc:mainfrom
Rony-ZenAlden:fix/anthropic-handleaimessage-multipart

Conversation

@Rony-ZenAlden

Copy link
Copy Markdown

handleAIMessage only inspected msg.Parts[0], so an assistant message containing both text and a tool call (e.g. [TextContent, ToolCall]) serialized only the first part and silently dropped the rest. This caused Anthropic API errors on the following turn because the tool_result had no corresponding tool_use block.

Iterate over every part and emit a content block for each, mirroring handleHumanMessage. This also fixes a potential panic when Parts is empty (previously an out-of-range access on Parts[0]).

Add unit tests covering a mixed text + tool-call message and the empty-parts case.

Fixes #1468

PR Checklist

  • Read the Contributing documentation.
  • Read the Code of conduct documentation.
  • Name your Pull Request title clearly, concisely, and prefixed with the name of the primarily affected package you changed according to Good commit messages (such as memory: add interfaces for X, Y or util: add whizzbang helpers).
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. Fixes #123).
  • Describes the source of new concepts.
  • References existing implementations as appropriate.
  • Contains test coverage for new functions.
  • Passes all golangci-lint checks.

handleAIMessage only inspected msg.Parts[0], so an assistant message
containing both text and a tool call (e.g. [TextContent, ToolCall])
serialized only the first part and silently dropped the rest. This
caused Anthropic API errors on the following turn because the
tool_result had no corresponding tool_use block.

Iterate over every part and emit a content block for each, mirroring
handleHumanMessage. This also fixes a potential panic when Parts is
empty (previously an out-of-range access on Parts[0]).

Add unit tests covering a mixed text + tool-call message and the
empty-parts case.

Fixes tmc#1468

Co-Authored-By: Claude Opus 4.8 <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