fix(json): accept readable streams that cannot seek

Closed
ZH
1mo ago
+35-2

Description

Both JSON partitioners successfully read valid bytes from a pipe, then fail at an unconditional seek(0). This prevents direct callers from parsing readable, non-seekable input streams.

Rewind only streams that report seekable(). Pipes remain consumed and caller-owned, while seekable inputs retain the existing rewind behavior.

Related Issues

Closes #4643

Module(s) Affected

  • unstructured/partition/json.py
  • unstructured/partition/ndjson.py
  • test_unstructured/partition/test_json_nonseekable_streams.py

Testing

Baseline main@910d9f41e70a17e561797106ef4b9096a8554254. Published commit 49b0b7fd5e618319ea1383c2c93c702b913b1b18. The published file tree matches the independently tested local commit 6d17ea919adc1bed256ffeca76d2d5df37eefcf7.

DO_NOT_TRACK=1 UNSTRUCTURED_DISABLE_NOTICE=1 python -m pytest test_unstructured/partition/test_json_nonseekable_streams.py -q

Before: 2 failed, 2 passed. After: 4 passed, repeated on the independent branch before publication. Tests exercise both public partitioners with real OS pipes, verify parsed content and caller ownership, and retain seekable-stream rewind controls.

Targeted Ruff 0.17.3 lint/format checks and git diff --check pass. Python 3.13.15 and pytest 9.2.0, macOS arm64.

An existing JSON/NDJSON subset also passed 34 tests with 136 deselected on the combined local validation branch. That combined result includes the separate NDJSON timestamp fix and is not presented as this independent branch's full suite.

Checklist

  • Conventional, user-facing fix title.
  • Focused regressions fail on baseline and pass with the fix.
  • Targeted lint, formatting and whitespace checks pass.
  • Full make test, make check and pre-commit checks completed.
  • Remote CI and maintainer review completed.
  • Maintainer applies the bug category label.

Additional Notes

Ready for review at the author's request. Full repository checks required by CONTRIBUTING.md remain unverified. Scope is the two explicit partitioners, without extending automatic file-type detection or other parsers. No production-incident or usage-frequency claim. No version bump or changelog edit. AI-assisted implementation and local verification.