Skip to content

Read the PDFs with PyMuPDF and PyPDF directly, and pin every package - #11

Merged
aymanazeem merged 6 commits into
mainfrom
fix/pdf-readers-and-lock
Dec 1, 2026
Merged

aymanazeem merged 6 commits into
mainfrom
fix/pdf-readers-and-lock

Conversation

@aymanazeem

@aymanazeem aymanazeem commented Dec 1, 2026 •

Copy link
Copy Markdown
Owner

Problem

  • The two PDF loaders came from langchain-community, which LangChain retired in May 2026 and no longer actively maintains. Every make embed printed a warning saying so.
  • requirements.txt pinned only the 25 packages the project uses directly. pip chose the newest release of the more than 100 others they need on the day of each install, so an install could change or break with no change here.

What changes

  • src/ingest_pdfs.py calls PyMuPDF, or PyPDF with --loader pypdf, itself. insert_tables puts each page's Markdown tables where the old loader put them, so the pages, the passages and the embedding estimate stay the same. Passages now record only their page, project and file name; nothing read the other details the old loaders stored.
  • requirements.in lists the 24 packages the project uses directly, now without langchain-community. make lock runs uv to pin them, and everything they need, in requirements.txt: 139 packages for Python 3.11 to 3.14. make install and the checks still install requirements.txt with pip. Four packages leave the install: langchain-community, langchain-classic, SQLAlchemy and httpx-sse.
  • Every pinned package has a ready-made version for Linux, Windows with an Intel or AMD processor, and Macs with Apple silicon and macOS 14 or later. The pinned onnxruntime, which Chroma needs, has none for Intel Macs or macOS 13 and earlier, so make install fails there. The README's Quickstart now names the systems it supports, and the decisions entry gives a fallback for Apple silicon Macs on macOS 13.
  • New tests check that a page with a footer and two tables comes out exactly as each old loader made it, and that requirements.txt pins every package in requirements.in at the same version.
  • Two new entries in docs/decisions.md give the alternatives, evidence and trade-offs. The README's "Built with" section mentions the lock, and the two future-work items this completes are gone.

Verification

  • Same pages. I read all 34 PDFs I have, 1,556 pages, with the old loaders (from the old environment) and with the new code: 28 AG1 reports, five reports of AC2-093, AF1-236 and AF2-353, and the test fixture. With both readers, 0 pages differ.
  • Same table placement. For every text of up to 16 characters made of a letter and line breaks, every text of up to 9 with spaces as well, and 300,000 random longer texts, each with no table, one, two or a lone line break, insert_tables gives exactly what langchain-community's own _merge_text_and_extras gives: 938,288 cases, with no difference.
  • Same embedding estimate. The embedding dry run on the 25 reports make fetch selects gives 5,499 passages, 1,980,104 tokens and $0.0396, with the old code and with the new. Only the old one prints the retirement warning.
  • The lock installs and the tests pass. In new environments for Python 3.11 and 3.14 (uv) and 3.13 (pip, as make install does), 222 tests pass in each, with main merged in, and langchain_community isn't installed. The warnings are PyMuPDF's, as on main, and on Python 3.14 a deprecation warning from Chroma, which main shows too.
  • Dry runs with the lock's packages, on the full panel and the 25 reports: the setup check, extraction, the evaluation and a question all finish without calling OpenAI.
  • Where it installs. Asked to use only ready-made packages, uv finds one for every pinned package on Linux with Intel, AMD or ARM processors, Windows with Intel or AMD, and Macs with Apple silicon and macOS 14, for Python 3.11 and 3.14. It finds no onnxruntime for macOS 13. On an Apple silicon Mac with macOS 13 and Python 3.11 to 3.13, requirements.in alone resolves, with an older onnxruntime.
  • make lock is stable. Running it twice gives the same file, with the same pins as the first resolution.
  • Planted bugs. The tests catch all 18 planted in a copy of the code: 7 of mine, 8 from the review (5 of which the tests missed before the new page test), and 3 changes to requirements.in without make lock.
  • CI: run 46562523890 passed on Python 3.11 and 3.14, 222 tests each, with 0 billable milliseconds.

Not checked against OpenAI's live API

Nothing here calls OpenAI: the embedding dry run counts tokens offline. The passages are the same as with the old loaders, so the paid steps should behave the same, but none of them, embedding, extraction or the evaluation, has run with either, because the account has no credits yet.

Review

An independent review of be6b425 found no blockers and four things to fix before merging: say where the lock installs, test that the pages stay exactly the same, describe the table rule correctly, and take #10's main. It also made seven minor points. 7266f38 and the commits after it fix all of them but one: I haven't added requirements.txt.tmp to .gitignore, so a make lock that fails halfway leaves a file that shows. These fixes had my review only.

langchain-community, which provided the two PDF loaders, was retired in May 2026, and every
make embed printed a warning saying so. src/ingest_pdfs.py now calls PyMuPDF and PyPDF itself
and puts each page's tables where the old loader did, so the pages, the passages and the
embedding estimate stay the same. Four packages leave the install.

requirements.txt pinned only the 25 packages the project uses directly, so pip took the newest
version of everything they need. requirements.in now lists the direct packages, and make lock
uses uv to pin all 139 packages, for Python 3.11 to 3.14, into requirements.txt, which make
install and the checks still install with pip.

Checked: all 1,556 pages of the 34 PDFs I have come out the same with the old loaders and the
new code, with both readers; the table placement matches the old loader's own code for 186,873
short texts; the embedding dry run on the 25 reports PR #10 selects gives 5,499 passages and
1,980,104 tokens with both; the tests pass on new installs of the lock on Python 3.11, 3.13 and
3.14; and the tests catch 7 of 7 planted bugs.
The pinned onnxruntime has no ready-made package for Intel Macs or
macOS 13, so the README now names the systems the lock installs on, and
the decisions entry gives the fallback for an Apple silicon Mac on
macOS 13.

A test now checks that a page with a footer and two tables comes out
exactly as each old langchain-community loader made it, and another
that requirements.txt pins each package in requirements.in at the same
version. The comment on where tables go now gives the old loader's
rule. The evidence counts the random pages checked, 938,288, and the
tokens embedded, 1,980,104.
@aymanazeem

Copy link
Copy Markdown
Owner Author

Evidence for ab11ecd, the head to be merged.

  • CI: the checks job passed on Python 3.11 and 3.14 in run 46562523890, 222 tests each, with 0 billable milliseconds (from gh api repos/aymanazeem/Interconnection-Queue-Agent/actions/runs/46562523890/timing). Python 3.11 shows no warnings, and 3.14 only Chroma's asyncio.iscoroutinefunction deprecation. On main, both also show langchain-community's retirement warning, which this removes.
  • Locally: 222 tests pass with the lock installed for Python 3.11 and 3.14 by uv, and for 3.13 by pip, as make install does.
  • Dry runs with the lock's packages, on the full panel and the 25 default reports: the setup check, extraction, the evaluation and a question all finish without calling OpenAI. The embedding dry run gives 5,499 passages and 1,980,104 tokens, as with the old loaders.
  • Planted bugs: 18 of 18 caught on the final branch. Mine: tables always at the end; at the last break, not the last but one; wide breaks ignored; the text after a break dropped; no tables; pages numbered from 1; --loader pypdf ignored. The review's: tables joined with nothing, or with a blank line; only the first table kept; page text not stripped; tables after a page with no break joined by three line ends; page text sorted; PyPDF in layout mode; tables found by text alignment. And requirements.in changed without make lock: duckdb 1.5.3, uvicorn[standard]==0.99.0 and pytest>=9.1.1.
  • Table placement: 938,288 cases, with no difference from langchain-community's own code. The script is below.
  • Review: the independent review of be6b425 found no blockers, four things to fix and seven minor points. 7266f38, 6e01752 and ab11ecd fix all of them except leaving requirements.txt.tmp out of .gitignore. These fixes had my review only.
  • Not checked against OpenAI's live API: embedding, extraction and the evaluation have not run with either the old loaders or the new code.
The table placement check and its output
"""Check insert_tables against langchain-community's _merge_text_and_extras, exhaustively and at random.

Run from a checkout of this branch, with a Python that still has langchain-community 0.4.2:
    python -W ignore placement_check.py
"""

import itertools
import random
import sys
import warnings
from pathlib import Path

sys.path.insert(0, str(Path.cwd()))
warnings.filterwarnings("ignore")
from langchain_community.document_loaders.parsers.pdf import _merge_text_and_extras  # noqa: E402

from src.ingest_pdfs import insert_tables  # noqa: E402


def old(text: str, tables: str) -> str:
    # PyMuPDFParser._get_page_content appends the tables only when the joined string is non-empty
    return _merge_text_and_extras([tables] if tables else [], text).strip()


def new(text: str, tables: str) -> str:
    return insert_tables(text, tables).strip()


TABLES = ["", "|A|1|\n|---|---|\n", "|B|2|\n|---|---|\n\n|C|3|\n|---|---|\n", "\n"]


def texts(alphabet: str, longest: int):
    for length in range(longest + 1):
        for letters in itertools.product(alphabet, repeat=length):
            yield "".join(letters)


def main() -> None:
    cases = set()
    for alphabet, longest in (("a\n", 14), ("a \n", 9)):
        cases.update(texts(alphabet, longest))
    print(f"author's grid: {(2**15 - 1) + (3**10 - 1) // 2} texts counted with overlap, "
          f"{len(cases)} distinct; x3 table choices = {3 * ((2**15 - 1) + (3**10 - 1) // 2)} vs {3 * len(cases)}")
    cases.update(texts("a\n", 16))
    checked = mismatches = 0
    for text in cases:
        for tables in TABLES:
            checked += 1
            if old(text, tables) != new(text, tables):
                mismatches += 1
                if mismatches <= 5:
                    print("MISMATCH", repr(text), repr(tables))
    rng = random.Random(20261201)
    pieces = ["a", "b", " ", "\n", "\n\n", "\n\n\n", "\r", "\x0c", "\t", "Page 4"]
    for _ in range(300_000):
        text = "".join(rng.choice(pieces) for _ in range(rng.randint(0, 40)))
        tables = rng.choice(TABLES)
        checked += 1
        if old(text, tables) != new(text, tables):
            mismatches += 1
            if mismatches <= 10:
                print("MISMATCH", repr(text), repr(tables))
    print(f"checked {checked} cases, {mismatches} mismatches")


if __name__ == "__main__":
    main()
author's grid: 62291 texts counted with overlap, 61268 distinct; x3 table choices = 186873 vs 183804
checked 938288 cases, 0 mismatches

The first line is from the review, which found that the count first given here, 186,873, counted 1,023 texts twice.

@aymanazeem
aymanazeem merged commit 931fd3b into main Dec 1, 2026
2 checks passed
@aymanazeem
aymanazeem deleted the fix/pdf-readers-and-lock branch December 1, 2026 02:40
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