Skip to content

fix: handle UnicodeDecodeError in PlainTextConverter when charset detection is inaccurate (fixes #1505) - #1938

Closed
hanhan761 wants to merge 1 commit into
microsoft:mainfrom
hanhan761:fix-1505-plaintext-unicode-decode
Closed

hanhan761 wants to merge 1 commit into
microsoft:mainfrom
hanhan761:fix-1505-plaintext-unicode-decode

Conversation

@hanhan761

Copy link
Copy Markdown
Contributor

Summary

When stream_info.charset is detected from partial file content (first 4096 bytes) and returns 'ascii', but the full file contains non-ASCII characters (e.g., Spanish accents, UTF-8 characters), .decode('ascii') raises UnicodeDecodeError.

This fix adds a fallback: if decoding with the detected charset fails, fall back to charset_normalizer for automatic charset detection on the full file content.

Issue

Fixes #1505

Verification

  • UTF-8 files with Spanish characters now decode correctly
  • Pure ASCII files continue to work as before
  • Files where charset detection from partial content returns 'ascii' but full content has UTF-8 characters fall back gracefully

@afourney

Copy link
Copy Markdown
Member

Thank you for contributing the plaintext charset fallback. This implements the same fallback proposed in #1540, which also includes regression cases. I am closing this PR as a duplicate so review can continue in that proposal; this does not mean the remaining fallback behavior has already been merged.

If this PR covers an important case that #1540 misses, please reopen it or comment here so we can preserve that difference.

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.

Bug with Spanish symbols: PlainTextConverter threw UnicodeDecodeError with message: 'ascii' codec can't decode byte

2 participants