Skip to content

fix: file-type detection crash on non-UTF-8 text starting with "{" or "[" - #4447

Open
2sumtech wants to merge 1 commit into
Unstructured-IO:mainfrom
2sumtech:fix/json-detect-unicode
Open

fix: file-type detection crash on non-UTF-8 text starting with "{" or "["#4447
2sumtech wants to merge 1 commit into
Unstructured-IO:mainfrom
2sumtech:fix/json-detect-unicode

Conversation

@2sumtech

@2sumtech 2sumtech commented Aug 19, 2026

Copy link
Copy Markdown

Fixes #4446

Summary

detect_filetype() and partition() raised UnicodeDecodeError on any non-UTF-8 plain-text file (cp1252, latin-1, …) whose first non-whitespace character is { or [ — log files with [timestamp] lines, INI-style configs, or legacy-encoded JSON on systems where libmagic reports it as text/plain. _TextFileDifferentiator._is_json parses the raw byte stream with json.load(), which auto-detects only the UTF-8/16/32 family (the JSON interchange encodings, RFC 8259), and caught only json.JSONDecodeError.

Fix

Keep the json.load() raw-stream parse first (preserving JSON-spec UTF-16/32 auto-detection exactly), and on UnicodeDecodeError/UnicodeError retry once decoding with the declared encoding — so an explicitly-declared legacy-encoded JSON file still classifies as JSON — otherwise classify as not-JSON instead of raising. This is a regression of the encoding-aware detection from #707 (issue #705), reintroduced when detection was rewritten into the _TextFileDifferentiator architecture.

Testing

Two new parametrized _is_json cases (cp1252 JSON-lookalike, latin-1 INI) plus a file-path test and a declared-encoding-JSON test — all four fail with the exact UnicodeDecodeError without the fix and pass with it. Full test_filetype.py: 277 passed. ruff check + ruff format --check clean on touched files. CHANGELOG entry and version bump (0.26.5 — note: open PR #4444 claims 0.26.4; whichever merges second has a trivial adjacent-line CHANGELOG/version conflict, happy to renumber) 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. Fixes a crash in file-type detection when non-UTF-8 text starts with '{' or '['. Adds tests for the fix. No tradeoffs requiring human sign-off.

Re-trigger cubic

_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.

detect_filetype()/partition() raise UnicodeDecodeError on non-UTF-8 text files that start with "{" or "["

1 participant