Skip to content

fix: avoid splitting UTF-8 characters during charset detection - #2466

Merged
afourney merged 1 commit into
mainfrom
utf8_splitting
Sep 11, 2026
Merged

fix: avoid splitting UTF-8 characters during charset detection#2466
afourney merged 1 commit into
mainfrom
utf8_splitting

Conversation

@afourney

Copy link
Copy Markdown
Member

Charset detection samples the first 64 KiB of text input. If that boundary falls inside a multibyte UTF-8 character, the truncated sample can be mistaken for another encoding. Applying that guess to the full document can silently corrupt valid text—for example, turning  résumé  into  rĂ©sumĂ© —or cause conversion to fail.

This change completes a trailing UTF-8 character before passing the sample to  charset_normalizer :

• Use a strict incremental decoder to distinguish a split character from invalid UTF-8.
• Read at most three additional bytes, stopping as soon as the character is complete.
• Preserve the original sample if the prefix or continuation is invalid, or EOF prevents completion.

The existing charset detector still selects the encoding; the fix does not force UTF-8, discard bytes, or override an explicitly supplied charset. Stream-position restoration remains unchanged.

This is a narrow mitigation for sample-boundary splitting. It does not address other encoding-detection limitations, such as non-ASCII content appearing entirely beyond an ASCII-only sample.

Copilot AI 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.

🟢 Approval recommended

The implementation is bounded, preserves existing fallback behavior, and has comprehensive focused tests.

Pull request overview

Prevents charset detection from misclassifying valid UTF-8 when the 64 KiB sample splits a multibyte character.

Changes:

  • Completes trailing UTF-8 characters with bounded lookahead.
  • Adds boundary, invalid-input, stream-position, and explicit-charset tests.
File summaries
File Description
packages/markitdown/src/markitdown/_markitdown.py Safely completes UTF-8 charset samples.
packages/markitdown/tests/test_charset_sample.py Covers sampling behavior and regressions.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 0
  • Review effort level: Balanced

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@afourney
afourney merged commit 75e7114 into main Sep 11, 2026
24 checks passed
@afourney
afourney deleted the utf8_splitting branch September 11, 2026 21:35
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.

2 participants