fix(openai): keep streamed choices separate - #1853
Open
1fanwang wants to merge 1 commit into
Open
Conversation
Signed-off-by: 1fanwang <1fannnw@gmail.com>
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?
OpenAI chat streams with
n > 1can interleave chunks from several choices. Langfuse currently combines them into one output, which corrupts both content and streamed tool arguments. After this change, each choice is recorded separately inchoice.indexorder, while single-choice streams keep their existing output shape.The stream parser now keeps one accumulator per choice. Tool-call indexes remain scoped to their choice, matching OpenAI's stream model. This completes the multiple-choice support introduced by #997, which covered request parameters and non-streaming responses.
Fixes langfuse/langfuse#16998
Type of change
Verification
Raw logs
Before:
After:
Checklist
code_review.md..env.templateif needed. No documentation changes are needed because this restores the existingn > 1output contract for streams.Greptile Summary
This PR separates interleaved OpenAI streamed chat choices into per-choice accumulators while preserving the existing singleton shape for one observed choice.
n=2.Confidence Score: 5/5
The PR appears safe to merge, with no actionable changed-code failures identified.
The new accumulator isolates content and tool calls by choice index, preserves single-choice output behavior, and orders multi-choice telemetry consistently with the non-streaming response contract.
Flowchart
%%{init: {'theme': 'neutral'}}%% flowchart LR A[OpenAI stream chunks] --> B[Read each choice index] B --> C[Per-choice accumulator] C --> D[Merge content and tool-call deltas] D --> E[Sort completed choices by index] E --> F{Number of observed choices} F -->|One| G[Singleton output] F -->|Multiple| H[Ordered output list]Reviews (1): Last reviewed commit: "fix(openai): keep streamed choices separ..." | Re-trigger Greptile
Context used: