Skip to content

PDF pages are read as laid out: tables, headings, columns and page furniture - #292

Merged
zlareb1 merged 8 commits into
mainfrom
dp-pdf-layout
Nov 6, 2026
Merged

zlareb1 merged 8 commits into
mainfrom
dp-pdf-layout

Conversation

@zlareb1

@zlareb1 zlareb1 commented Nov 6, 2026 •

Copy link
Copy Markdown
Member

Related: #289

Changelog category (leave one):

  • Improvement

Changelog entry (one or two sentences a user of Loom can read without the diff; the release pull request collects these into the release notes):

The reference extractor reads a PDF as it is laid out: its tables become typed tables and datasets with rows cited by page, headings come from the type when the file has no outline, two-column pages read in order, sideways pages read like any other, and running headers, footers and page numbers are left out of the text and listed.

Why

A PDF was read as its text layer only. A table came out as loose words with nothing to say which cell was which, so it could not become a dataset and a hit on a row did not carry its header; a PDF with no outline (most exported docs and decks) had no headings, so its chunks had no section path; and running headers and page numbers were indexed on every page. On the document-parsing bench (#289) the previous extractor scored 0 on PDF table cells and 0.15 on PDF headings on the held-out split.

Design

extractor/loom_extractor/pdflayout.py reads a page from the positions pypdf reports for each run of text it shows (extract_text(visitor_text=...), its public interface) and the lines the page draws (its own walk of the content stream and form XObjects). No new dependency.

  • Widths: from the font's /Widths, a CID font's /W through its ToUnicode map (inverted), or Adobe's metrics for the standard fonts. pypdf reports each piece of one text operator at the operator's start, so a piece with the same matrices as the one before goes on from its end (a ligature drawn from a second font no longer lands at the line's start).
  • Lines and segments: runs on one baseline, cut at a gap wider than the font size; where the geometry joins two runs into a word the page's text layer does not have but has both halves of, they are parted.
  • Turn: a page is laid out in the direction most of its text runs, so /Rotate or turned content reads like any page; the rest is kept as paragraphs after it.
  • Columns, ruled grids, borderless bands, then furniture (a line no table holds, with a letter, recurring in the page's top or bottom tenth; a running header that sits apart from a borderless table below it is not one of its rows, while the table's own repeated header is; named by shape, with the number of pages, in attrs.furniture), paragraphs, lists, code and headings, as docs/architecture/document-extraction.md lists; a table that goes on over consecutive pages in the same columns is one table, its repeated header read once; an outline, when there is one, names the headings, under any heading set larger than all of them, on every page.
  • Tables are emitted through tabular.table like a sheet's: a typed table in artifact.tables and row blocks of column: value lines, cited by pages and rows, so knowledge.ingest makes each a dataset. A cell is typed only when its value reads back as shown (1.50 and a version 1.10 stay text).
  • Guard: a page whose laid-out reading holds less than 99.8% of its text layer's letters and digits (counted apart from spacing, which the text layer gets wrong where it runs words together), past 5,000 lines, or whose layout fails, is read as its text layer, with a warning. A layout mistake can cost structure, never text.

Alternatives: pypdf's layout mode prints a fixed-width page, which keeps alignment but loses cells that wrap and gives no fonts; its internals are a private module. A model-based layout reader is out of scope for the reference extractor (size, speed, determinism).

Scope and risk

extractor/loom_extractor/ (the PDF parser, PARSER_VERSION 2, and csvfile._cell made public as typed_cell), docs, tests. A PDF ingested again makes a new artifact with a different shape: furniture lines are no longer blocks, table text is row blocks instead of flattened lines, list items and code are list and code blocks. Text is kept by the guard. Bounded on hostile input: a page's lines (5,000), drawing operators (400,000; past it no rules) and rules (4,000; past it no ruled tables, the grid search by sorted position), ToUnicode maps and width arrays (1 MiB, 65,536 values written), the gutter's slots (a few thousand however wide the page), form depth 4; a long line and a long outline are read in linear time.

Compatibility, rollout and rollback

  • Wire: none; the artifact keeps contract version 1. A PDF artifact can now carry tables, and attrs.furniture and attrs.orientation (on a block of turned text). A build N-1 server reads N's artifacts (unknown attrs are kept as attrs; tables were already in the contract).
  • Operator steps: none. Existing PDFs keep their artifact until they are ingested or synced again.
  • Rollback: reverting returns PDFs to the text-layer reading on their next ingest; artifacts and datasets written meanwhile stay as written.

Tests

tests/unit/test_document_extraction_pdf_layout.py (PDFs built byte by byte): a table over three pages is one table and rows at the page margins stay rows; text drawn turned without /Rotate; outline headings under a larger title on a page read as its text layer too; a CID font's widths through its map; widths, a very wide page and a 3,000-entry outline in bounded time; a ruled table is a typed table with rows cited by page and section and passes parse_artifact; aligned columns without rules are a table and a wrapped cell stays one cell; a table over two pages is one table with its header read once; headings from size and weight, numbering ranking one style, a bold sentence not a heading; two columns read left then right; page furniture left out and named; a page shown turned is read in its text's direction; a list marker is not a column; the text-layer guard; a page past the rule bound; ToUnicode ranges and arrays. The existing PDF tests pass with the outline placed on a line inside a paragraph.

Document-parsing bench, held-out split (python -m bench.docparse run --out new.jsonl on each build, then score previous=... new=... --split heldout), paired difference with a 95% bootstrap interval:

Category (files) Heading F1 Table cell F1 Locators Text recall
All PDFs (20) 0.145 → 0.900 [+0.56, +0.95] 0.000 → 0.872 [+0.69, +1.00] 0.850 → 0.881 0.893 → 0.893
PDF exports (5) 0.380 → 1.000 0.000 → 0.991 0.953 → 0.990 1.000 → 1.000
Borderless tables (2) 0.000 → 1.000 0.000 → 1.000 0.938 → 1.000 1.000 → 1.000
Tables over pages (2) 0.000 → 1.000 0.000 → 1.000 0.955 → 1.000 1.000 → 1.000
Slide exports (3) 0.333 → 1.000 0.000 → 1.000 0.857 → 0.905 0.952 → 0.952
Rotated, two-column, typeset, public (6) 0.000 → 1.000 0.000 → 1.000 ≥ before 1.000 → 1.000

Heading level accuracy 0.33 → 1.00; silent loss unchanged (2, both text inside a picture on a slide, which needs OCR); scanned pages still fail as needing OCR. The dev split moved the same way (tables 0 → 0.888, headings 0.165 → 0.905). Time 0.07 → 0.18 s and peak memory 36 → 37 MB a file.

Fifteen real PDFs the corpus does not hold (three RFCs, twelve public conference decks, 11 to 194 pages): no failure, every page read as laid out (no guard fallback), every letter and digit of the text layer kept, 0.06 to 13.6 s a file (the 194-page RFC), at most 80 MB.

AI assistance in the code (leave one; writing this description with AI is fine):

  • AI-generated (an agent wrote most of the diff): Claude Code. I checked each rule against failing bench files on the dev split only, the guard against fifteen real PDFs, and the bounds against hostile inputs in the tests.

  • I can explain every line of this diff and how it interacts with the rest of Loom.

Checklist

  • make format-check, make lint, make typecheck and make test-unit pass; the integration suite passes if server behaviour changed.
  • Generated files are regenerated in this PR, not by hand: the SDK op tables, docs/reference/configuration.md and the docs/reference/ tables (python scripts/codegen/docs_reference.py), the reach manifest, schemas/schema.sql (make schema-dump) and the Codex plugin copies (python3 scripts/codegen/agent_plugin_sync.py).
  • A new migration is additive and numbered after the latest on main; no merged migration is edited.
  • A behaviour change to any file a plugin ships (hooks/, lib/, bin/, skills/, .mcp.json, the Codex plugin-defaults.json) bumps both plugin manifests' version, and no other open PR uses that version.
  • The frontend builds (npm --prefix frontend run build) and its tests pass, if frontend/ changed.
  • Docs under docs/ describe the system as it now is, and this description names any operator steps and wire changes.
  • Repository text has no dates, people, host names, internal namespaces, ticket or decision numbers; the history goes in this description (tests/unit/test_public_copy.py).
  • Commits are signed off (git commit -s), as CONTRIBUTING.md asks.

🤖 Generated with Claude Code

zlareb1 and others added 8 commits November 6, 2026 17:26
…l text, bounded reading

Furniture is decided once tables are found and needs a letter; a table is
carried over consecutive pages; a cell is typed only when its value reads back
as shown; a page past 5,000 lines or whose layout fails is read as its text
layer; outline placement is linear and shifted on every page; a font's widths,
the gutter's slots and the grid search are bounded; turned text keeps its
lines; furniture is named by shape.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…furniture kept out of tables

An entry after a whole-paragraph title is searched past its lines; a run drawn
twice at one place (either direction) is read once and counted by the guard; a
furniture candidate never joins a borderless table and is counted per page; a
borderless run holds at most 200 lines and a grid checks only the lines within
its height.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…xt repeated in another font is text

A furniture candidate at a borderless run's end stays a row unless it sits apart
from the run; a repeat of the same text at one place counts as a copy only in
the same font. The bounded-work tests count work instead of timing it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…w only when it reads as one

It stays when it fills every column (the table's repeated header) or opens a
row at the table's row spacing; a running header or a page number near the
table is furniture. The bands come from the table's other lines.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…r alone is never its row

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…number is never a table row

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
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