Unstructured-IO / Unstructured-IO/unstructured

bug/a text file cannot be loaded from a ZipExtFile

Open
#4,097 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
HTML
Stars
15.5k
Forks
1.3k
Avg merge
4d 2h
Merged PRs (30d)
13

Description

Describe the bug

I have some code which takes uploaded files and passes them into the langchain UnstructuredLoader, which as you can see from my error log down below is calling Unstructured's partition function. When the uploaded file is a zip file I'm using Python's built-in zipfile module to load the contents into file-like objects. I've tried with several different text files with the same results. In all cases I'm passing a file-like object into Unstructured.

  • Uploading the text file directly: success
  • Uploading a zip file containing PDF, DOCX, PNG etc.: success
  • Uploading a zip file containing the working text file:
2025-09-23 16:50:09   File "/shabti/.venv/lib/python3.12/site-packages/unstructured/partition/auto.py", line 292, in partition
2025-09-23 16:50:09     elements = partition(filename=filename, file=file, **partitioning_kwargs)
2025-09-23 16:50:09                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2025-09-23 16:50:09   File "/shabti/.venv/lib/python3.12/site-packages/unstructured/partition/common/metadata.py", line 162, in wrapper
2025-09-23 16:50:09     elements = func(*args, **kwargs)
2025-09-23 16:50:09                ^^^^^^^^^^^^^^^^^^^^^
2025-09-23 16:50:09   File "/shabti/.venv/lib/python3.12/site-packages/unstructured/chunking/dispatch.py", line 74, in wrapper
2025-09-23 16:50:09     elements = func(*args, **kwargs)
2025-09-23 16:50:09                ^^^^^^^^^^^^^^^^^^^^^
2025-09-23 16:50:09   File "/shabti/.venv/lib/python3.12/site-packages/unstructured/partition/text.py", line 81, in partition_text
2025-09-23 16:50:09     encoding, file_text = read_txt_file(file=file, encoding=encoding)
2025-09-23 16:50:09                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2025-09-23 16:50:09   File "/shabti/.venv/lib/python3.12/site-packages/unstructured/file_utils/encoding.py", line 146, in read_txt_file
2025-09-23 16:50:09     formatted_encoding, file_text = detect_file_encoding(file=file)
2025-09-23 16:50:09                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2025-09-23 16:50:09   File "/shabti/.venv/lib/python3.12/site-packages/unstructured/file_utils/encoding.py", line 70, in detect_file_encoding
2025-09-23 16:50:09     byte_data = convert_to_bytes(file)
2025-09-23 16:50:09                 ^^^^^^^^^^^^^^^^^^^^^^
2025-09-23 16:50:09   File "/shabti/.venv/lib/python3.12/site-packages/unstructured/partition/common/common.py", line 386, in convert_to_bytes
2025-09-23 16:50:09     raise ValueError("Invalid file-like object type")
2025-09-23 16:50:09 ValueError: Invalid file-like object type

To Reproduce

with zipfile.ZipFile(file) as my_zip:
    for info in my_zip.infolist():
        loader = UnstructuredLoader(
            file=my_zip.open(info),
            strategy="auto",
            chunking_strategy="by_title",
            metadata_filename=info.filename,
        )
        pages = loader.load()

Expected behavior
The file is able to be loaded

Environment Info
Please run python scripts/collect_env.py and paste the output here.

I can't find where this collect_env.py is in my installation.

I created a Docker image based on astral/uv:python3.12-trixie-slim with unstructured[all-docs]>=0.18.14 in my Python dependencies. I have installed all the recommended system dependencies except libmagic as I am also having some issues with that.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with the shown zipfile and UnstructuredLoader example, then trace partition/text.py through file_utils/encoding.py and partition/common/common.py where ZipExtFile is rejected. Done means a text file opened from a ZIP loads successfully while the existing direct-file and other document cases continue to work.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.