Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### Fixes

- **Update README.md**: readme-only changes; added a link to Unstructured Pipelines to the README. No library behavior changes.
- **Fix crash on non-UTF-8 bracket-prefixed text with no extension**: `detect_filetype()` called `json.load()` directly on the raw file bytes when probing whether `text/plain`-guessed content is actually JSON. If the content started with `[`/`{` but contained non-UTF-8 bytes, `json.load()` raised an uncaught `UnicodeDecodeError` (not the `json.JSONDecodeError` the code caught), crashing detection instead of falling back to `FileType.TXT`. Both exception types are now caught.

## 0.25.0

Expand Down
9 changes: 9 additions & 0 deletions test_unstructured/file_utils/test_filetype.py
Original file line number Diff line number Diff line change
Expand Up @@ -1342,6 +1342,15 @@ def test_json_content_type_is_disambiguated_for_ndjson():
assert predicted_type == FileType.NDJSON


def test_it_does_not_crash_on_non_utf8_bracket_prefixed_text_with_no_extension():
non_utf8_bytes = b"{not json at all just braces \xe9 text} and more padding to fill the head"

file_buffer = io.BytesIO(non_utf8_bytes)
predicted_type = detect_filetype(file=file_buffer)

assert predicted_type == FileType.TXT


def test_office_files_when_document_archive_has_non_standard_prefix():
predicted_type = detect_filetype(
file_path=input_path("file_type/test_document_from_office365.docx")
Expand Down
2 changes: 1 addition & 1 deletion unstructured/file_utils/filetype.py
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ def _is_json(self) -> bool:
with self._ctx.open() as file:
json.load(file)
return True
except json.JSONDecodeError:
except (json.JSONDecodeError, UnicodeDecodeError):
return False


Expand Down