fix: catch UnicodeDecodeError in _is_json to avoid crashing detect_filetype()
Problem
_TextFileDifferentiator._is_json calls json.load() directly on the raw file bytes when libmagic guesses a generic text/plain MIME type and the content starts with [/{:
try:
with self._ctx.open() as file:
json.load(file)
return True
except json.JSONDecodeError:
return False
If the bytes aren't valid UTF-8, json.load() raises UnicodeDecodeError (via its internal detect_encoding step) — a different exception type than json.JSONDecodeError, so it's not caught. It propagates straight out of the public detect_filetype() API instead of falling back to FileType.TXT, the behavior for any other content libmagic can't identify more specifically.
Repro
import io
from unstructured.file_utils.filetype import detect_filetype
data = b"{not json at all just braces \xe9 text} and more padding"
detect_filetype(file=io.BytesIO(data), metadata_file_path="filename.pdf")
# UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe9 in position 29: invalid continuation byte
Fix
Broaden the except clause to also catch UnicodeDecodeError.
Testing
- Added
test_it_does_not_crash_on_non_utf8_bracket_prefixed_text_with_no_extension, confirmingdetect_filetype()returnsFileType.TXTinstead of raising. Confirmed it fails against the pre-fix code and passes after. - Ran the full
test_unstructured/file_utils/suite (380 passed, 1 xfailed — no regressions). ruff check/ruff format --checkclean. Rebased on currentmain; noCHANGELOG.mdor version change, per the current contributing guide.
Disclosure: I used an AI coding assistant (Claude) to help identify this bug and draft the fix. I independently reproduced the crash before writing the regression test and ran the full local test suite before opening this PR.