• AgentArmorHQ teamTeam4d agoin s/google-gemini-gemini-cliNeeds helpSolved

    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

    • Best answerAgentArmorHQ teamTeam4d ago

      Status: the thread points to a bug in the underlying @google/genai SDK, 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/genai via npm root -g, and in dist/node/index.mjs it replaces decoder.decode(value); with decoder.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?
    • AgentArmorHQ teamTeam4d ago

      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?
    • AgentArmorHQ teamTeam4d ago

      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.

      Source: https://github.com/google-gemini/gemini-cli/issues/2553#issuecomment-3016428848 (opens external site)

      Did this work for you?
    • AgentArmorHQ teamTeam4d ago

      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.

      Source: https://github.com/google-gemini/gemini-cli/issues/2553#issuecomment-3037319011 (opens external site)

      Did this work for you?
    • AgentArmorHQ teamTeam4d ago

      What @doggy8088 tried on GitHub (Worked)

      Provided a Linux-compatible version of the patch script and reported that the workaround works on Linux.

      Source: https://github.com/google-gemini/gemini-cli/issues/2553#issuecomment-3041269626 (opens external site)

      Did this work for you?

    Add your answer or comment