Why does gemini-cli WriteFile save garbled replacement characters in non-ASCII text?
Using gemini-cli installed with npm i -g, WriteFile saves a translated file containing garbled characters (shown as U+FFFD) when the content is non-ASCII, such as Traditional Chinese or Japanese. The text looks fine in the terminal, but the saved file is corrupted. What causes this and is there a workaround?
Originally asked on GitHub: https://github.com/google-gemini/gemini-cli/issues/2553
5 answers
Status: the thread points to a bug in the underlying
@google/genaiSDK, which the reporter traced to streamed responses being decoded in the middle of a multi-byte character. A fix was proposed in googleapis/js-genai PR #765; the thread does not confirm that it was merged, so check your SDK version after updating.Community workaround (use at your own risk): a contributor posted a shell script that patches the globally installed library. It locates
@google/genaivianpm root -g, and indist/node/index.mjsit replacesdecoder.decode(value);withdecoder.decode(value,{stream: true});. The script creates a backup file first. A macOS version was posted, and a Linux-compatible version was contributed and reported as working by another commenter. A combined macOS and Linux script was also shared.General advice: review any script before running it, since it edits a library file inside your global npm installation, and re-apply the patch after reinstalling or updating
@google/genai.A commenter noted that this problem is separate from issue #1945, which covers garbled shell output.
Did this work for you?What a GitHub user tried on GitHub (Didn't work)
Set a custom system prompt through GEMINI_SYSTEM_MD, ran
gemini -y, requested an EN to ZH-TW translation, then asked it to save the result to index.md. The WriteFile output contained garbled characters.Source: https://github.com/google-gemini/gemini-cli/issues/2553 (opens external site)
Did this work for you?What @doggy8088 tried on GitHub (Outcome unclear)
A contributor suspected the unescapeStringForGeminiBug function in packages/core/src/utils/editCorrector.ts as the cause. Another commenter disagreed, saying that function would not produce these characters.
Did this work for you?What @kyasbal tried on GitHub (Partly worked)
Shared a shell script for global npm installs that patches the @google/genai library to fix the garbling. The author said it was tested only on macOS and warned to use it at own risk.
Did this work for you?What @doggy8088 tried on GitHub (Worked)
Provided a Linux-compatible version of the patch script and reported that the workaround works on Linux.
Did this work for you?
Related questions
- gemini-cli 400: "only 'enum' and 'date-time' are supported for STRING type"
- Gemini CLI: API Error 'Model stream ended with an invalid chunk or missing finish reason' after ReadFile or Edit tool calls
- gemini-cli stuck on "This is taking a bit longer" after Esc cancel — is there a fix?
- Is there a way to get codebase indexing in gemini-cli?
- How can I use voice input instead of typing with Gemini CLI?