← Back to Pull requests

fix(llmobs): keep streamed tool calls apart when the delta has no index #21111

Open
unknown opened this pull request with 355 lines diff across 4 files
View on GitHub
unknown unknown commented on Oct 20, 2026 03:22 UTC
Author

Description

Gemini's OpenAI-compatible endpoint (https://generativelanguage.googleapis.com/v1beta/openai) streams each parallel tool call whole (id, name and arguments) in its own chunk and leaves tool_calls[].index unset. openai_construct_tool_call_from_streamed_chunk looks up the stored call by index, so every call with index=None is appended to the first one. The streamed LLMObs span then shows a single tool call with the first call's id and the arguments concatenated.

Replaying a captured gemini-3.5-flash stream with two parallel get_weather calls through the openai client and openai_construct_message_from_streamed_chunks:

before: call_4234484 get_weather {"city":"Paris"}{"city":"Tokyo"}
after:  call_4234484 get_weather {"city":"Paris"}
        call_4234487 get_weather {"city":"Tokyo"}

When a delta has no index, an id that matches a call already collected continues that call, a new id starts a new call, and a delta without an id continues the latest call. Indexed deltas take the same path as before. The final sort used x.get("index", 0), which raises TypeError once two calls have index=None, so unindexed calls are now kept in arrival order after the indexed ones.

The same reducer is used by the litellm integration and by AI Guard's OpenAI streaming evaluation, so those paths get the fix too (they are not covered by the tests below).

Testing

  • New TestOpenAIConstructMessageWithUnindexedToolCalls in tests/llmobs/test_integrations_utils.py:

  • - test_parallel_calls_without_index_stay_separate: the two-call Gemini shape. It fails on main (one merged call) and passes with the fix.
    - test_unindexed_fragment_without_id_or_name_continues_latest_call: an unindexed argument fragment without id or name is appended to the latest call.
    - test_unindexed_fragments_repeating_the_same_id_continue_the_call: unindexed fragments that repeat the call's id on every chunk stay one call (as on main).
  • tests/llmobs/test_integrations_utils.py: 165 passed with the fix, vs 164 passed / 1 failed on main.

  • ruff check / ruff format --check (0.14.10) clean on the changed files.

  • Environment note: main's native extensions need a Rust toolchain that wasn't available, so the tests ran against the 4.16.0 wheel with main's ddtrace/llmobs sources (and ddtrace/internal/utils/service.py) on top. The reducer is identical between 4.16.0 and main. The AI Guard streaming tests weren't run locally for the same reason. The full suite runs in this PR's CI.
  • Risks

    Low. Only deltas without an index take the new branch; OpenAI and other backends that send index behave as before.

    Additional Notes

    This change was made with Claude Code; the tests above were run on the branch to check it.

    🤖 Generated with Claude Code

    Diff: 355 lines (+266 / -89) across 4 files Reviewer: pcbeingused333 Review duration: 53964m
    All Vouch review confidence checks passed
    Residual risk 0.32 = Change Risk (0.55) × (1 − Review Confidence (0.41)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.55 · 355 lines diff across 4 files
    0.55
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    40%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.351
    Shallow Review Depth shallow_review_depth
    0.344
    Ai Authored ai_authored
    0.050
    Real PR Conversation Timeline
    1 comments
    "Review active on #21111"
    general weight: 0.35
    Assignees
    Unassigned
    Labels
    risk: low confidence: 40.9% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 54.5% (MEDIUM) · Higher = larger blast radius
    54.5%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 40.9% · Lower = rushed or shallow review
    40.9%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 32.2% · Safe to merge
    32.2%