← Back to Pull requests

fix(openai): keep streamed tool calls apart when they reuse an index #3585

Open
pcbeingused333 opened this pull request with 41 lines diff across 2 files
View on GitHub
pcbeingused333 pcbeingused333 commented on Nov 04, 2026 23:19 UTC
Author

Why are these changes needed?

_process_stream in ag2/config/openai/openai_client.py uses tc.index as the position of a streamed tool call. When an OpenAI-compatible endpoint sends two whole calls with different ids under the same index (for example index: 0 for every parallel call), they land in one entry: the second id and name overwrite the first and the arguments are concatenated, so the agent gets one broken call instead of two.

Each index now maps to an entry, and a delta with a new id under an index that already holds a different id opens a new entry. Fragments without an id still append to the call at their index, and deltas without an index keep the behaviour from #3611.

The case was raised in review on this PR. The PR's original scope, calls without an index, landed in #3611, so this is now limited to reused indexes.

Related issue number

None.

Checks

  • I've included any doc changes needed. (none needed)

  • I've added tests (if relevant) corresponding to the changes introduced in this PR. (test_whole_tool_calls_that_all_use_index_zero_stay_separate fails on main and passes here; the streams are built by hand with a mock transport, no live endpoint was called)

  • I've made sure all auto checks have passed.
  • AI assistance

    This change was made with Claude Code and Codex.

    🤖 Generated with Claude Code

    Diff: 41 lines (+26 / -15) across 2 files Reviewer: its-amann Review duration: 32754m
    All Vouch review confidence checks passed
    Residual risk 0.03 = Change Risk (0.32) × (1 − Review Confidence (0.90)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.32 · 41 lines diff across 2 files
    0.32
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.96 · 3 substantive comment(s)
    0.96
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 50%
    89%
    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.224
    Ai Authored ai_authored
    0.050
    Real PR Conversation Timeline
    4 items
    codecov[bot] codecov[bot] commented on Nov 06, 2026 06:51 UTC
    Bot

    Codecov Report


    :white_check_mark: All modified and coverable lines are covered by tests.

    | Files with missing lines | Coverage Δ | |
    |---|---|---|
    | ag2/config/openai/openai\_client.py | 86.50% <100.00%> (+8.90%) | :arrow_up: |

    ... and 1 file with indirect coverage changes
    <details><summary> :rocket: New features to boost your workflow: </summary>

  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

  • :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

  • </details>

    its-amann its-amann commented on code on Nov 07, 2026 06:35 UTC
    Code Review
    ag2/config/openai/openai_client.py:1
    @@ -224,8 +224,10 @@ async def _process_stream(
                         full_content += c
                         await context.send(ModelMessageChunk(c))
     
    -                for tc in delta.tool_calls or []:
    -                    ix = tc.index
    +                known_calls = len(full_tool_calls)
    +                for position, tc in enumerate(delta.tool_calls or []):
    +                    # OpenAI-compatible endpoints may leave out `index`: each such call is a whole call of its own
    +                    ix = tc.index if isinstance(tc.index, int) else known_calls + position

    if two whole tool calls both arrive with index 0 (some OpenAI compatible APIs do that), they still collapse into one slot here. the new tests only cover a missing index. worth treating a new call id under an occupied index as a separate call?

    pcbeingused333 pcbeingused333 commented on code on Nov 07, 2026 07:05 UTC
    Code Review
    ag2/config/openai/openai_client.py:1
    @@ -224,8 +224,10 @@ async def _process_stream(
                         full_content += c
                         await context.send(ModelMessageChunk(c))
     
    -                for tc in delta.tool_calls or []:
    -                    ix = tc.index
    +                known_calls = len(full_tool_calls)
    +                for position, tc in enumerate(delta.tool_calls or []):
    +                    # OpenAI-compatible endpoints may leave out `index`: each such call is a whole call of its own
    +                    ix = tc.index if isinstance(tc.index, int) else known_calls + position

    Good catch: a call that arrives with a new id under an index that already holds a different id now opens its own entry (indices map to entries instead of being list positions), so two whole calls both sent with index: 0 stay separate. Continuation chunks without an id still append to the call at their index. Added test_whole_tool_calls_that_all_use_index_zero_stay_separate (3875734).

    This comment was drafted with Claude Code.

    Reviewers
    Assignees
    pcbeingused333 pcbeingused333
    Labels
    risk: low confidence: 89.5% 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: 31.8% (LOW) · Higher = larger blast radius
    31.8%
    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: 89.5% · Lower = rushed or shallow review
    89.5%
    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: 3.3% · Safe to merge
    3.3%
    Repository