Skip to content

fix(core): improve diagnostics for malformed streaming responses#22352

Open
junaiddshaukat wants to merge 12 commits into
google-gemini:mainfrom
junaiddshaukat:fix/streaming-response-diagnostics
Open

fix(core): improve diagnostics for malformed streaming responses#22352
junaiddshaukat wants to merge 12 commits into
google-gemini:mainfrom
junaiddshaukat:fix/streaming-response-diagnostics

Conversation

@junaiddshaukat
Copy link
Copy Markdown
Contributor

Summary

Improves robustness and diagnostics in the streaming pipeline by making malformed or incomplete streamed responses easier to detect and debug.

Details

This change makes three focused improvements in the core streaming path:

  • In packages/core/src/core/turn.ts, stream events with no response body are no longer skipped silently. They now emit a warning and continue processing later chunks.
  • In packages/core/src/core/geminiChat.ts, invalid streaming response chunks are explicitly warned about before being skipped, making stream degradation easier to diagnose.
  • In packages/core/src/code_assist/server.ts, the SSE parser now flushes the final buffered data: chunk even when the stream ends without a trailing blank line.

This keeps behavior unchanged for healthy streams while improving debuggability and correctness for malformed / incomplete streaming scenarios.

Related Issues

Fixes #22351

How to Validate

  1. Run the focused test suites:
    npm test -w @google/gemini-cli-core -- src/core/turn.test.ts src/core/geminiChat.test.ts src/code_assist/server.test.ts

  2. Confirm the tests pass, including coverage for:
    warning and skipping stream events without a response body
    warning when invalid stream chunks are skipped before a valid completion
    parsing a final SSE data chunk without a trailing blank line

Pre-Merge Checklist

  • Updated relevant documentation and README (if needed)
  • Added/updated tests (if needed)
  • Noted breaking changes (if any)
  • Validated on required platforms/methods:
    • MacOS
      • npm run
      • npx
      • Docker
      • Podman
      • Seatbelt
    • Windows
      • npm run
      • npx
      • Docker
    • Linux
      • npm run
      • npx
      • Docker

Local Tests

image

@junaiddshaukat junaiddshaukat requested a review from a team as a code owner March 13, 2026 17:16
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the diagnostic capabilities and robustness of the streaming pipeline. By introducing explicit warnings for malformed or incomplete streamed responses and refining the Server-Sent Events (SSE) parser's behavior, it makes it much easier to detect and debug issues in streaming interactions, ensuring more reliable operation without altering behavior for healthy streams.

Highlights

  • Improved Stream Event Handling: Stream events in turn.ts that lack a response body now emit a warning instead of being silently skipped, enhancing debuggability.
  • Explicit Warning for Invalid Chunks: The geminiChat.ts module now explicitly warns when invalid streaming response chunks are encountered and skipped, aiding in diagnosing stream degradation.
  • Robust SSE Parsing: The SSE parser in server.ts was updated to correctly flush the final buffered data: chunk even if the stream ends without a trailing blank line, improving correctness for incomplete streaming scenarios.
Changelog
  • packages/core/src/code_assist/server.test.ts
    • Added a test case to verify that the SSE parser correctly handles a final data chunk without a trailing blank line.
  • packages/core/src/code_assist/server.ts
    • Modified the SSE parser to process any remaining buffered lines as a final chunk when the stream ends, even if no trailing blank line is present, and logs a warning for malformed JSON.
  • packages/core/src/core/geminiChat.test.ts
    • Imported debugLogger.
    • Added a test case to confirm that a warning is issued when invalid stream chunks are skipped before a valid completion.
  • packages/core/src/core/geminiChat.ts
    • Imported debugLogger.
    • Implemented a warning log when an invalid streaming response chunk is skipped.
  • packages/core/src/core/turn.test.ts
    • Imported debugLogger.
    • Added a test case to ensure a warning is logged and events are skipped when a stream event lacks a response body.
  • packages/core/src/core/turn.ts
    • Imported debugLogger.
    • Added a warning log when a stream event is received without a response body, preventing silent skipping.
Activity
  • The author, junaiddshaukat, created this pull request to improve diagnostics and robustness for streaming responses.
  • The pull request includes new and updated tests to cover the enhanced error handling and parsing logic.
  • The author has validated the changes on MacOS using npm run.
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
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-cli gemini-cli Bot added the area/core Issues related to User Interface, OS Support, Core Functionality label Mar 13, 2026
Copy link
Copy Markdown
Contributor

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

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 improves the robustness and diagnostics of the streaming pipeline by adding warnings for malformed or incomplete stream data and correctly handling streams that end without a final blank line. The changes are well-tested and improve debuggability. I have one suggestion regarding code duplication in the SSE parser to enhance maintainability and ensure error logging adheres to best practices.

Comment thread packages/core/src/code_assist/server.ts Outdated
@gemini-cli
Copy link
Copy Markdown
Contributor

gemini-cli Bot commented Mar 28, 2026

Hi there! Thank you for your interest in contributing to Gemini CLI.

To ensure we maintain high code quality and focus on our prioritized roadmap, we have updated our contribution policy (see Discussion #17383).

We only guarantee review and consideration of pull requests for issues that are explicitly labeled as 'help wanted'. All other community pull requests are subject to closure after 14 days if they do not align with our current focus areas. For this reason, we strongly recommend that contributors only submit pull requests against issues explicitly labeled as 'help-wanted'.

This pull request is being closed as it has been open for 14 days without a 'help wanted' designation. We encourage you to find and contribute to existing 'help wanted' issues in our backlog! Thank you for your understanding and for being part of our community!

@gemini-cli gemini-cli Bot closed this Mar 28, 2026
@cocosheng-g
Copy link
Copy Markdown
Contributor

@junaiddshaukat, apologies for the bot closing this PR! We have reopened it. Please sync your branch to the latest main and we will have someone review it shortly.

@cocosheng-g cocosheng-g reopened this Apr 8, 2026
@gemini-cli gemini-cli Bot added the help wanted We will accept PRs from all issues marked as "help wanted". Thanks for your support! label Apr 8, 2026
Resolve conflict in packages/core/src/code_assist/server.ts by keeping
the deduplicated yieldBufferedChunk helper introduced in this PR, which
covers the same logic that upstream inlined.
@junaiddshaukat
Copy link
Copy Markdown
Contributor Author

@cocosheng-g i have resolved the merge confilcts.

@cocosheng-g
Copy link
Copy Markdown
Contributor

/gemini review

Copy link
Copy Markdown
Contributor

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

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 improves the robustness of the Gemini CLI by adding debug logging when skipping invalid streaming response chunks or events without a response body. It refactors the SSE chunk processing logic in CodeAssistServer to ensure proper handling of final data chunks and adds corresponding unit tests to verify these improvements across the core components.

@cocosheng-g
Copy link
Copy Markdown
Contributor

Please fix test

@junaiddshaukat
Copy link
Copy Markdown
Contributor Author

@cocosheng-g I have fixed CI tests, pleaes have a look now

@gemini-cli gemini-cli Bot added the priority/p2 Important but can be addressed in a future release. label May 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/core Issues related to User Interface, OS Support, Core Functionality help wanted We will accept PRs from all issues marked as "help wanted". Thanks for your support! priority/p2 Important but can be addressed in a future release.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve diagnostics and error handling for malformed streaming responses

2 participants