Commit 56d2aaa
fix: raise on an empty HTTP-200 split chunk in NDJSON mode (0.46.1) (#348)
## The bug
In NDJSON elements-file mode, a split-PDF chunk that returned HTTP 200
with an empty body was logged and skipped, so the combined
`elements_file` was silently short by that chunk's pages while the call
still returned 200.
Nothing downstream could detect it: `combine_chunk_files_to_ndjson`
hands back only the combined path, and `split_pdf_allow_failed=False`
does not cover the case because an empty 200 counts as a *successful*
chunk. The buffered path fails outright on the same response
(`res.json()` raises `JSONDecodeError`), so enabling NDJSON mode
converted a hard failure into silent truncation.
## The fix
Recombination now raises `EmptyChunkResponseError` (a `ValueError`,
matching where the buffered path's `JSONDecodeError` lands) instead of
skipping.
Emptiness is judged against the chunk's own `Content-Type`, because the
two formats disagree about what an empty body means:
- **JSON** has no empty document — a chunk with no elements is `[]` — so
an empty body is malformed and raises.
- **`application/x-ndjson`** encodes zero records as zero lines, so an
empty body is well formed. It contributes nothing and does not fail the
partition; otherwise a split whose pages are blank would break once a
server honors the accept header.
- An **unknown or missing** media type is read as JSON. That is what the
deployed API returns, and guessing NDJSON would reinstate the silent
truncation.
`combine_chunk_files_to_ndjson` takes an optional `media_types` list,
positionally matched to `chunk_paths`; omitting it keeps the strict
reading. `_elements_from_task_responses` collects each chunk's
`Content-Type` before the cached branch overwrites the body with a
temp-file path — the header survives both cache branches, so the cached
case carries a real media type too.
## Tests
`_test_unstructured_client/unit/test_ndjson_elements_file.py`:
- empty JSON chunk raises, over 3 empty-ish bodies × `application/json`
/ unset / with-charset
- empty NDJSON chunk is zero records, over 3 bodies × 3 media-type
spellings (parameters, casing)
- every chunk empty yields an empty output rather than an error
- `ValueError` parity with the buffered path, and the strict default
when `media_types` is omitted
- hook level, both cache modes: an empty JSON chunk fails the operation
and leaves no partial, spilled, or combined file behind; an empty NDJSON
chunk is accepted with the surrounding chunks' elements intact
Mutating `_is_ndjson_media_type` to `return False` fails 12 of these,
including both hook-level cache-mode tests, so the coverage is
load-bearing.
271 unit + 64 contract tests pass; pylint 10/10 on both changed modules.
## Release
Bumped to 0.46.1 with CHANGELOG and RELEASES entries. Consumers that pin
`unstructured-client >=0.46.0` for NDJSON elements-file mode should
raise the floor to `>=0.46.1`.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>1 parent 6314d9a commit 56d2aaa
6 files changed
Lines changed: 267 additions & 21 deletions
File tree
- _test_unstructured_client/unit
- src/unstructured_client
- _hooks/custom
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
1 | 6 | | |
2 | 7 | | |
3 | 8 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1251 | 1251 | | |
1252 | 1252 | | |
1253 | 1253 | | |
| 1254 | + | |
| 1255 | + | |
| 1256 | + | |
| 1257 | + | |
| 1258 | + | |
| 1259 | + | |
| 1260 | + | |
| 1261 | + | |
| 1262 | + | |
| 1263 | + | |
Lines changed: 169 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
118 | 118 | | |
119 | 119 | | |
120 | 120 | | |
121 | | - | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
122 | 132 | | |
123 | 133 | | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
124 | 178 | | |
125 | | - | |
| 179 | + | |
126 | 180 | | |
127 | 181 | | |
128 | 182 | | |
129 | | - | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
130 | 186 | | |
131 | 187 | | |
132 | 188 | | |
133 | 189 | | |
134 | 190 | | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
135 | 207 | | |
136 | | - | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
137 | 213 | | |
138 | 214 | | |
139 | 215 | | |
| |||
380 | 456 | | |
381 | 457 | | |
382 | 458 | | |
383 | | - | |
| 459 | + | |
384 | 460 | | |
385 | | - | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
386 | 464 | | |
| 465 | + | |
| 466 | + | |
387 | 467 | | |
388 | | - | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
389 | 480 | | |
390 | 481 | | |
391 | 482 | | |
392 | | - | |
| 483 | + | |
393 | 484 | | |
394 | 485 | | |
395 | 486 | | |
| |||
546 | 637 | | |
547 | 638 | | |
548 | 639 | | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
| 660 | + | |
| 661 | + | |
| 662 | + | |
| 663 | + | |
| 664 | + | |
| 665 | + | |
| 666 | + | |
| 667 | + | |
| 668 | + | |
| 669 | + | |
| 670 | + | |
| 671 | + | |
| 672 | + | |
| 673 | + | |
| 674 | + | |
| 675 | + | |
| 676 | + | |
| 677 | + | |
| 678 | + | |
| 679 | + | |
| 680 | + | |
| 681 | + | |
| 682 | + | |
| 683 | + | |
| 684 | + | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
| 692 | + | |
| 693 | + | |
| 694 | + | |
| 695 | + | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
| 699 | + | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
| 706 | + | |
| 707 | + | |
| 708 | + | |
| 709 | + | |
549 | 710 | | |
550 | 711 | | |
551 | 712 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
290 | 290 | | |
291 | 291 | | |
292 | 292 | | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
293 | 315 | | |
294 | 316 | | |
295 | 317 | | |
| |||
301 | 323 | | |
302 | 324 | | |
303 | 325 | | |
304 | | - | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
305 | 331 | | |
306 | 332 | | |
307 | 333 | | |
| |||
316 | 342 | | |
317 | 343 | | |
318 | 344 | | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
319 | 352 | | |
320 | 353 | | |
321 | 354 | | |
| 355 | + | |
| 356 | + | |
322 | 357 | | |
323 | 358 | | |
324 | 359 | | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
325 | 363 | | |
326 | 364 | | |
327 | 365 | | |
328 | | - | |
| 366 | + | |
| 367 | + | |
329 | 368 | | |
330 | 369 | | |
331 | 370 | | |
332 | | - | |
333 | | - | |
334 | | - | |
335 | | - | |
336 | | - | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
337 | 386 | | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
338 | 395 | | |
339 | | - | |
340 | 396 | | |
341 | 397 | | |
342 | 398 | | |
| |||
0 commit comments