fix: accept any readable file-like object in convert_to_bytes()#4275
Closed
weiguangli-o wants to merge 1 commit intoUnstructured-IO:mainfrom
Closed
fix: accept any readable file-like object in convert_to_bytes()#4275weiguangli-o wants to merge 1 commit intoUnstructured-IO:mainfrom
weiguangli-o wants to merge 1 commit intoUnstructured-IO:mainfrom
Conversation
convert_to_bytes() only accepted a hardcoded set of file types (BytesIO, BufferedReader, SpooledTemporaryFile, TextIOWrapper). Any other IO[bytes] type — such as zipfile.ZipExtFile returned by zipfile.ZipFile.open() — was rejected with "ValueError: Invalid file-like object type". Add a duck-typing fallback before raising: if the object has a .read() method, read it; if it also has .seek(), reset the cursor for the caller. This fixes the crash when partitioning text files loaded from zip archives and also covers other standard IO[bytes] types like GzipFile and tarfile.ExFileObject. Closes Unstructured-IO#4097
Author
|
Closing — shifting focus to AI Agent core projects. Thank you for your time! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #4097
Problem
When a user opens a file from inside a zip archive via
zipfile.ZipFile.open()and passes the resultingZipExtFileintopartition(), the call crashes with:This happens because
convert_to_bytes()only accepts a hardcoded whitelist of types (BytesIO,BufferedReader,SpooledTemporaryFile,TextIOWrapper). Any otherIO[bytes]implementation — includingZipExtFile— is immediately rejected.Fix
Added a duck-typing fallback before the final
raise ValueError(...): if the object has a.read()method, read it; if it also has.seek(), reset the cursor so the caller can re-read the file. This preserves all existing behavior for the whitelisted types while also coveringZipExtFile,GzipFile,tarfile.ExFileObject, and any other standardIO[bytes]implementation.Testing
Four new unit tests added in
test_unstructured/partition/common/test_common.py:it_reads_a_ZipExtFileZipExtFilefrom a zip archive is accepted and returns correct bytesit_resets_cursor_after_reading_a_ZipExtFileit_reads_a_generic_IO_bytes_with_read_method.read()is accepted (duck-typing)it_raises_on_a_non_readable_objectValueErrorTo verify locally: