Skip to content

Fix UnicodeEncodeError on startup when stdio uses a non-UTF-8 encoding - #2561

Open
LHMQ878 wants to merge 1 commit into
MoonshotAI:mainfrom
LHMQ878:fix/shell-console-utf8-stdio
Open

Fix UnicodeEncodeError on startup when stdio uses a non-UTF-8 encoding#2561
LHMQ878 wants to merge 1 commit into
MoonshotAI:mainfrom
LHMQ878:fix/shell-console-utf8-stdio

Conversation

@LHMQ878

@LHMQ878 LHMQ878 commented Jul 27, 2026

Copy link
Copy Markdown

Fixes #1436

Problem

On Windows, launching kimi from Git Bash dies immediately:

'gbk' codec can't encode character '▐' in position 0: illegal multibyte sequence

is , from the welcome banner logo in ui/shell/__init__.py:

▐█▛█▛█▌  Welcome to Kimi Code CLI!
▐█████▌  Send /help for help information.

The banner is rendered through the shared console singleton in src/kimi_cli/ui/shell/console.py, which inherits whatever encoding the standard streams have. On Windows those default to the system locale encoding (GBK/cp936 on Chinese installs), which cannot represent the box-drawing glyphs, so the very first console write raises UnicodeEncodeError and the CLI exits before it starts.

The same singleton also prints (U+2713) in ui/shell/oauth.py and ui/shell/setup.py, so logout and setup were exposed to the identical failure.

Fix

Reconfigure sys.stdout / sys.stderr to UTF-8 when the shell console module is initialized.

I first tried leaving the locale codec in place and only adding errors="replace". That stops the crash but renders the banner as partial-byte garbage (?��?��?����), because GBK is multibyte and replacement happens per unencodable character. Forcing UTF-8 keeps the glyphs intact, and I verified on a GBK-default Git Bash that a UTF-8 byte stream displays ▐█▛█▛█▌ correctly. errors="replace" is kept as a last resort for anything UTF-8 still can't carry (e.g. lone surrogates).

Streams that don't expose reconfigure(), or that reject it (detached/closed), are skipped rather than failing startup.

Verification

Environment: Windows 11, Git Bash, sys.stdout.encoding == 'gbk', locale.getpreferredencoding() == 'cp936' — i.e. the configuration from the report.

  • Reproduced the crash first: rendering the banner through a GBK-backed Rich console raises UnicodeEncodeError: 'gbk' codec can't encode character '▐'.
  • With the fix, stdout.encoding becomes utf-8, and both the banner panel and the message render correctly, in a terminal and with stdout redirected to a file.
  • Confirmed the crash returns if the new call is removed, so the test is exercising the fix rather than an unrelated change.
  • Added tests/ui/test_console_encoding.py (6 tests, all passing): reconfiguration of both streams, tolerance of streams without reconfigure(), tolerance of OSError/ValueError from it, plus a guard asserting the banner glyphs genuinely aren't GBK-encodable so the premise can't silently rot.
  • ruff check and ruff format --check pass on both changed files.

Note on overlap with #2560

I have a separate PR (#2560) fixing Fixes #2532, the same class of bug in utils/server.py's print_banner, which covers the kimi web and kimi vis startup paths. That one uses plain print(), so it needed a different fix. This PR covers the interactive shell path and is independent — neither depends on the other, and they touch different files.


Open in Devin Review

The welcome banner uses box-drawing glyphs (U+2590 and friends) and status
messages use U+2713. On Windows the standard streams default to the system
locale encoding (e.g. GBK/cp936), which cannot represent them, so the first
console write raised UnicodeEncodeError and the CLI died before starting.

Reconfigure stdout/stderr to UTF-8 when the shell console is set up. Forcing
UTF-8 preserves the glyphs, whereas errors='replace' alone on a multibyte
locale codec mangles them into partial-byte garbage.

Fixes MoonshotAI#1436

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Open in Devin Review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Gitbash start kimi failed

1 participant