Skip to content

fix: Support text partitioning from ZipExtFile objects#4350

Open
dsolankii wants to merge 1 commit into
Unstructured-IO:mainfrom
dsolankii:fix-zip-ext-file-text-partition
Open

fix: Support text partitioning from ZipExtFile objects#4350
dsolankii wants to merge 1 commit into
Unstructured-IO:mainfrom
dsolankii:fix-zip-ext-file-text-partition

Conversation

@dsolankii

@dsolankii dsolankii commented May 11, 2026

Copy link
Copy Markdown

Fixes #4097.

Summary

This PR fixes partitioning for text files opened from ZIP archives using Python's zipfile module.

zipfile.ZipFile.open() returns a ZipExtFile object. ZipExtFile is a readable file-like object, but convert_to_bytes() previously only accepted a limited set of concrete file-like types. As a result, text files inside ZIP archives failed during encoding detection with an Invalid file-like object type error.

This change updates convert_to_bytes() to support generic readable file-like objects while preserving the original stream position when possible.

Changes

  • Added support for generic readable file-like objects in convert_to_bytes().
  • Preserved stream position for seekable file-like objects.
  • Added regression coverage for partitioning a text file from a ZipExtFile object.
  • Added unit coverage to verify that convert_to_bytes() preserves the position of seekable streams.

How to test

Run the targeted tests:

python -m pytest \
  test_unstructured/partition/test_auto.py::test_partition_txt_from_zip_ext_file \
  test_unstructured/partition/common/test_common.py::test_convert_to_bytes_preserves_seekable_file_position \
  -q

Expected result:

2 passed

Validation

I reproduced the issue locally before applying the fix. Partitioning a text file from a ZIP archive failed with:

ValueError: Invalid file-like object type

After the fix, the same flow successfully returns partitioned text elements.

@dsolankii

Copy link
Copy Markdown
Author

@cragwolfe Would you please review?

@dsolankii

Copy link
Copy Markdown
Author

@badGarnet Would you please review this PR , its been 2 week.

@ghost ghost requested review from badGarnet and qued May 26, 2026 19:30
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/a text file cannot be loaded from a ZipExtFile

1 participant