fix: Accept non-seekable file-like objects in partition()

SH
2d ago
+287-2

Summary

partition() raised io.UnsupportedOperation: File or stream is not seekable when given a readable stream that cannot seek, such as the read end of os.pipe(). Passing content_type="text/plain" did not avoid it, because file-type detection rewinds the stream before the content type is used.

A non-seekable file is now copied once, before detection, and partitioning runs on that copy. Seekable inputs (regular files, BytesIO, and objects without a seekable() method) are passed through unchanged and are not copied.

Guarding each file.seek(0) in filetype.py was considered and rejected. Detection reads the first bytes before it rewinds, so a guard alone would leave those bytes consumed and partitioning would start mid-document without an error. Copying the stream once keeps detection and partitioning on the same bytes.

Context: #4643 and #4649 fixed the same kind of failure for partition_json() and partition_ndjson(). The change here covers the generic partition() path.

Fixes #4677

What changed

  • The copy reads the stream in 1 MiB chunks until end-of-file. The caller's stream is read to EOF and is left open.
  • Before anything is read, the same exactly_one(file, filename, url) check that partition() runs is applied, so passing a non-seekable file together with a positional or keyword filename, or with url, raises the existing ValueError without reading or closing the stream. An open pipe can't block before that error.
  • A read that returns None (a non-blocking stream with no data available right now) raises BlockingIOError instead of partitioning a partial document. An error after partial input propagates.
  • The copy is a tempfile.SpooledTemporaryFile with a 64 MiB in-memory threshold, the same threshold partition_tsv uses. Larger input is written to a temporary file until the copy is closed. The copy is closed on success and on failure.
  • The extension of the original stream's name (for example .ndjson) is given to file-type detection only, so a named non-seekable stream is detected the same way as a named seekable one. The output filename metadata does not change.
  • _audio_suffix in partition/audio.py uses a stream name only when it is a string or a path. A spooled copy that has rolled over to disk gets an integer file descriptor as its name, which used to raise TypeError for large piped audio. Such a name now falls back to .wav.

The first version of this change read the stream into an io.BytesIO. The copy described above replaces that.

Notes for reviewers

  • Memory: up to 64 MiB of a non-seekable input stays in memory. Larger input goes to a temporary file in the system temp directory until the copy is closed.
  • Text-mode streams, such as sys.stdin, are still not supported. Pass the binary buffer (sys.stdin.buffer). The documented contract is a binary stream, and this change does not alter it.
  • The copy is made by a small private decorator on partition(), which passes the name hint on through a private keyword argument (_detection_file_path). The public signature does not change. For the early source check, the decorator binds the call with inspect.signature(partition).bind(...), the same way the telemetry decorator does, so positional arguments are not handled by hand.
  • The name hint is used for type detection only. It does not become the output filename, and the audio temp file for a named non-seekable stream gets a .wav suffix unless metadata_filename has an extension. For a pipe with no name, pass metadata_filename when the extension matters.
  • The seek calls in unstructured/file_utils/filetype.py are unchanged, and detect_filetype() called directly is unchanged.
  • The change does not touch CHANGELOG.md or unstructured/__version__.py.

Validation

Run on the branch head (commit 0c4451a0, on top of upstream/main):

  • uv run --no-sync pytest test_unstructured/partition/test_auto_nonseekable_streams.py test_unstructured/file_utils/test_filetype.py test_unstructured/partition/test_json_nonseekable_streams.py test_unstructured/partition/test_ndjson.py test_unstructured/partition/test_audio.py::test_audio_suffix -n auto: 384 passed.
  • uv run --no-sync pytest test_unstructured/partition/test_auto_nonseekable_streams.py::test_partition_checks_sources_before_reading_a_nonseekable_stream -v: 3 passed. On the previous head (3be5beb1, without the early check) all 3 cases fail, because the stream has already been read. On upstream/main the test passes, since nothing reads the stream there before partition()'s own check.
  • uv run --no-sync ruff check .: pass.
  • uv run --no-sync ruff format --check .: pass.

The new test file covers: conflicting file/filename/url raise before a non-seekable stream is read or closed; a non-blocking pipe with a partial prefix raises BlockingIOError; a read error after partial input propagates and the copy is closed; input larger than a lowered threshold rolls over to disk and is closed afterwards; the copy is closed when partitioning fails; a named non-seekable NDJSON stream gives the same elements as the named seekable one, with and without content_type; a seekable file is passed through without a copy.

Not run:

  • test_unstructured/partition/test_auto.py was not run. It can't be collected in my local environment because the pdf and image extras are not installed (pdf2image is missing), so please rely on CI for it.
  • The full make test suite was not run locally.
  • The make check target was not run as a single command. Its ruff lint and format parts were run directly, as above. No type checker was run.

Checklist

  • The PR title is a user-facing conventional commit (fix:), with no breaking-change marker.
  • Category label bug applied, or a maintainer asked to apply it.
  • For breaking changes, use ! in the title and explain impact and migration above. (Not a breaking change.)