Skip to content

fix: handle encoding for text file checks - #707

Merged
MthwRobinson merged 2 commits into
mainfrom
fix/encoding-for-csv-detection
Jun 9, 2023
Merged

MthwRobinson merged 2 commits into
mainfrom
fix/encoding-for-csv-detection

Conversation

@MthwRobinson

Copy link
Copy Markdown
Contributor

Summary

Closes #705. Updates the text file checks for JSON and CSV structured in detect_filetype to handle documents with different encodings.

Testing

This code should now run without error

from unstructured.file_utils.filetype import _is_text_file_a_csv, _is_text_file_a_json

filename = "example-docs/fake-text-utf-32.txt"
_is_text_file_a_csv(filename=filename)
_is_text_file_a_json(filename=filename)

@MthwRobinson
MthwRobinson requested review from Coniferish and qued June 9, 2023 13:39

@qued qued 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.

LGTM! Errors on main when running the test code, no errors on this branch.

@MthwRobinson
MthwRobinson merged commit 0289ca3 into main Jun 9, 2023
@MthwRobinson
MthwRobinson deleted the fix/encoding-for-csv-detection branch June 9, 2023 15:08
2sumtech added a commit to 2sumtech/unstructured that referenced this pull request Aug 24, 2026
_TextFileDifferentiator._is_json parsed the raw byte stream with
json.load(), which auto-detects only the UTF-8/16/32 family, and caught
only json.JSONDecodeError. A plain-text file in any other character set
(e.g. cp1252/latin-1) whose first non-whitespace character is { or [ --
a log file or INI-style config -- raised UnicodeDecodeError out of
detect_filetype() and partition() instead of classifying as TXT.

Treat an undecodable payload as not-JSON after retrying with the
declared encoding, so an explicitly-declared legacy-encoded JSON file
still classifies as JSON. Regression of the encoding-aware detection
fixed in Unstructured-IO#707 (issue Unstructured-IO#705), reintroduced when detection was rewritten.
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.

bug/JSON and CSV detection for text/plain MIME types fails with non UTF-8 encodings

2 participants