gemini-cli - ✅(Solved) Fix Frequent ERR_STREAM_PREMATURE_CLOSE during response streaming and tool execution [1 pull requests, 4 comments, 3 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
google-gemini/gemini-cli#25253Fetched 2026-04-14 05:56:29
View on GitHub
Comments
4
Participants
3
Timeline
10
Reactions
0
Author
Timeline (top)
commented ×4labeled ×3unlabeled ×2cross-referenced ×1

Error Message

I am experiencing frequent ERR_STREAM_PREMATURE_CLOSE errors while using the Gemini CLI. This error has occurred multiple times

  • Error Code: ERR_STREAM_PREMATURE_CLOSE

PR fix notes

PR #25360: Handle ERR_STREAM_PREMATURE_CLOSE as a retryable error

Description (problem / solution / changelog)

Added ERR_STREAM_PREMATURE_CLOSE to RETRYABLE_NETWORK_CODES to automatically recover from premature stream closures during long generation.

  • test(core): add regression test for ERR_STREAM_PREMATURE_CLOSE with large content

Summary

#This PR resolves the frequent ERR_STREAM_PREMATURE_CLOSE errors encountered during long-form content generation or large-payload sessions. By categorizing this specific Node.js error code as a retryable network exception, we activate the system's built-in "retry-with-context" mechanism, significantly improving the reliability of streaming interactions.

Details

  • Core Change: Added ERR_STREAM_PREMATURE_CLOSE to the RETRYABLE_NETWORK_CODES list in packages/core/src/utils/retry.ts.
  • Technical Context: This error typically occurs when the HTTP response headers are received, but the underlying socket is closed before the full stream body is consumed (common in unstable networks or long-lived HTTP/2 streams).
  • Architectural Rational:
    • Since the Gemini API is stateless and the CLI maintains the full conversation Context, re-initiating the request with the existing history is the most effective application-layer self-healing path for stream interruptions.
    • This change is surgical and safe, as it primarily affects idempotent generation requests and does not introduce side effects for non-idempotent tools.
  • AI Review Synthesis: This solution has passed 10 consecutive rounds of deep AI code review, confirming its correctness, performance, and adherence to engineering standards.

Related Issues

https://github.com/google-gemini/gemini-cli/issues/25253

How to Validate

The fix has been rigorously verified using a TDD (Test-Driven Development) workflow:

  1. Reproduction (RED): Created a test case simulating a complex 10KB session history that fails mid-stream (after 20 chunks) with ERR_STREAM_PREMATURE_CLOSE. Verified that the system crashed without the fix.
  2. Verification (GREEN): Applied the fix and confirmed that the same test case now automatically triggers a retry and successfully completes the generation.
  3. Regression Guard: Permanently added the reproduction case to the official test suite.

Validation Command: 1 npm test -w @google/gemini-cli-core -- src/core/geminiChat_network_retry.test.ts Expected Result: All 8 network-related retry tests (including the new regression test) pass successfully.

Pre-Merge Checklist

<!-- Check all that apply before requesting review or merging. -->
  • Updated relevant documentation and README (if needed)
  • [ x ] Added/updated tests (if needed)
  • Noted breaking changes (if any)
  • [ x ] Validated on required platforms/methods:
    • [ x ] MacOS
      • [ x ] npm run
      • npx
      • Docker
      • Podman
      • Seatbelt
    • Windows
      • npm run
      • npx
      • Docker
    • Linux
      • npm run
      • npx
      • Docker

Changed files

  • packages/core/src/core/geminiChat_network_retry.test.ts (modified, +79/-0)
  • packages/core/src/utils/retry.ts (modified, +1/-0)
RAW_BUFFERClick to expand / collapse

What happened?

I am experiencing frequent ERR_STREAM_PREMATURE_CLOSE errors while using the Gemini CLI. This error has occurred multiple times (n times) today during both long response generations and sequences of tool executions. The process terminates abruptly, forcing me to restart the task.

[ACTION REQUIRED] 📎 PLEASE ATTACH THE EXPORTED CHAT HISTORY JSON FILE TO THIS ISSUE IF YOU FEEL COMFORTABLE SHARING IT.

What did you expect to happen?

I expected the streaming response and tool execution to complete successfully without the stream closing prematurely. The connection should remain stable throughout long generations, or provide a more resilient recovery mechanism.

Client information

  • CLI Version: 0.37.1
  • Git Commit: 3b2d4f100
  • Session ID: 566b8616-9967-4125-921f-3436d4ac16d9
  • Operating System: darwin v25.8.2
  • Sandbox Environment: no sandbox
  • Model Version: auto-gemini-3
  • Auth Type: oauth-personal
  • Memory Usage: 360.6 MB
  • Terminal Name: tmux 3.6a
  • Terminal Background: #000000
  • Kitty Keyboard Protocol: Unsupported

Login information

Authenticated using oauth-personal.

Anything else we need to know?

  • Error Code: ERR_STREAM_PREMATURE_CLOSE
  • Frequency: Very high (happened numerous times today).
  • Context: The issue seems to trigger more often when the model is generating large amounts of text or when multiple complex tools are being called in succession.
  • Note: I am attaching the exported chat history JSON to this issue to help with the investigation of the stream interruption.

extent analysis

TL;DR

Investigate and potentially adjust the Gemini CLI configuration or environment to handle large response generations and sequential tool executions without premature stream closure.

Guidance

  • Review the exported chat history JSON file for patterns or specific requests that may be causing the ERR_STREAM_PREMATURE_CLOSE errors.
  • Check the system resources (e.g., memory, network connectivity) to ensure they are not contributing to the premature closures, especially during large text generations or multiple tool executions.
  • Consider testing with a smaller model version or reducing the complexity of tool executions to see if the issue persists.
  • Look into any recent changes in the operating system, terminal, or Kitty keyboard protocol that might be interfering with the Gemini CLI's streaming functionality.

Example

No specific code snippet can be provided without more details on the Gemini CLI's internal workings or the exact nature of the chat history JSON file.

Notes

The high frequency of ERR_STREAM_PREMATURE_CLOSE errors suggests a potential issue with handling large or complex requests. The fact that it happens more often with large text generations or multiple tool executions implies a possible resource limitation or configuration issue.

Recommendation

Apply workaround: Adjust the Gemini CLI usage to avoid large response generations or sequential complex tool executions until a more permanent fix can be identified, as the root cause is not immediately clear from the provided information.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING