fix(openai): keep n>1 stream choices separate in recorded output - #1864
Open
sailikhithk wants to merge 2 commits into
Open
fix(openai): keep n>1 stream choices separate in recorded output#1864sailikhithk wants to merge 2 commits into
sailikhithk wants to merge 2 commits into
Conversation
_extract_streamed_openai_response used a single accumulator for every choice in a chat completion stream, so an n>1 stream with interleaved choices merged content and tool-call arguments from separate choices into one corrupted output. Accumulate per choice index and return one response per choice when the stream carries multiple choices; single-choice streams keep the existing output shape.
An n>1 stream that only ever yields output for one nonzero choice index, for example after partial consumption, was read back from index 0 and recorded as None. Fall back to the single collected choice regardless of its index.
sailikhithk
force-pushed
the
fix/openai-stream-multi-choice-merging
branch
from
September 5, 2026 18:33
eddd8e8 to
aeb8e6b
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.
What does this PR do?
Fixes langfuse/langfuse#16998
_extract_streamed_openai_responseused a single accumulator for every choice in a chat completion stream. Ann>1stream interleaves choices within a single chunk, so content and tool-call arguments from separate choices were merged into one corrupted output (the issue's repro records"AB10"instead of"A0"and"B1").The extractor now accumulates per choice index and returns one response per choice when the stream carries multiple choices. Single-choice streams keep the existing output shape, so the common
n=1case is unchanged. A stream that only ever yields output for one nonzero choice index (partial consumption) now records that choice instead ofNone.Type of change
Verification
Checklist
code_review.md..env.templateif needed. (no docs impact: internal extractor fix, public output shape forn=1unchanged)