Skip to content

fix(genai): handle empty choices in OpenAI stream formatters - #1975

Open
SAMEERARIF43 wants to merge 1 commit into
foss42:mainfrom
SAMEERARIF43:resolve-issue-openai-empty-choices
Open

SAMEERARIF43 wants to merge 1 commit into
foss42:mainfrom
SAMEERARIF43:resolve-issue-openai-empty-choices

Conversation

@SAMEERARIF43

@SAMEERARIF43 SAMEERARIF43 commented Dec 2, 2026 •

Copy link
Copy Markdown

Summary

Valid OpenAI-compatible streaming events can contain no text, and the app mishandled them in two ways:

  1. RangeError on empty choices. OpenAI documents an empty choices array for the final usage chunk when usage reporting is enabled, and role/finish deltas carry no content. OpenAIModel.streamOutputFormatter and AzureOpenAIModel.streamOutputFormatter indexed choices[0] unconditionally (x["choices"]?[0]["delta"]?["content"]), so choices: [] threw RangeError (length). OllamaModel inherits the OpenAI formatter, so it crashed too.

  2. <?> leaking into the answer. SSEDisplay appended z ?? '<?>' 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.dart and azureopenai.dart: outputFormatter/streamOutputFormatter now guard choices (non-list or empty → null), verify each level is a Map, and return null when 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-only choices: [] chunk, and missing choices — plus an AIRequestModel.getFormattedStreamOutput dispatch check.
  • test/widgets/sse_display_test.dart: widget test feeding role/text/finish/usage events through SSEDisplay with an OpenAI AIRequestModel, asserting the output is exactly Hello world with no <?>.

Verified offline on Flutter 3.47.9:

  • flutter test --no-pub packages/genai/test/interface/model_providers — 42/42 pass
  • flutter test --no-pub test/widgets/sse_display_test.dart — 4/4 pass

Note: 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

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 SAMEERARIF43 left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handle empty choices in OpenAI stream formatters
DONE

@SAMEERARIF43 SAMEERARIF43 left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handled empty choices in OpenAI stream formatters.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: OpenAI-compatible non-text stream events raise RangeError and add placeholders

1 participant