Skip to content

fix(litellm): record every tool_result block of an anthropic message, not just the last - #3818

Merged
caroger merged 4 commits into
Arize-ai:mainfrom
Harsh23Kashyap:fix/litellm-anthropic-multi-tool-result
Sep 30, 2026
Merged

caroger merged 4 commits into
Arize-ai:mainfrom
Harsh23Kashyap:fix/litellm-anthropic-multi-tool-result

Conversation

@Harsh23Kashyap

Copy link
Copy Markdown
Contributor

Fixes #3817

When a user message carries several tool_result blocks (the standard reply to parallel tool calls), litellm's anthropic messages extractor writes every block to the same two keys, message.content and message.tool_call_id, at the message's index. Only the last block reaches the span; the earlier tool results are dropped from the trace.

This change records each tool_result block as its own input message with message.role = "tool", message.content, and message.tool_call_id:

  • A message holding only tool_result blocks expands into one tool message per block instead of a single "user" message that keeps only the last result.
  • Mixed contents are left as they are, as are string contents, the system prompt path, and the output message path.

Same fix shape as #3810 (anthropic), #3812 (vertexai), and #3814 (google-genai), per spec/tool_calling.md.

Test plan:

  • New regression test test_create_input_multiple_tool_result_blocks in tests/test_anthropic_messages.py; fails on main and passes with this change.
  • pytest tests/test_anthropic_messages.py - 24 passed, including the existing content-block tests, which are unaffected.

@ialisaleh ialisaleh left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reviewed the PR, and the change fixes the reported case by recording each parallel string tool result as a separate tool message with its content and tool_call_id. The regression test covers both results. As a follow-up, we can check list-valued tool_result.content, since the new path changes how it is recorded and may omit some nested block types. Thanks!

@caroger
caroger merged commit 8f0e355 into Arize-ai:main Sep 30, 2026
21 checks passed
@mikeldking mikeldking mentioned this pull request Sep 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

litellm: only the last tool_result block of an anthropic message is recorded

3 participants