Skip to content

fix(filetype): implement fallback encoding detection for file-like objects - #4435

Open
ujjwalredd wants to merge 3 commits into
Unstructured-IO:mainfrom
ujjwalredd:fix-file-like-object-encoding
Open

fix(filetype): implement fallback encoding detection for file-like objects#4435
ujjwalredd wants to merge 3 commits into
Unstructured-IO:mainfrom
ujjwalredd:fix-file-like-object-encoding

Conversation

@ujjwalredd

@ujjwalredd ujjwalredd commented Aug 12, 2026

Copy link
Copy Markdown

Resolves the TODO in filetype.py:text_head.

Previously, if a document was passed as a stream (e.g., BytesIO from an S3 bucket or API payload) and wasn't encoded in UTF-8, text_head would blindly decode it with errors="ignore". This caused silent data corruption and stripped characters, leading to downstream file classification failures.

Changes:

  • Added strict decoding for self._file_arg streams.
  • Implemented a UnicodeDecodeError fallback that routes the byte chunk through detect_file_encoding (matching the existing behavior for physical file paths).
  • Updated tests to verify correct character preservation for non-UTF-8 streams.

This hardens the ingestion pipeline for production environments where documents are predominantly handled as in-memory streams.

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.

All reported issues were addressed across 2 files

Shadow auto-approve: would not auto-approve because issues were found.

Re-trigger cubic

Comment thread unstructured/file_utils/filetype.py Outdated

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

All reported issues were addressed across 2 files (changes from recent commits).

Shadow auto-approve: would not auto-approve because issues were found.

Re-trigger cubic

Comment thread unstructured/file_utils/filetype.py

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

0 issues found across 1 file (changes from recent commits).

Shadow auto-approve: would auto-approve. Focused bug fix: text_head now falls back to detect_file_encoding when strict decoding of a file-like object fails, eliminating silent data loss; tests assert preserved characters. No rollout or tradeoff changes.

Re-trigger cubic

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.

1 participant