[AI] Refactor stream parsing to skip intermediate failures - #15960
Conversation
Using Gemini Code AssistThe 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
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 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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
This reverts commit 14c8402.
paulb777
left a comment
There was a problem hiding this comment.
Is it feasible to add a test?
Refactored
GenerativeModelSession.ResponseStream.AsyncIteratorto 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.