~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""Shared ceilings for PDF text extraction.
The storage/upload caps bound a document's file SIZE, not the COST of
extracting text from it: a PDF built from millions of cheap pages, or one
whose streams decompress into huge text, can still burn unbounded CPU
and memory inside the server process while a page-walking extractor
chews through it.
At this tree these ceilings bound four extraction paths:
- the download service's ``_extract_text_from_pdf``
(``research_library/services/download_service.py``, pdfplumber and its
PyPDF fallback);
- the downloader base helper ``extract_text_from_pdf``
(``research_library/downloaders/base.py``);
- the upload-path extraction service
(``web/services/pdf_extraction_service.py``, pdfplumber).
- the byte-backed document loader used by RAG uploads and Zotero sync
(``document_loaders/bounded_pdf.py``, pypdf).
On all four paths they bound exactly four things:
- pages walked: text is extracted from at most
``MAX_PDF_EXTRACTION_PAGES`` (500) pages per attempt. The download
service's PyPDF fallback runs only when pdfplumber produced no text,
so one extraction there can walk up to twice that many pages (500
empty pdfplumber pages, then up to 500 PyPDF pages). A longer
document yields the text of at most its first 500 pages, and fewer
if another ceiling stops the walk first;
- characters kept: at most ``MAX_PDF_EXTRACTED_CHARS`` (10,000,000)
characters -- Python ``str`` characters, i.e. code points, not bytes
-- including the separators that join pages; the page that crosses
the ceiling contributes the prefix that fits;
- CPU time between pages: one whole extraction, every attempt of it
included, stops starting new pages once the extracting thread has
spent ``MAX_PDF_EXTRACTION_CPU_SECONDS`` of its OWN CPU time
(``time.thread_time()``) since it began. Wall-clock time is
deliberately not used: time spent waiting for the GIL or for other
requests' extractions is not work done on this document, and counting
it would let server load cut honest documents short;
- what the PDF library keeps between pages: pdfminer and pypdf keep
every stream they decode and every object they parse for the life of
the document, and closing a pdfplumber page does not release them.
Once the decoded stream bytes plus an estimate of the parsed objects
the walk has cached reach ``MAX_PDF_RETAINED_DECODED_BYTES`` (64 MiB),
``utilities/pdf_stream_release`` returns the decoded streams to their
encoded state and drops the parsed objects (and pdfminer's fonts) the
walk added. What the walk holds between pages therefore stays near
that threshold instead of growing with the pages walked.
What they do NOT bound:
- page-tree materialisation on paths that still use eager page access.
``pypdf`` flattens the whole page tree, and accessing
``pdfplumber.PDF.pages`` builds every page object before the first page
is yielded. These operations can take O(all pages) time and memory,
regardless of the page ceiling. The download service and upload-path
extraction instead use ``utilities/pdf_page_walk`` to walk pdfplumber
pages lazily, building only the pages reached by the walk (plus the
one that establishes that more pages exist). The PyPDF fallback remains
eager, so its page-tree construction is not bounded by the extraction
ceilings.
- the cost of a single page. One page's ``extract_text()`` runs to
completion inside the library with no check point available to us; a
few kilobytes of compressed content streams can take minutes or
exhaust gigabytes of address space there, and what that page decodes
and parses is held until the release after it.
- the exact size of what is held between releases. Parsed objects are
estimated at a fixed number of bytes per array item or dictionary
entry, and pdfminer's fonts keep their own copy of their widths, so
the memory held can exceed the threshold by a small multiple: walks
that held ~20 MB (decoded streams) or ~60 MB (parsed ``/Widths``
arrays) more per page peaked ~100-250 MB above their starting RSS.
Objects cached before the walk began (the page tree, where it was
built before the walk) are kept, and a
released stream that a later page uses again is decoded again, at the
CPU budget's expense.
Interrupting a parse or a page that is already running would need a
separate process, which these constants deliberately do not introduce.
Text cut short by any of these ceilings is returned, and stored, like a
complete extraction. The upload-path service also returns a truncation flag
to its API and research form; the byte-backed loader marks its page documents
as truncated, but the RAG/Zotero callers only consume joined text, so they
currently expose the partial result through a logged warning.
"""
from __future__ import annotations
# 500 pages comfortably covers book-length documents; real research papers
# are two orders of magnitude below it.
MAX_PDF_EXTRACTION_PAGES = 500
# 10 MB of text is ~5 million words — far beyond any real paper, while
# capping decompression-bomb output.
MAX_PDF_EXTRACTED_CHARS = 10_000_000
# CPU budget, in seconds of the extracting thread's own CPU time
# (``time.thread_time()``), for one whole extraction: every attempt of
# it, from entry to return, shares this one budget. It is checked
# BETWEEN pages only, so it cannot interrupt a page already inside
# pdfplumber/pypdf, nor the parser's page-tree construction before the
# first page.
#
# Sized so an honest document at the page ceiling is not cut. Measured
# with pdfplumber 0.11.10 on CPython 3.14: a synthetic 500-page PDF of
# very dense text (~12,700 characters per page, about twice a dense book
# page) costs ~146 s of the thread's CPU when extracted alone, and
# ~0.5-0.66 s per page (~250-330 s for 500 pages) with 4 to 16
# extractions contending for the GIL, whose switching overhead
# thread_time() does count; an ordinary text page (~5,700 characters)
# costs ~0.14 s alone. 600 s leaves 2x margin over the dense document
# with 4 extractions contending and ~1.8x with 16. Per character that is
# ~23 us alone, ~39 us at 4-way and ~52 us at 16-way contention, so the
# character ceiling (10,000,000 characters: ~230-520 s) binds before
# this budget on honest text. Heavier contention, or a host several times
# slower, can still reach it on a dense 500-page document; the text is
# then cut the same way the page ceiling cuts it.
#
# The budget still stops a document whose individual pages are
# pathologically expensive, but it is not a hard cap on the extraction's
# CPU. Unchecked are: the page-tree parse before the first check
# (``pdfplumber.open``/``PdfReader``), the page already running when the
# budget runs out, and -- in the download service, when pdfplumber
# produced no text with budget still left -- the PyPDF fallback's
# ``PdfReader`` parse, which starts after the last check and runs to
# completion before its first page is checked.
MAX_PDF_EXTRACTION_CPU_SECONDS = 600.0
# What the PDF library may keep cached between pages, in (estimated)
# bytes. pdfminer and pypdf keep every stream they decode, and every
# object they parse, for the life of the document, and page.close() does
# not release them: a page whose content stream is 20 MB of spaces
# compresses to ~20 KB, yields no characters and costs ~0.17 s of CPU,
# so a walk to the page ceiling held ~10 GB from a ~10 MB file; a page
# whose font points at a 500,000-entry /Widths array held ~60 MB more per
# page. Once the decoded stream bytes plus an estimate of the parsed
# objects the walk has cached reach this many bytes,
# ``utilities/pdf_stream_release`` releases them (checked between pages,
# like the other ceilings). Honest documents -- content streams of tens
# to hundreds of KB per page, a few embedded fonts -- stay far below it:
# no release ran on any of the real and synthetic documents measured.
MAX_PDF_RETAINED_DECODED_BYTES = 64 * 1024 * 1024