Skip to content

Conversation

@hewliyang
Copy link

@hewliyang hewliyang commented Jan 24, 2026

Summary

Add support for accumulating tool calls from streaming delta chunks in both sync and async streaming handlers for the LiteLLM instrumentation.

Changes

  • Add _accumulate_tool_calls() to collect tool call chunks by index
  • Add _build_message_from_accumulated() to format messages for attribute extraction
  • Update _finalize_sync_streaming_span() to accumulate tool calls
  • Update _finalize_streaming_span() (async) to accumulate tool calls
  • Add tests for streaming with single and multiple tool calls

Context

The existing streaming handlers (_finalize_sync_streaming_span and _finalize_streaming_span) only captured delta.content but missed delta.tool_calls. This meant tool calls in streaming responses were not being recorded in spans.

The implementation follows the pattern from OpenAI instrumentation's _ChatCompletionAccumulator but in a simpler form since LiteLLM already normalizes the streaming format.

Testing

  • Added some unit tests w/ mock streams (iiuc, this is how its done in the openai instrumentation.) But LMK if we need to get an actual cassette for it

Add support for accumulating tool calls from streaming delta chunks in
both sync and async streaming handlers.

Changes:
- Add _accumulate_tool_calls() to collect tool call chunks by index
- Add _build_message_from_accumulated() to format messages for attribute extraction
- Update _finalize_sync_streaming_span() to accumulate tool calls
- Update _finalize_streaming_span() (async) to accumulate tool calls
- Add tests for streaming with single and multiple tool calls

The implementation follows the pattern from OpenAI instrumentation's
_ChatCompletionAccumulator but in a simpler form since LiteLLM already
normalizes the streaming format.
@dosubot dosubot bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Jan 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L This PR changes 100-499 lines, ignoring generated files.

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

1 participant