Fix UnicodeDecodeError when non-ASCII text falls past the sniffed charset prefix - #2337
Open
sesharif wants to merge 1 commit into
Open
Fix UnicodeDecodeError when non-ASCII text falls past the sniffed charset prefix#2337sesharif wants to merge 1 commit into
sesharif wants to merge 1 commit into
Conversation
The charset is sniffed from the first 4k of a stream but then used to decode all of it, so a mostly-ASCII document whose first non-ASCII character appears beyond that window fails to convert. Running `markitdown README.md` in this repo reproduces it: the first em dash sits at byte 5030, the prefix sniffs as ASCII, and the conversion dies with "'ascii' codec can't decode byte 0xe2 in position 5030". Fixed in two places: - Widen an ASCII guess to UTF-8 in _get_stream_info_guesses. ASCII is a strict subset of UTF-8, so genuine ASCII decodes identically, while UTF-8 content beyond the sniffed prefix now decodes correctly. This also matters because the charset is part of the StreamInfo identity used to decide which guesses are compatible, not just how bytes are decoded. - Add decode_bytes(), which tries the declared charset, re-detects over the full data if that fails, and finally falls back to a lossy decode. A charset can also arrive already declared -- from a Content-Type header, or from the caller -- in which case it is trusted without being sniffed, and can be wrong in the same way. Wired into the three converters that decode a whole stream with such a charset: plain text, CSV, and ipynb. The HTML-family converters pass the charset to BeautifulSoup as from_encoding, which recovers on its own, and are left alone. _normalize_charset now delegates to the shared helper rather than duplicating it. Two test vectors asserted charset="ascii" for test.json and test_notebook.ipynb. Both fixtures are pure ASCII, so UTF-8 decodes them identically; the expectations are updated to match the widened guess. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
|
sesharif please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The charset is sniffed from the first 4k of a stream but then used to decode all of it, so a mostly-ASCII document whose first non-ASCII character appears beyond that window fails to convert.
This repo's own README reproduces it — its first em dash sits at byte 5030:
Fix
Two layers, because the bug has two halves.
Detection — widen an ASCII guess to UTF-8 in
_get_stream_info_guesses. ASCII is a strict subset of UTF-8, so genuine ASCII decodes identically, while UTF-8 content beyond the sniffed prefix now decodes correctly. This layer matters beyond decoding: the charset is part of theStreamInfoidentity used to decide which guesses are compatible, so it also affects which converters get tried.Decoding — add
decode_bytes()in a new_charset_utilsmodule: try the declared charset, re-detect over the full data if that fails, and fall back to a lossy decode as a last resort. A charset can also arrive already declared — from aContent-Typeheader, or from the caller — in which case it is trusted without being sniffed and can be wrong in the same way.Wired into the three converters that decode a whole stream with such a charset: plain text, CSV, and ipynb. The HTML-family converters (
_html_converter,_bing_serp_converter,_wikipedia_converter,_youtube_converter) pass the charset to BeautifulSoup asfrom_encoding, which recovers on its own, so they are left alone._normalize_charsetnow delegates to the shared helper rather than duplicating it.Tests
New
tests/test_charset.py(16 tests) covering the sniffed-prefix regression, a too-narrow declared charset through each of the three converters, and thedecode_bytesfallback chain. Verified they catch the bug: with the fix reverted, 4 fail; restored, all pass.Full suite goes from 335 to 351 passing, with no new failures.
One expectation changed
Two vectors in
_test_vectors.pyassertedcharset="ascii"fortest.jsonandtest_notebook.ipynb. Both fixtures are pure ASCII, so UTF-8 decodes them byte-identically; the expectations are updated to match the widened guess. Flagging it explicitly since it is an assertion change rather than a code fix — happy to take a different approach if you would rather the widening not be observable in_get_stream_info_guessesoutput.🤖 Generated with Claude Code