Skip to content

[AI] Refactor stream parsing to skip intermediate failures - #15960

Merged
andrewheard merged 6 commits into
mainfrom
ah/ai-intermediate-stream-decoding
Mar 20, 2026
Merged

[AI] Refactor stream parsing to skip intermediate failures#15960
andrewheard merged 6 commits into
mainfrom
ah/ai-intermediate-stream-decoding

Conversation

@andrewheard

@andrewheard andrewheard commented Mar 20, 2026

Copy link
Copy Markdown
Contributor

Refactored GenerativeModelSession.ResponseStream.AsyncIterator to skip intermediate JSON decoding failures when streaming JSON. Terminal errors are still thrown if the final streamed output cannot be parsed.

Context: When streaming structured output (JSON) from Gemini, the intermediate chunks are sometimes incomplete in a manner that is un-parseable by FoundationModels.GeneratedContent(json:). These intermediate failures are now skipped since delayed output is preferable to an error. In the worst case scenario this would result in the structured output being returned all at once, rather than incrementally, though I have never observed this.

@gemini-code-assist

Copy link
Copy Markdown
Contributor
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request refactors stream parsing to handle intermediate decoding failures, which is a good improvement for robustness when streaming structured data. The implementation correctly skips transient errors and only fails if the final chunk is unparsable. My main feedback is a minor suggestion to address code duplication between the next() and next(isolation:) methods to improve long-term maintainability.

Comment thread FirebaseAI/Sources/GenerativeModelSession.swift

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the stream parsing logic in GenerativeModelSession to gracefully handle intermediate JSON decoding failures. Instead of throwing an error immediately, it now skips these failures and only throws if the final streamed output cannot be parsed. The implementation correctly achieves this goal. My main feedback is regarding significant code duplication between the next() and next(isolation:) methods. I've suggested refactoring this into a private helper method to improve code clarity and maintainability.

Comment thread FirebaseAI/Sources/GenerativeModelSession.swift
@andrewheard
andrewheard requested a review from paulb777 March 20, 2026 01:09
@andrewheard
andrewheard marked this pull request as ready for review March 20, 2026 01:09

@paulb777 paulb777 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is it feasible to add a test?

@andrewheard

andrewheard commented Mar 20, 2026

Copy link
Copy Markdown
Contributor Author

Is it feasible to add a test?

@paulb777 I think we should, though it'll be a larger effort so I opened #15962 for tracking and added a TODO.

@andrewheard
andrewheard merged commit a04afd4 into main Mar 20, 2026
91 checks passed
@andrewheard
andrewheard deleted the ah/ai-intermediate-stream-decoding branch March 20, 2026 19:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants