gemini-cli - ✅(Solved) Fix Error saving message to chat history. RangeError: Invalid string length at JSON.stringify (<anonymous>) ChatRecordingService.writeConversation [1 pull requests, 1 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#24902Fetched 2026-04-09 08:17:39
View on GitHub
Comments
0
Participants
1
Timeline
4
Reactions
0
Author
Participants
Timeline (top)
labeled ×2cross-referenced ×1referenced ×1

Error Message

This morning (German time) I've had a slow internet connection with occasional outages (not a DNS problem), which could have contributed to the error. │ ✖ Error writing conversation file. RangeError: Invalid string length │ │ ✖ Error saving message to chat history. RangeError: Invalid string length │ │ ✖ Error when talking to Gemini API Full report available at: │ │ /tmp/gemini-client-error-Turn.run-sendMessageStream-2026-04-08T07-18-01-234Z.json RangeError: Invalid string │ │ ✖ Error writing conversation file. RangeError: Invalid string length │ │ ✖ Error saving message to chat history. RangeError: Invalid string length │ │ ✖ Error writing conversation file. RangeError: Invalid string length │ │ ✖ Error saving message to chat history. RangeError: Invalid string length │ │ This is an unexpected error. Please file a bug report using the /bug tool. │ I don't think I saw any error message indicating that an update failed. This morning (German time) I've had a slow internet connection with occasional outages (not a DNS problem), which could have contributed to the error.

Root Cause

A dialog box will appear asking for a "retry" because it might involve a file system operation (i.e., saving the chat history).

Fix Action

Fixed

PR fix notes

PR #24910: fix: handle RangeError when conversation exceeds JSON serializable size

Description (problem / solution / changelog)

Problem

When a conversation grows very large, JSON.stringify in ChatRecordingService.writeConversation can throw a RangeError: Invalid string length because the serialized output exceeds V8's maximum string length (~512MB). This error was not caught by the existing handler, causing an unhandled promise rejection that crashed the CLI with:

CRITICAL: Unhandled Promise Rejection: RangeError: Invalid string length

Fixes #24902

Solution

Mirror the existing ENOSPC graceful-degradation pattern:

  • Catch RangeError in writeConversation's error handler
  • Set conversationFile and cachedConversation to null to disable recording
  • Emit a debugLogger.warn message so the user knows why recording stopped
  • Return without throwing — the conversation continues uninterrupted

Changes

  • packages/core/src/services/chatRecordingService.ts: add RANGE_WARNING_MESSAGE constant and RangeError catch branch in writeConversation
  • packages/core/src/services/chatRecordingService.test.ts: add 3 regression tests covering the new behavior (mirrors existing ENOSPC test suite structure)

Testing

All existing tests continue to pass. New tests verify:

  1. No throw when RangeError occurs during writeConversation
  2. Recording is disabled (file path becomes null) after RangeError
  3. Subsequent recording calls are no-ops after recording is disabled

Changed files

  • packages/core/src/services/chatRecordingService.test.ts (modified, +83/-0)
  • packages/core/src/services/chatRecordingService.ts (modified, +49/-6)

Code Example

> /about
# paste output here
RAW_BUFFERClick to expand / collapse

What happened?

I'm working on Windows with WLS2 Terminal under the default Ubuntu system.

This morning (German time) I've had a slow internet connection with occasional outages (not a DNS problem), which could have contributed to the error.

`Debug Console (F12 to close) │ │ │ │ ✖ Error writing conversation file. RangeError: Invalid string length │ │ at JSON.stringify (<anonymous>) │ │ at ChatRecordingService.writeConversation │ │ (file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/chunk-2RPVSGOE.js:252232:31) │ │ at ChatRecordingService.updateConversation │ │ (file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/chunk-2RPVSGOE.js:252260:10) │ │ at ChatRecordingService.recordMessage │ │ (file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/chunk-2RPVSGOE.js:252050:12) │ │ at GeminiChat.sendMessageStream │ │ (file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/chunk-2RPVSGOE.js:308490:33) │ │ at process.processTicksAndRejections (node:internal/process/task_queues:104:5) │ │ at async Turn.run (file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/chunk-2RPVSGOE.js:308994:30) │ │ at async GeminiClient.processTurn │ │ (file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/chunk-2RPVSGOE.js:312711:22) │ │ at async GeminiClient.sendMessageStream │ │ (file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/chunk-2RPVSGOE.js:312824:14) │ │ at async file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/interactiveCli-FJD2UDE7.js:43116:24 │ │ ✖ Error saving message to chat history. RangeError: Invalid string length │ │ at JSON.stringify (<anonymous>) │ │ at ChatRecordingService.writeConversation │ │ (file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/chunk-2RPVSGOE.js:252232:31) │ │ at ChatRecordingService.updateConversation │ │ (file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/chunk-2RPVSGOE.js:252260:10) │ │ at ChatRecordingService.recordMessage │ │ (file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/chunk-2RPVSGOE.js:252050:12) │ │ at GeminiChat.sendMessageStream │ │ (file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/chunk-2RPVSGOE.js:308490:33) │ │ at process.processTicksAndRejections (node:internal/process/task_queues:104:5) │ │ at async Turn.run (file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/chunk-2RPVSGOE.js:308994:30) │ │ at async GeminiClient.processTurn │ │ (file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/chunk-2RPVSGOE.js:312711:22) │ │ at async GeminiClient.sendMessageStream │ │ (file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/chunk-2RPVSGOE.js:312824:14) │ │ at async file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/interactiveCli-FJD2UDE7.js:43116:24 │ │ ✖ Error when talking to Gemini API Full report available at: │ │ /tmp/gemini-client-error-Turn.run-sendMessageStream-2026-04-08T07-18-01-234Z.json RangeError: Invalid string │ │ length │ │ at JSON.stringify (<anonymous>) │ │ at ChatRecordingService.writeConversation │ │ (file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/chunk-2RPVSGOE.js:252232:31) │ │ at ChatRecordingService.updateConversation │ │ (file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/chunk-2RPVSGOE.js:252260:10) │ │ at ChatRecordingService.recordMessage │ │ (file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/chunk-2RPVSGOE.js:252050:12) │ │ at GeminiChat.sendMessageStream │ │ (file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/chunk-2RPVSGOE.js:308490:33) │ │ at process.processTicksAndRejections (node:internal/process/task_queues:104:5) │ │ at async Turn.run (file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/chunk-2RPVSGOE.js:308994:30) │ │ at async GeminiClient.processTurn │ │ (file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/chunk-2RPVSGOE.js:312711:22) │ │ at async GeminiClient.sendMessageStream │ │ (file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/chunk-2RPVSGOE.js:312824:14) │ │ at async file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/interactiveCli-FJD2UDE7.js:43116:24 │ │ ✖ Error writing conversation file. RangeError: Invalid string length │ │ at JSON.stringify (<anonymous>) │ │ at ChatRecordingService.writeConversation │ │ (file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/chunk-2RPVSGOE.js:252232:31) │ │ at ChatRecordingService.updateConversation │ │ (file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/chunk-2RPVSGOE.js:252260:10) │ │ at ChatRecordingService.recordMessage │ │ (file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/chunk-2RPVSGOE.js:252050:12) │ │ at file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/chunk-Y5DDI4MA.js:63090:35 │ │ at file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/interactiveCli-FJD2UDE7.js:42905:7 │ │ at file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/interactiveCli-FJD2UDE7.js:43140:13 │ │ at async file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/interactiveCli-FJD2UDE7.js:43291:38 │ │ at async file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/chunk-2RPVSGOE.js:253665:23 │ │ ✖ Error saving message to chat history. RangeError: Invalid string length │ │ at JSON.stringify (<anonymous>) │ │ at ChatRecordingService.writeConversation │ │ (file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/chunk-2RPVSGOE.js:252232:31) │ │ at ChatRecordingService.updateConversation │ │ (file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/chunk-2RPVSGOE.js:252260:10) │ │ at ChatRecordingService.recordMessage │ │ (file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/chunk-2RPVSGOE.js:252050:12) │ │ at file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/chunk-Y5DDI4MA.js:63090:35 │ │ at file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/interactiveCli-FJD2UDE7.js:42905:7 │ │ at file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/interactiveCli-FJD2UDE7.js:43140:13 │ │ at async file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/interactiveCli-FJD2UDE7.js:43291:38 │ │ at async file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/chunk-2RPVSGOE.js:253665:23 │ │ ✖ Error writing conversation file. RangeError: Invalid string length │ │ at JSON.stringify (<anonymous>) │ │ at ChatRecordingService.writeConversation │ │ (file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/chunk-2RPVSGOE.js:252232:31) │ │ at ChatRecordingService.updateConversation │ │ (file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/chunk-2RPVSGOE.js:252260:10) │ │ at ChatRecordingService.recordMessage │ │ (file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/chunk-2RPVSGOE.js:252050:12) │ │ at file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/chunk-Y5DDI4MA.js:63090:35 │ │ at file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/interactiveCli-FJD2UDE7.js:43340:15 │ │ at async file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/chunk-2RPVSGOE.js:253665:23 │ │ ✖ Error saving message to chat history. RangeError: Invalid string length │ │ at JSON.stringify (<anonymous>) │ │ at ChatRecordingService.writeConversation │ │ (file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/chunk-2RPVSGOE.js:252232:31) │ │ at ChatRecordingService.updateConversation │ │ (file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/chunk-2RPVSGOE.js:252260:10) │ │ at ChatRecordingService.recordMessage │ │ (file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/chunk-2RPVSGOE.js:252050:12) │ │ at file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/chunk-Y5DDI4MA.js:63090:35 │ │ at file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/interactiveCli-FJD2UDE7.js:43340:15 │ │ at async file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/chunk-2RPVSGOE.js:253665:23 │ │ ✖ ========================================= │ │ This is an unexpected error. Please file a bug report using the /bug tool. │ │ CRITICAL: Unhandled Promise Rejection! │ │ ========================================= │ │ Reason: RangeError: Invalid string length │ │ Stack trace: │ │ RangeError: Invalid string length │ │ at JSON.stringify (<anonymous>) │ │ at ChatRecordingService.writeConversation │ │ (file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/chunk-2RPVSGOE.js:252232:31) │ │ at ChatRecordingService.updateConversation │ │ (file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/chunk-2RPVSGOE.js:252260:10) │ │ at ChatRecordingService.recordMessage │ │ (file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/chunk-2RPVSGOE.js:252050:12) │ │ at file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/chunk-Y5DDI4MA.js:63090:35 │ │ at file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/interactiveCli-FJD2UDE7.js:43340:15 │ │ at async file:///usr/local/lib/node_modules/@google/gemini-cli/bundle/chunk-2RPVSGOE.js:253665:23

What did you expect to happen?

A dialog box will appear asking for a "retry" because it might involve a file system operation (i.e., saving the chat history).

Client information

<details> <summary>Client Information</summary>

I just noticed that I'm not using the final version of 0.36.

I don't think I saw any error message indicating that an update failed.

Why didn't the automatic update work this morning? (Perhaps due to an unstable internet connection?)

About Gemini CLI │ │ │ │ CLI Version 0.36.0-preview.3 │ │ Git Commit 8e2629759 │ │ Model Auto (Gemini 3) │ │ Sandbox no sandbox │ │ OS linux │ │ Auth Method Signed in with Google ([email protected]) │ │ Tier Gemini Code Assist in Google One AI Pro │ │ │

> /about
# paste output here
</details>

Login information

Google Account

Anything else we need to know?

This morning (German time) I've had a slow internet connection with occasional outages (not a DNS problem), which could have contributed to the error.

extent analysis

TL;DR

Check the conversation file for invalid string lengths and ensure stable internet connectivity to prevent RangeError: Invalid string length errors.

Guidance

  • Verify the conversation file for any corrupted or malformed data that could be causing the RangeError: Invalid string length error.
  • Check the internet connection stability, as occasional outages may be contributing to the issue.
  • Consider updating to the latest version of the Gemini CLI, as the current version (0.36.0-preview.3) may have known issues.
  • Review the error logs and stack traces to identify any patterns or specific error messages that could help diagnose the issue.

Example

No specific code example is provided, as the issue seems to be related to data corruption or internet connectivity rather than a code-specific problem.

Notes

The issue may be related to the unstable internet connection, which could be causing data corruption or incomplete writes to the conversation file. Updating to the latest version of the Gemini CLI may also help resolve the issue.

Recommendation

Apply workaround: Check the conversation file for invalid string lengths and ensure stable internet connectivity to prevent RangeError: Invalid string length errors. This is because the issue seems to be related to data corruption or internet connectivity rather than a code-specific problem, and updating to the latest version may not be immediately possible or may not fix the issue.

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