You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
partition_csv() raises UnicodeDecodeError on UTF-16-LE files even with correct encoding argument #4443
Delimiter sniffing in partition_csv() decodes its sample line-by-line (unstructured/partition/csv.py, _CsvPartitioningContext.delimiter). readlines() splits the raw bytes on 0x0A, which in UTF-16 lands in the middle of a code unit, and only the first fragment carries the BOM. For UTF-16-LE — what str.encode("utf-16") and Excel's "Unicode Text" export produce — the first fragment is odd-length and decoding raises UnicodeDecodeError: 'utf-16-le' codec can't decode byte 0x0a ... truncated data, even though the caller passed the correct encoding and pd.read_csv handles the same bytes fine.
UTF-16-BE doesn't raise, but silently decodes every line after the first as mojibake, so the sniffer sees corrupted data. (The existing stanley-cups-utf-16.csv example doc is BE, which is why tests never hit this.)
To Reproduce
importiofromunstructured.partition.csvimportpartition_csvdata="name,age\nAlice,30\nBob,25\n".encode("utf-16")
partition_csv(file=io.BytesIO(data), encoding="utf-16")
# UnicodeDecodeError: 'utf-16-le' codec can't decode byte 0x0a in position 18: truncated data
Fails identically with filename=. pd.read_csv(io.BytesIO(data), encoding="utf-16") parses the same bytes correctly.
Expected behavior
The file partitions successfully when the correct encoding is supplied (and delimiter sniffing sees correctly decoded text for both UTF-16 variants).
Environment
unstructured 0.26.3, Python 3.11, reproduced on current main.
Activity