fix: improve CSV encoding detection for non-UTF-8 files - #293
Open
li5435945-ship-it wants to merge 2 commits into
Open
fix: improve CSV encoding detection for non-UTF-8 files#293li5435945-ship-it wants to merge 2 commits into
li5435945-ship-it wants to merge 2 commits into
Conversation
added 2 commits
June 2, 2026 14:25
Fix UnicodeDecodeError when chardet misdetects Shift-JIS/CP932 files as UTF-8 due to ASCII-heavy headers. Changes: - Increase chardet sample size from 4KB to 64KB for better accuracy - Add encoding validation: verify detected encoding can decode the sample - Add fallback mechanism: try common encodings (cp932, shift_jis, gb18030, latin-1) - Add _retry_csv_with_fallback_encodings() for runtime retry on UnicodeDecodeError Fixes danny-avila#291
Add comprehensive tests for detect_file_encoding() to verify: - UTF-8 and UTF-8 BOM detection - UTF-16 LE detection - Shift-JIS detection with ASCII headers (issue danny-avila#291 scenario) - GB18030 detection with ASCII headers - Latin-1 fallback detection - CSVLoader integration test with Shift-JIS file These tests ensure the encoding detection fix works correctly for non-UTF-8 CSV files with ASCII-heavy headers.
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
CSVLoader fails with
UnicodeDecodeErrorfor non-UTF-8 CSV files (e.g., Shift-JIS / CP932 from Japanese Excel) when the file has a mostly-ASCII header. Thechardetdetection only inspects the first 4096 bytes, which contains too few non-ASCII characters to detect Shift-JIS reliably.Fixes #291
Root Cause
detect_file_encoding()reads only 4096 bytes. For CSV files with English column headers and Japanese data rows, chardet returns "utf-8" because the sample is mostly ASCII.Solution
Three-layer approach:
Changes
app/utils/document_loader.py: 81 insertions, 4 deletionsTesting
Tested with a Shift-JIS CSV file (English headers, Japanese data) that was previously misdetected as UTF-8.