Skip to content

fix: make console output resilient to terminal encoding#5159

Open
ATOM00blue wants to merge 1 commit into
Aider-AI:mainfrom
ATOM00blue:fix/console-encode-error-resilient-output
Open

fix: make console output resilient to terminal encoding#5159
ATOM00blue wants to merge 1 commit into
Aider-AI:mainfrom
ATOM00blue:fix/console-encode-error-resilient-output

Conversation

@ATOM00blue
Copy link
Copy Markdown

Why

Fixes #5128.

On consoles whose codepage isn't UTF-8 (for example Windows cp1252), aider crashes with an uncaught UnicodeEncodeError as soon as any output contains a character the codepage can't encode — checkmarks, arrows, box-drawing characters, emoji, or non-Latin text. The traceback bottoms out in aider/io.py _tool_message -> console.print -> rich's _write_buffer.

_tool_message already tries an ASCII fallback, but it can't recover: when rich's file.write raises, the unencodable text stays in rich's internal buffer (the del self._buffer[:] after the write never runs), so the retry re-flushes the same bad buffer and raises again. tool_output had no guard at all. The result is that essentially any decorated output takes down the whole session.

What

Relax the console output stream's error handler from strict to replace when the console is constructed. Unencodable characters are then emitted as a placeholder instead of raising, which fixes every console-backed output path at once rather than guarding individual call sites.

  • An explicit, non-strict error handler is left untouched.
  • Streams that can't be reconfigured are handled gracefully.
  • No new dependencies, no behavior change on UTF-8 terminals.

Test

Added regression tests that drive the real output paths against a strict cp1252 stream (a stand-in for a legacy Windows console) and assert no crash with unencodable characters replaced, plus a test that an explicit error handler is preserved.

On consoles whose codepage cannot encode every character (for example
Windows cp1252), printing a character the codec does not support raised
an uncaught UnicodeEncodeError that crashed the whole session. The
existing per-message ASCII fallback could not recover because rich leaves
the unencodable text in its output buffer after a failed write, so the
retry hit the same error.

Relax the console output stream's error handler to "replace" when it is
strict, so unencodable characters become a placeholder and output keeps
working. An explicit non-strict handler is left untouched.

Fixes Aider-AI#5128
Copilot AI review requested due to automatic review settings May 21, 2026 04:42
@CLAassistant
Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a 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.

Uncaught UnicodeEncodeError in console.py line 2097

3 participants