Skip to content

The reference extractor parses PDF, DOCX and PPTX - #257

Merged
zlareb1 merged 10 commits into
mainfrom
feat/extractor-office
Nov 4, 2026
Merged

zlareb1 merged 10 commits into
mainfrom
feat/extractor-office

Conversation

@zlareb1

@zlareb1 zlareb1 commented Nov 4, 2026 •

Copy link
Copy Markdown
Member

Related: #157

Changelog category (leave one):

  • New Feature

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 parses PDF, DOCX and PPTX files: knowledge.ingest publishes them, a PDF page or a deck slide cited by its page span (locator.page_start/page_end), and the document-source sync converts the PDFs and decks its folders hold.

Why

The issue asks for office documents and PDFs to be ingested server side and cited back to their place in the original. Before this change the reference extractor recognised these formats and refused them as format_unavailable. This PR adds the parsers inside the extractor service. The Loom server still parses nothing: it only reads the artifact and its new locator fields.

Design

  • Where the parsing runs. In the extractor service, in the sandboxed child (no sockets, rlimits, non-dumpable), like every other format.
  • PDF: pypdf (pure Python), reading the text layer.
    • Each page's text is one or more paragraph blocks, split at blank lines, each with page_start/page_end.
    • The outline (bookmarks) supplies the headings. An entry becomes a heading block on its page, at the line that holds its title, or at the start of the page when no line does.
    • A page with no text that draws an image (an image object, one in a form, or one inline in its content stream, found by reading its operators, with no image decoded) is a failed unit, because OCR is not run. A scanned PDF is therefore not complete and publishes only with allow_partial.
    • A PDF that opens only with a password is unsupported_format.
    • cryptography is added so that a PDF encrypted with AES under an owner password alone (a common case) opens.
  • DOCX and PPTX: the standard library's zipfile and expat, not python-docx or python-pptx.
    • Those libraries would bring lxml and Pillow, two large C parsers, into the process that handles hostile files.
    • The text both formats need is a few XML elements. On the labelled fixtures, the standard-library readers give exactly what python-docx and python-pptx give (table below).
    • A part is read up to 256 MiB uncompressed.
    • A part that declares a DOCTYPE is refused as corrupt. The check runs in expat's handler, so it holds for UTF-16 parts too, and no entity is ever expanded.
  • DOCX reading.
    • Headings come from the Heading N / Title styles, or from an outline level set on the paragraph or found along its style's basedOn chain.
    • Numbered or List-style paragraphs become one list block per run.
    • Inserted text is read; a tracked deletion (its runs, with their tabs, breaks and pictures, and deleted rows and cells), and moved text at its old place (a tracked move keeps its text in ordinary runs at both places), are not. Content controls are read in place, and alternate content is read once.
    • Tables become one block each. Footnotes and endnotes are read after the body.
    • Headers, footers and comments are not read.
    • DOCX has no pages, so its blocks carry no locator, and a citation names their section path.
  • PPTX reading.
    • Slides are read in the presentation's order.
    • The title is a heading. Each other text shape is a block, with grouped shapes read in place.
    • Tables and speaker notes (attrs.notes) are blocks.
    • Hidden slides are read and marked attrs.hidden.
    • A slide of pictures alone is a failed unit.
  • Tables stay paragraph blocks. A table's text is one row per line with its cells joined by |, and attrs.table carries its shape. The contract's typed Table and the typed snapshots belong to the datasets work (XLSX, CSV, Parquet), so this PR adds no new block type.
  • Locator. page_start/page_end (1-based, both 0 or a range) is added to the extractor's and the server's Locator. The server validates it as a range, and a chunk cut from several blocks spans their pages.
  • Alternatives considered.
    • A layout-model PDF parser: measured on the same fixtures, it adds headings, table cells and OCR on PDFs, but it costs about 6 GB with its model runtime and lost a rotated page's text silently while reporting success. It is left for an optional second extractor image behind the same contract, in its own change.
    • python-docx and python-pptx: same output, more native code in the sandbox.
    • A new table block type: rejected, because a server one build older refuses a block type it does not know.

Scope and risk

  • extractor/loom_extractor/:
    • pdffile.py, docxfile.py, pptxfile.py and ooxml.py, the package reader;
    • source.py routes the three formats and refuses an encoding for them (invalid_option);
    • service.py declares them with no options;
    • child.py quiets pypdf's repair warnings;
    • the heading-stack accumulator moves from text.py to artifact.py (Blocks), so the new parsers share it; it gains heading attrs (a hidden slide's title) and close_sections (a slide with no title).
  • server/knowledge/domain/extract/: Locator.page_start/page_end, validated in validate.py and merged in structural.py. No server module imports pypdf, zipfile or xml in the knowledge plane (tests/unit/test_package_contracts.py).
  • Dependencies: pypdf>=6.21,<7 and cryptography>=50,<51 in extractor/pyproject.toml and in the root extractor extra, with both locks at one version of each package (the root lock keeps its format revision; the CI and image uv versions accept both).
  • Docs: docs/architecture/document-extraction.md (the formats table, the PDF and DOCX/PPTX sections, the locator, the dependencies), docs/reference/extractor-contract.md (locator fields, coverage units), docs/features/knowledge.md, extractor/README.md.

Compatibility, rollout and rollback

  • Rolling upgrade.
    • A server one build older drops the page_start/page_end fields it does not know, as the contract allows. It therefore publishes a new extractor's PDF and PPTX artifacts without page spans.
    • No block type is added, so no artifact is refused.
    • A new server reading an older extractor sees these formats undeclared, as before.
  • Wire. No op field changes. A hit's locator may now carry page_start/page_end; the knowledge.search hit field comment, the assistant toolset's description and the feature page name them.
  • Operator steps. None. The extractor image grows by the two wheels.
  • Rollback. Revert. Documents already published keep their chunks.

Tests

  • What fails one unit, not the file.
    • PDF. A page pypdf cannot read fails that page, and a failed page's outline headings still open their sections. A lone surrogate from a font's character map reads as U+FFFD, with a warning naming the page, so the server's Unicode check does not refuse the whole artifact.
    • OOXML parts. A part that is damaged (a bad checksum, a truncated stream, an encrypted entry), over the bound or not XML fails its slide, the notes or the styles. A part compressed with a method other than stored or deflate is refused, so the bound holds whatever the method.
    • Content not read. A DOCX altChunk is a failed unit, as is a document of pictures with no text, and a slide whose charts, diagrams or embedded objects carry everything it shows.
    • What is read in place. DOCX content controls and custom XML in the body, rows and cells; PPTX shapes in alternate content, through their first choice.
  • Unit, tests/unit/test_document_extraction_office.py. Files are built byte by byte: the PDFs in tests/fixtures/office_files.py (shared with the integration test), the DOCX and PPTX packages in the test file itself. The cases:
    • DOCX headings (style, outline level, basedOn), lists, tracked changes (a move read once; a deleted run's hyphen and tab, a deleted row and a deleted cell not read), a deleted picture not counted, a content control, alternate content, a table and footnotes, in order;
    • PPTX deck order differing from part names, title, group shape, table, notes, a hidden slide, a picture-only slide failing;
    • PDF outline headings at their lines and at a page's start, page locators, an image page, a form-XObject image page, an inline-image page and a form drawing its image inline failing for OCR;
    • an AES PDF with an owner password alone read, an encrypted one unsupported_format, a broken one corrupt;
    • DOCTYPE refusal, in UTF-8 and UTF-16 parts;
    • the server reading page spans and refusing a bad one;
    • a chunk spanning pages;
    • a lone surrogate from a broken character map, and a form named many times being searched once;
    • DOCX rows and cells in content controls and custom XML, an altChunk, a pictures-only document, a picture beside text (no failure), a non-decimal outline level and one of 5,000 digits (no heading, nothing else lost), and outline level 9 (body text) overriding a heading style on a style and on a paragraph;
    • PPTX alternate content read once, and a chart-only slide failing;
    • a slide part compressed with bzip2, one that is not XML, one over the bound, and one with a bad checksum, each failing that slide alone.
  • Integration. The publishing tests skip unless a configured extractor declares SQL, TOML and PDF (extractors.available_formats), and the mislabelled upload expects format_mismatch exactly when one declares pdf. The inspect test's and the namespace.tune document test's unavailable-format cases use .xlsx, which the reference extractor still does not parse; its job is run by itself, as the ingest tests' are.
  • Updated tests. Tests that used pdf as a format the reference does not take now use csv (a truncated PDF is now corrupt). The capabilities test and the quickstart smoke (scripts/gates/docker_extractor_smoke.py) list the three formats. The pin test covers every parser dependency.
  • Integration, tests/integration/test_knowledge_ingest_live.py.
    • A PDF with an image page fails incomplete_extraction, then publishes partial with allow_partial, its chunk citing page 1 under its outline heading.
    • The mislabelled .pdf upload now fails format_mismatch, because the reference extractor reads the bytes; with no extractor routed for pdf it fails format_unavailable.
  • Local runs against a migrated ClickHouse, the server and the reference extractor:
    • the ingest, inspect-ingest, tune-documents and queue files: 12 passed;
    • -k "knowledge or document or extract or source or tune", with structural search merged: 96 passed, 53 skipped;
    • the full unit suite: 26,734 passed; the one failure is test_capabilities_nested_too_deep_are_unavailable_and_declare_nothing, which fails on main too under Python 3.14 (its JSON decoder reports deep nesting differently); CI runs 3.12.
  • Measured on the labelled fixtures from the parser evaluation (two PDF pages with headings and a grid table, a two-column page, a rotated page, a 70-row table over two pages, a scanned page, a DOCX, a deck):
File python-docx / python-pptx / pypdf (baseline) this extractor
native.pdf text 3/3, pages 3/3, headings 0/3, table cells 0/12 same
two_column.pdf text 6/6, order 5/5 same
rotated.pdf text 1/1 same
table_span.pdf table cells 0/213 (read as text) same
scanned.pdf text 0/3 text 0/3, page 1 failed: needs OCR (not reported complete)
policy.docx text 4/4, headings 3/3, table cells 6/6 same
deck.pptx 10/10 items on their slide same

PDF headings and table cells, and OCR, are what an optional layout-aware extractor would add.

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 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.
  • 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
zlareb1 force-pushed the feat/extractor-office branch 6 times, most recently from 523e5f1 to 0b63eeb Compare November 4, 2026 13:30
zlareb1 and others added 7 commits November 4, 2026 13:55
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
@zlareb1
zlareb1 force-pushed the feat/extractor-office branch from 0b63eeb to 50cff69 Compare November 4, 2026 14:02
zlareb1 and others added 3 commits November 4, 2026 14:27
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
@zlareb1
zlareb1 merged commit 9b27442 into main Nov 4, 2026
20 of 22 checks passed
@zlareb1
zlareb1 deleted the feat/extractor-office branch November 4, 2026 14:38
@pufit pufit mentioned this pull request Nov 4, 2026
9 tasks done
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