Skip to content

Fix: Resolve Unicode charmap error in stdout streaming#13

Merged
victorchall merged 1 commit intomainfrom
fix-unicode-error
Aug 7, 2025
Merged

Fix: Resolve Unicode charmap error in stdout streaming#13
victorchall merged 1 commit intomainfrom
fix-unicode-error

Conversation

@victorchall
Copy link
Owner

The application would raise a UnicodeEncodeError ('charmap' codec can't encode character) when the caption_openai.py script printed a Unicode character.

This was caused by redirecting sys.stdout to a custom StreamingStdout object in app.py that was missing the encoding attribute. Python's print() function would then fall back to a default, non-Unicode-friendly system encoding.

The fix involves two changes to the StreamingStdout class:

  1. Added self.encoding = 'utf-8' to the __init__ method to explicitly declare the stream's encoding.
  2. Removed a now-redundant and incorrect block that attempted to decode bytes in the write method. This was related to a previous attempt at a fix and is no longer needed.

The application would raise a `UnicodeEncodeError` ('charmap' codec can't encode character) when the `caption_openai.py` script printed a Unicode character.

This was caused by redirecting `sys.stdout` to a custom `StreamingStdout` object in `app.py` that was missing the `encoding` attribute. Python's `print()` function would then fall back to a default, non-Unicode-friendly system encoding.

The fix involves two changes to the `StreamingStdout` class:
1.  Added `self.encoding = 'utf-8'` to the `__init__` method to explicitly declare the stream's encoding.
2.  Removed a now-redundant and incorrect block that attempted to decode bytes in the `write` method. This was related to a previous attempt at a fix and is no longer needed.
@victorchall victorchall merged commit c3d4669 into main Aug 7, 2025
0 of 2 checks passed
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.

1 participant