Skip to content

fix(responses): keep affinity item IDs consistent across stream events - #49338

Closed
tin-berri wants to merge 2 commits into
mainfrom
litellm_47881_responses_item_identity
Closed

tin-berri wants to merge 2 commits into
mainfrom
litellm_47881_responses_item_identity

Conversation

@tin-berri

@tin-berri tin-berri commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Responses streams could give one reasoning item two different IDs, causing clients that merge by ID to retain duplicate history. This keeps the same deployment-carrying ID from the first event through completion, preserving ID-only follow-up routing and matching the Python and Rust codecs

Details

TLDR

Problem this solves:

  • One reasoning item appeared under two stream IDs
  • ID-based history merging retained duplicate reasoning

How it solves it:

  • Encode reasoning and compaction IDs from their first event
  • Normalize corresponding event references through one shared helper
  • Keep repeated normalization safe across Python and Rust

Intentional product change: with encrypted-content affinity enabled, reasoning and compaction IDs carry deployment metadata before encrypted content arrives, including plaintext reasoning items; existing ID-only routing remains available

User Flow

Before: merging the streamed response by item ID can retain duplicate reasoning

  1. Send POST http://localhost:44783/v1/responses with stream: true, store: false, and encrypted reasoning included
  2. Receive rs_… during item events and encitem_… at completion
  3. Merge by ID and retain two reasoning records for replay

After: the same request retains one reasoning item and its deployment affinity

  1. Send POST http://localhost:44783/v1/responses with stream: true, store: false, and encrypted reasoning included
  2. Receive the same encitem_… during item events and completion
  3. Merge by ID and retain one reasoning record for replay

Relevant issues

Pre-Submission checklist

  • I have added meaningful tests
  • The handful of test files covering my change pass locally
  • My PR passes all required CI/CD checks
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • Every automated review on my PR's latest commit has finished, with findings addressed or answered

Screenshots / Proof of Fix

Actual local LiteLLM, PostgreSQL, and OpenAI gpt-5-mini, with encrypted-content affinity enabled and separate baseline/final checkouts

Before (7e4403ea66)

  1. Stream the request above over SSE and WebSocket, then merge item-done and completed output by ID
  2. Observe rs_… on item events, encitem_… on completed output, and two retained reasoning items
  3. Replay the merged history: current OpenAI accepts it; the reporter's rejecting fixture reproduced the duplicate-item HTTP 400

After (deb0ceee99)

  1. Repeat the same SSE and WebSocket requests and merge
  2. Observe one identical encitem_… on added, done, and completed output, one retained reasoning item, and a completed second turn
  3. Create a stored response on deployment B, change routing weights so ordinary traffic chooses A, then replay only its item references
  4. Observe control traffic on A, ID-only replay on B, and content replay on B; both replays complete

The recording runs fresh HTTP requests against the actual proxy; IDs are shortened with hashes only for display

Live stream and deployment-affinity proof

responses-rebased-demo.mp4

Rebased onto main 7e4403ea66 at deb0ceee99, retaining both imports in the Anthropic bridge test conflict

  • Fresh SSE and WebSocket proof: baseline retains two reasoning items; rebased code retains one and completes the second turn
  • Fresh nonstream proof: baseline and rebased code preserve encoded IDs, wrapped content, and successful replay
  • Fresh two-deployment proof: ordinary requests choose A while ID-only and content replays return to their origin B
  • Local checks: 946 Python tests including the budget regression, 35 Rust codec tests, 28 affected native parity cases, 47 routing integration tests, 46 CI ownership tests, and make check passed
  • Main includes the budget month-boundary fix in test(budgets): age the monthly window past any calendar month so the reset test passes on the 31st #49372 and dependency updates in build(deps): bump pyasn1, pypdf and minimatch to clear osv-scan #49362; OSV now passes
  • Upstream fix(streaming)!: fail a stream that ends without a finish reason #47665 exposed a missing finish_reason in the installed SDK HTTP fixture across Python 3.10-3.15; this PR corrects that test fixture
  • The SDK HTTP script failed before the fixture correction and passes afterward, including sync/async streaming, usage, error mapping, retries, cancellation, and Bedrock authentication
  • All five required CI checks and the Python 3.10-3.15 SDK matrix pass at 5a32061c71
  • Greptile 5/5, Bugbot and Veria pass at the current tip, with every review thread resolved
  • Python, JavaScript/TypeScript and Actions CodeQL analyses report zero findings

Proof carry-forward: live requests and recording tested deb0ceee99; current tip 5a32061c71 adds only the SDK fixture finish reason. git diff deb0ceee99 HEAD -- litellm litellm-rust is empty; the full SDK HTTP check and make check pass at the current tip

Type

Bug Fix

Caveats

Medium

  • Existing duplicated client history still needs client cleanup

Low

  • Native Rust Responses routing remains disabled by existing policy

Final Attestation

  • Regression checks cover stream identity, resumed references, replay affinity, and codec parity

@tin-berri

Copy link
Copy Markdown
Contributor Author

@greptileai Please review the current tip for stream identity consistency, wrapped-content affinity, and backward compatibility of legacy encoded IDs

@tin-berri

Copy link
Copy Markdown
Contributor Author

bugbot run

Please check current stream item identity, legacy replay compatibility, and affinity behavior with encrypted content present or absent

@greptile-apps

greptile-apps Bot commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge with no new actionable findings

Summary

The PR keeps reasoning and compaction IDs consistent across stream events and final output while preserving deployment routing on replay

  • Reasoning and compaction items keep one ID across Responses events.
  • The SDK HTTP stream fixture marks its chat reply as finished.

Reviews (6) · Last reviewed commit: "test(sdk): terminate the HTTP stream fix..." · Reviewed by Greptile

Comment thread litellm/responses/utils.py Resolved

@cursor cursor Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

@codspeed

codspeed Bot commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

1 warning

⚠️ 1 benchmark is dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 43 untouched benchmarks


Comparing litellm_47881_responses_item_identity (5a32061) with main (ced995d)1

Open in CodSpeed

Footnotes

  1. No successful run was found on main (3129e44) during the generation of this report, so ced995d was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@tin-berri

Copy link
Copy Markdown
Contributor Author

@greptileai Please re-review the added live WebSocket and nonstream before/after evidence; the source head remains 72a1431

@tin-berri tin-berri changed the title fix(responses): preserve encrypted item IDs across stream events fix(responses)!: preserve encrypted item IDs across stream events Oct 30, 2026
@tin-berri

Copy link
Copy Markdown
Contributor Author

@greptileai The title now marks the documented ID-only affinity compatibility change with fix(responses)!; please re-review the current head and completed live proof

@codecov

codecov Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.66667% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
litellm/responses/utils.py 96.22% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

@tin-berri
tin-berri force-pushed the litellm_47881_responses_item_identity branch from 72a1431 to e11a41e Compare October 31, 2026 01:21
@tin-berri tin-berri changed the title fix(responses)!: preserve encrypted item IDs across stream events fix(responses): keep affinity item IDs consistent across stream events Oct 31, 2026
@tin-berri

Copy link
Copy Markdown
Contributor Author

@greptileai Please review e11a41e for stable encoded stream IDs, preserved ID-only deployment affinity, Rust codec parity, and updated live replay evidence

@tin-berri

Copy link
Copy Markdown
Contributor Author

@cursor review e11a41e for consistent reasoning event references, preserved ID-only affinity, idempotent Python and Rust codecs, and completion bridge coverage

@cursor cursor Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

@tin-berri
tin-berri force-pushed the litellm_47881_responses_item_identity branch from e11a41e to deb0cee Compare October 31, 2026 19:32
@tin-berri

Copy link
Copy Markdown
Contributor Author

@greptileai Please review rebased commit deb0cee, preserving upstream bridge behavior and both test imports, with 946 Python regression tests passing

@tin-berri

Copy link
Copy Markdown
Contributor Author

@cursor review deb0cee after rebasing onto current main, retaining upstream bridge tests and existing stable encoded IDs and deployment affinity

@cursor cursor Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

@tin-berri

Copy link
Copy Markdown
Contributor Author

@greptileai Please review 5a32061 with refreshed real-provider proof and the SDK fixture correction for upstream stream EOF validation

@tin-berri

Copy link
Copy Markdown
Contributor Author

@cursor Please review 5a32061, including the SDK stream fixture correction; refreshed live proof covers identical production code on deb0cee

@cursor

cursor Bot commented Oct 31, 2026

Copy link
Copy Markdown
Contributor

Unable to authenticate your request. Please make sure to connect your GitHub account to Cursor. Go to Cursor

@tin-berri

Copy link
Copy Markdown
Contributor Author

bugbot run for commit 5a32061, covering the SDK fixture correction and unchanged production code verified by the refreshed recording

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 5a32061. Configure here.

@CLAassistant

CLAassistant commented Nov 5, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@github-actions

Copy link
Copy Markdown
Contributor

#47881 was fixed by #51243 on main, so this pull request is closed. Reopen it if something was missed.

@github-actions github-actions Bot closed this Nov 11, 2026
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.

[Bug]: encrypted_content_affinity gives one streamed Responses item two IDs, so clients replay duplicate reasoning items

2 participants