fix(chat): handle out-of-order streamed choice indexes - #3709
Open
HostX0 wants to merge 1 commit into
Open
Conversation
HostX0
force-pushed
the
fix/chat-stream-choice-index-order
branch
from
August 21, 2026 04:44
ad1e2a7 to
1440abb
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changes being requested
Fix Chat Completions stream accumulation when choice chunks arrive out of index order.
ChatCompletionStreamStatecurrently useschoice.indexas a Python list offset in the snapshot and event-state collections. That only works when every lower-numbered choice has already appeared. A chunk containing a single choice withindex=1, for example, has a one-elementchoiceslist, so the initial snapshot path attempts to assignchoices[1]and raisesIndexError.This change keeps the API-provided index as the choice identity instead of assuming it is the current list position:
choice.index;indexfield;The regression test feeds the accumulator a valid
1 -> 0 -> 1sequence and verifies that the later delta is appended to choice 1, while the final snapshot remains ordered as choices 0 and 1.Additional context & links
The production change is confined to the handwritten
src/openai/lib/streaming/chat/implementation; no generated API resource or public model changes are required.