Skip to content

fix: decode CSV delimiter-sniff sample as a single stream - #4444

Open
2sumtech wants to merge 1 commit into
Unstructured-IO:mainfrom
2sumtech:fix/csv-delimiter-utf16
Open

fix: decode CSV delimiter-sniff sample as a single stream#4444
2sumtech wants to merge 1 commit into
Unstructured-IO:mainfrom
2sumtech:fix/csv-delimiter-utf16

Conversation

@2sumtech

@2sumtech 2sumtech commented Aug 19, 2026

Copy link
Copy Markdown

Fixes #4443

Summary

partition_csv() crashed with UnicodeDecodeError: truncated data on UTF-16-LE files even when the correct encoding was passed, because _CsvPartitioningContext.delimiter decoded its sniff sample line-by-line: readlines() splits raw bytes on 0x0A, which lands mid-code-unit in UTF-16, and only the first fragment carries the BOM. UTF-16-BE didn't raise but silently fed the sniffer mojibake for every line after the first.

Fix

Keep the whole-line read semantics from #2998, but join the line fragments back into one byte stream and decode once with codecs.getincrementaldecoder(encoding)(...).decode(sample, final=False) — a character split at the read boundary is buffered instead of raising. Wrong-encoding inputs (e.g. latin-1 bytes decoded as utf-8) still raise exactly as before.

Testing

test_partition_csv_with_utf_16_le_encoding (end-to-end) and and_it_auto_detects_the_delimiter_for_a_UTF_16_encoded_CSV_file — both fail with the exact UnicodeDecodeError without the fix and pass with it. Full test_csv.py: 36 passed (includes the #2643 long-line regression, semicolon, single-column, and BE-utf-16 cases); test_tsv.py 15 passed; test_text.py 50 passed. ruff check + ruff format --check clean on touched files. CHANGELOG entry and version bump 0.26.3 → 0.26.4 included per repo convention.

Disclosure: prepared with AI assistance (Claude Code); I reviewed the change and take responsibility for it.

Review in cubic

@cubic-dev-ai cubic-dev-ai Bot 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.

No issues found across 4 files

Shadow auto-approve: would auto-approve. Bug fix for partition_csv() delimiter sniffing to decode sample bytes as a single stream, fixing UTF-16 crashes and mojibake. Includes focused tests, changelog entry, and version bump.

Re-trigger cubic

Delimiter sniffing in partition_csv() decoded its sample line-by-line:
readlines() splits the raw bytes on 0x0A, which in multi-byte encodings
like UTF-16 lands mid-code-unit, and only the first fragment carries the
BOM. A UTF-16-LE file raised 'UnicodeDecodeError: truncated data' even
when the correct encoding was passed; UTF-16-BE silently sniffed
mojibake. Decode the joined sample with an incremental decoder
(final=False) so a character split at the read boundary is buffered
rather than raising.
@2sumtech
2sumtech force-pushed the fix/csv-delimiter-utf16 branch from 4f24153 to 23d8a48 Compare August 24, 2026 21:44
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.

partition_csv() raises UnicodeDecodeError on UTF-16-LE files even with correct encoding argument

1 participant