Skip to content

fix(responses): keep the message output_item.done sequence_number monotonic - #48017

Closed
shurrman wants to merge 1 commit into
BerriAI:mainfrom
shurrman:pr/responses-sequence-number
Closed

shurrman wants to merge 1 commit into
BerriAI:mainfrom
shurrman:pr/responses-sequence-number

Conversation

@shurrman

Copy link
Copy Markdown

TLDR

Problem this solves:

  • The chat-completions to Responses streaming bridge sends the message response.output_item.done with sequence_number=1
  • When text precedes a tool call, that event arrives after events numbered higher (observed: [..., 9, 10, 1])
  • Clients that order events by sequence_number (Codex CLI) render the message text twice

How it solves it:

  • Advance the per-stream sequence counter for that event, like every other event in the iterator

User Flow

Before: a Codex CLI user on a model served through the chat-completions bridge sees the reply duplicated

  1. They send POST https://litellm-domain/v1/responses with "stream": true to a non-Responses-native model (e.g. an Anthropic model)
  2. The model replies with text and then calls a tool
  3. The final message response.output_item.done arrives with sequence_number: 1, lower than earlier events
  4. Codex shows the same message text twice

After: the reply shows once

  1. They send the same streaming request
  2. Every event's sequence_number is strictly increasing, and Codex shows the text once

Pre-Submission checklist

  • I have added meaningful tests
  • The test files covering my change pass locally: uv run pytest tests/unit/responses/litellm_completion_transformation/test_streaming_iterator_transformation.py -v
  • My PR passes all required CI/CD checks
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • Greptile Confidence Score of at least 4/5

Screenshots / Proof of Fix

We have run this patch in production on a self-hosted proxy since v1.101.0. Codex CLI sessions on Claude models through /v1/responses then showed each text item once, with strictly increasing sequence_number values. A Before/After run captured on this branch's merge base and tip will be added before review is requested.

Type

🐛 Bug Fix

The chat-completions to Responses API streaming bridge hardcoded
sequence_number=1 on the final response.output_item.done event for the
text message. When a response contains a tool call, every other event
in the same stream uses a growing counter, so that event ends up with a
sequence_number lower than events that were actually emitted earlier
(observed live: [...9, 10, 1]).

A client that orders streamed events by sequence_number treats the
message's out-of-order done event as a new, later item and re-renders
the already-streamed message text a second time. This reproduced with
claude-sonnet-5 through our ChatGPT/Anthropic Responses bridge whenever
the model replied with text before calling a tool.

Advance the existing per-stream sequence_number counter for this event
instead of hardcoding 1, matching every other event type in this class.

Confirmed still present in upstream v1.101.0 and v1.102.0 (stable).
@codspeed

codspeed Bot commented Oct 23, 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

✅ 35 untouched benchmarks


Comparing shurrman:pr/responses-sequence-number (5db97d9) with main (f2118fc)

Open in CodSpeed

@codecov

codecov Bot commented Oct 23, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


Alexandr A. Alexandrov seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

@mateo-berri

Copy link
Copy Markdown
Contributor

Thanks for this. #44944 numbers every bridged streaming event from zero, which covers this case, so we plan to close this after it merges

@mateo-berri

Copy link
Copy Markdown
Contributor

Closing as superseded by #44944, which is now merged and numbers every bridged streaming event from zero. Thanks for the fix!

@mateo-berri mateo-berri closed this Nov 2, 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.

3 participants