Repository navigation
fix(genai): handle empty choices in OpenAI stream formatters - #1975
Open
SAMEERARIF43 wants to merge 1 commit into
Open
SAMEERARIF43 wants to merge 1 commit into
SAMEERARIF43 wants to merge 1 commit into
Conversation
Valid OpenAI-compatible streaming events can contain no text: role and finish deltas have no content, and the final usage chunk sends an empty choices array when usage reporting is enabled. The OpenAI and Azure OpenAI formatters indexed choices[0] unconditionally, raising RangeError (Ollama inherits the OpenAI formatter). SSEDisplay also appended "<?>" whenever a formatter returned null, leaking those non-text events into the displayed answer. Guard the choices/message/delta access in both formatters so non-text events return null, and skip null formatter results in SSEDisplay so text deltas concatenate exactly. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
SAMEERARIF43
commented
Dec 2, 2026
SAMEERARIF43
left a comment
Author
There was a problem hiding this comment.
handle empty choices in OpenAI stream formatters
DONE
SAMEERARIF43
commented
Dec 3, 2026
SAMEERARIF43
left a comment
Author
There was a problem hiding this comment.
handled empty choices in OpenAI stream formatters.
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.
Summary
Valid OpenAI-compatible streaming events can contain no text, and the app mishandled them in two ways:
RangeError on empty
choices. OpenAI documents an emptychoicesarray for the final usage chunk when usage reporting is enabled, and role/finish deltas carry nocontent.OpenAIModel.streamOutputFormatterandAzureOpenAIModel.streamOutputFormatterindexedchoices[0]unconditionally (x["choices"]?[0]["delta"]?["content"]), sochoices: []threwRangeError (length).OllamaModelinherits the OpenAI formatter, so it crashed too.<?>leaking into the answer.SSEDisplayappendedz ?? '<?>'for every event whose formatter returned null, so role-only, finish, and usage events surfaced as literal<?>text in the displayed response.Changes
packages/genai/lib/interface/model_providers/openai.dartandazureopenai.dart:outputFormatter/streamOutputFormatternow guardchoices(non-list or empty →null), verify each level is aMap, and returnnullwhen there is no text content. Ollama is fixed via inheritance.lib/widgets/sse_display.dart: null formatter results append nothing instead of<?>, so text deltas concatenate exactly, including whitespace.Tests
packages/genai/test/interface/model_providers/: new cases for OpenAI, Azure OpenAI, and Ollama covering role-only deltas, finish events, the usage-onlychoices: []chunk, and missingchoices— plus anAIRequestModel.getFormattedStreamOutputdispatch check.test/widgets/sse_display_test.dart: widget test feeding role/text/finish/usage events throughSSEDisplaywith an OpenAIAIRequestModel, asserting the output is exactlyHello worldwith no<?>.Verified offline on Flutter 3.47.9:
flutter test --no-pub packages/genai/test/interface/model_providers— 42/42 passflutter test --no-pub test/widgets/sse_display_test.dart— 4/4 passNote: Anthropic and Gemini formatters have the same latent
choices[0]/candidates[0]indexing pattern; left unchanged to keep this PR scoped to the reported providers.This contribution is AI-assisted (Devin). Reimplements the approach from the closed #1958 with the now-documented issue report.
Fixes #1961