Skip to content

Studio: skip pymupdf4llm's table search on PDF pages that cannot hold a lined table - #16280

Merged
danielhanchen merged 3 commits into
mainfrom
perf/r15-pdf-tables
Nov 1, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
perf/r15-pdf-tables

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 31, 2026 •

Copy link
Copy Markdown
Member

What changes

PDF ingestion spends most of its time in pymupdf4llm, and most of that in page.find_tables(strategy="lines_strict"), which runs on every page and first builds a Python dict for every character on the page. On a text-only book page that is 77% of the parse.

lines_strict only builds table edges from vector line art: the line, rect and quad items of every path that is not a large fill, plus the border of each cluster of those paths. pymupdf4llm then drops any table with fewer than 2 rows or 2 columns. A table it keeps has cells at 2 or more distinct x0 and 2 or more distinct tops, so it needs at least 3 vertical and 3 horizontal edges.

_no_lined_table_possible(page) reads page.get_cdrawings() and counts an upper bound on those edges (each candidate line counted once for every orientation it could take). The cluster step is copied from make_edges so the cluster boxes match exactly. If either count is below 3, the page goes through to_markdown(..., table_strategy=None); every other page keeps the full search.

  • The existing calls are kept, including the scan / non-scan split. Each call is split into a "search" half and a "no search" half. Both halves share one IdentifyHeaders built at the point the single call built it, and the document is baked first under the same condition to_markdown uses.
  • Rotated pages, pages with more than 1000 graphic rects, and any error inside the check keep the full search.
  • The guard only runs on the legacy pymupdf4llm path, with table_strategy defaulting to lines_strict, PyMuPDF 1.27.2.3 or 1.28.2 (the two pinned versions), and no layout analyzer. In every other case the old code path runs unchanged.

Results base vs head

55 public PDFs, 3590 pages: 20 arXiv papers, 6 Gutenberg books, 12 government reports, 7 IRS forms, 6 slide decks, 4 scans, plus rotated and annotated variants. Each file and arm ran in its own process, 3 rounds, with the arm order alternating each round. Parse time:

category files pages base (s) head (s) total speedup median file speedup
arxiv 20 614 138.7 116.2 1.19x 1.30x
book 6 1196 62.7 21.0 2.98x 3.25x
form 7 36 16.5 14.9 1.11x 1.06x
gov 12 873 280.2 240.5 1.17x 1.19x
scan 4 448 19.6 12.1 1.62x 1.59x
slides 6 423 10.5 10.1 1.05x 1.04x
all 55 3590 528.3 414.8 1.27x 1.24x

Per-round totals: 527.8 / 534.7 / 526.6 s on base and 412.7 / 419.8 / 413.3 s on head, so 21.5% to 21.8% saved in every round. The slowest case is a vector-heavy UN report at 0.97x (48.0 s to 49.5 s), where reading the drawings costs time and almost no page can be skipped.

Output identity:

  • With pymupdf4llm's in_bbox_using_cache disabled in both arms, all 55 files are byte-identical in page texts, page fields, chunks and highlight regions. That cache is keyed on id(), so a freed rect's id can give a stale hit.
  • Without that patch, 48 of 55 files are identical. The other 7 differ on 16 pages, only in blank lines or paragraph order. Base already gives different outputs for those pages: one IRS page returned 4 distinct outputs from 20 identical to_markdown calls in one process. This is existing upstream behavior and this PR does not change it.
  • PyMuPDF 1.28.2 (the win-arm64 pin, run on Linux): a 12-file subset is identical and the new tests pass.

Tests

  • New tests/test_rag_pdf_table_guard.py (7 tests):
    • Markdown is identical with and without the guard on generated PDFs: a lined table, an unlined aligned table, a footnote rule, a tinted page, a rotated table, a boxed note, and a form plus annotations.
    • A find_tables call-count check. It fails on base, where the search runs on every page.
    • A soundness check on 80 random line-art pages: every page the guard skips has no kept table.
    • The guard stays off for other Markdown backends.
  • Independent check of the bound: 24,000 random line-art pages (jitter around the snap and join tolerances, thin fill rules, quads, polylines, dashes, circles, cropbox offsets) gave 921 skipped pages, none with a kept lines_strict table. Ten adversarial PDFs (a table from 2 rects plus lines, thin fill rules, touching cell fills, dashed and segmented lines, rejected 1x3 and 3x1 tables, a table off the page edge, a cropbox through a table, a mixed document with rotations, annotations, a widget and a scan page) and 15 real PDFs give identical Markdown on base and head.
  • The RAG suites and the data recipe seed suite give the same failing IDs on base and head. Base has 1363 passed and 10 failed; head has 1370 passed and the same 10 failed. Those 10 are office-format tests that fail on base too.

Not covered

  • Cross-platform CI: test_rag_pdf_table_guard.py 7/7 on ubuntu-latest, macos-15 (arm64) and windows-latest. The windows-11-arm leg could not install the Studio requirements (pyarrow has no win_arm64 wheel and fails to build from source), so it ran no tests.
  • PyMuPDF 1.28.2 was run on Linux only (12-file subset identical, new tests pass). Its table.py is pure Python and unchanged in every part the bound relies on (make_edges / clean_graphics, edge merging, intersections, row and column counts); it was not run on a real win-arm64 machine.
  • If make_chars throws inside find_tables on a page the guard skips, base sent the whole document to plain text. Head now keeps Markdown for that document. None of the corpus pages hit this.
  • No parse cache and no page parallelism. Each would need its own identity and spawn-safety work.

… a lined table

find_tables(strategy="lines_strict") only builds edges from vector line art, and pymupdf4llm keeps only tables with 2+ rows and 2+ columns, which need 3+ vertical and 3+ horizontal edges. Pages whose drawings bound those counts below 3 now go through to_markdown(table_strategy=None); everything else keeps the full search. Same Markdown, 21.5% less parse time on a 55-PDF corpus (books 3x).
danielhanchen added a commit that referenced this pull request Oct 31, 2026
…G imports (#16284)

`Document upload compatibility` (`Uploads (macos-latest)`, `Uploads
(windows-latest)`) has failed on every RAG PR since #16145, for example
#16280 now:

```
ERROR collecting studio/backend/tests/test_rag_vec_ann.py
studio/backend/tests/test_rag_vec_ann.py:13: in <module>
    import usearch  # noqa: F401
E   ModuleNotFoundError: No module named 'usearch'
ERROR collecting studio/backend/tests/test_rag_vec_cache_ann.py
```

The workflow's run history: `perf/rag-hnsw-tier` (#16145 itself),
`perf/r14-chat-search`, `dh/fix-engine-port-blocks-past-40-workers` and
`perf/r14-rag-ingest` all failed there, while the `flows-*` branches it
ran on passed.

### Why

#16145 pinned `usearch==2.26.6` in
`studio/backend/requirements/studio.txt` and the studio extra. Under
`CI`, its ANN tests import usearch unconditionally, by design: a missing
wheel where `studio.txt` was installed is a packaging failure, not a
skip. `tests/studio/rag_upload_ci.py` runs every
`studio/backend/tests/test_rag_*.py`, but in a venv built only from its
own pinned `tests/studio/rag_upload_requirements.txt`, and that list
never got usearch. The tests are right. The harness's requirements fell
behind.

### Change

- Pin `usearch==2.26.6` in `tests/studio/rag_upload_requirements.txt`,
matching `studio.txt`.
- New `tests/studio/test_rag_upload_requirements_cover_ci_imports.py`.
It reads each `test_rag_*.py` for `import`s made only under a `CI`
check, and requires each module to be pinned in
`rag_upload_requirements.txt` at the same version as `studio.txt`. It
has a control that the scan finds today's `usearch` import, so the
reader cannot pass by seeing nothing.

### Verification

- The new tests pass.
- Sabotage: dropping the usearch pin fails with `['test_rag_vec_ann.py:
usearch', 'test_rag_vec_cache_ann.py: usearch']`. Pinning `2.26.5` fails
with `usearch: upload 2.26.5 vs studio.txt 2.26.6`.
- The workflow's path filter includes `tests/studio/rag_upload*`, so
`Document upload compatibility` runs on this PR on macOS and Windows.
That run is the end-to-end check.
@danielhanchen

Copy link
Copy Markdown
Member Author

The three Uploads reds here are not from this change. The document upload harness built its venv without usearch, so test_rag_vec_ann.py and test_rag_vec_cache_ann.py failed collection on every RAG PR. #16284 pinned it and has merged. A re-run of Document upload compatibility on this branch should come back green.

@danielhanchen
danielhanchen marked this pull request as ready for review November 1, 2026 09:13
@danielhanchen
danielhanchen merged commit e6d4f3a into main Nov 1, 2026
@danielhanchen
danielhanchen deleted the perf/r15-pdf-tables branch November 1, 2026 09:13
@danielhanchen danielhanchen self-assigned this Nov 1, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 1, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-11-01T09:13:24.062566Z
We couldn’t complete this review. Comment @codex review to try again.
77b8c59 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🚀

Reviewed commit: 3e45a2d095

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant