Repository navigation
fix(ai): keep the choices of an n>1 OpenAI chat stream apart - #1152
Merged
marandaneto merged 2 commits intoOct 30, 2026
Merged
Conversation
marandaneto
approved these changes
Oct 30, 2026
marandaneto
left a comment
Member
There was a problem hiding this comment.
The code looks good; no qualifying findings. Verdict: correct.
Member
|
thanks @jaeminbird |
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.
Motivation and Context
Streaming an OpenAI chat completion with
n>1merges every choice into one._ChatCompletionsStreamState(posthog/ai/openai/_streaming.py) reads onlychoices[0]of each chunk and appends to a single buffer, but each chunk carries one choice identified bychoices[].index. The text interleaves ("HelloBonjour world le monde"), tool calls from different choices collide on index 0, and$ai_stop_reasoncomes from whichever choice finished last. The non-streamed path already keeps one item per choice.How did you test it?
The stream state now collects content and tool calls per choice index and builds
$ai_output_choicesin the same shape as the non-streamed path; the stop reason is choice 0's, as inextract_openai_stop_reason.n=1streams are unchanged.test_streaming_chat_keeps_n_choices_apartfails on main (interleaved text) and passes with the fix; 3 reruns passed.$ai_output_choicesequals the non-streamed capture of the same answer.python -W error -c "import posthog"clean. Full suite: no new failures (19 failures and 2 collection errors are pre-existing Windows/Anthropic environment problems).Checklist
make public_api_checknot run).Agent context
Autonomy: human-driven (agent-assisted). Kimi Code found the bug by comparing the streamed capture with the non-streamed one and wrote the fix and test; I reviewed and verified it.