~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
fix(pdf): preserve download text at character ceiling (#7230)
LearningCircuit1a8f9d0e37da
diff --git a/changelog.d/+pdf-download-character-boundary.bugfix.md b/changelog.d/+pdf-download-character-boundary.bugfix.mdnew file mode 100644index 0000000000..941f2a609c--- /dev/null+++ b/changelog.d/+pdf-download-character-boundary.bugfix.md@@ -0,0 +1 @@+PDF downloads preserve extracted text when the character limit leaves no room for another page, including its separator, instead of attempting that page and potentially losing the retained text if it fails to parse.diff --git a/src/local_deep_research/research_library/downloaders/base.py b/src/local_deep_research/research_library/downloaders/base.pyindex c1ed1e8916..2a427441c5 100644--- a/src/local_deep_research/research_library/downloaders/base.py+++ b/src/local_deep_research/research_library/downloaders/base.py@@ -719,6 +719,15 @@ class BaseDownloader(ABC):f"{page_number} pages")break+ # The running count reserves the next join separator too.+ # Do not risk a failing page when none of its text can fit.+ remaining = MAX_PDF_EXTRACTED_CHARS - extracted_chars+ if remaining <= 0:+ logger.warning(+ "PDF extraction stopped at character ceiling "+ f"({MAX_PDF_EXTRACTED_CHARS})"+ )+ breakreleaser.before_page()text = page.extract_text()# pypdf keeps what it decodes and parses cached on the@@ -726,7 +735,6 @@ class BaseDownloader(ABC):# MAX_PDF_RETAINED_DECODED_BYTES.releaser.after_page()if text:- remaining = MAX_PDF_EXTRACTED_CHARS - extracted_charsif len(text) > remaining:# Keep the slice that fits rather than dropping# the tripping page whole.diff --git a/src/local_deep_research/research_library/services/download_service.py b/src/local_deep_research/research_library/services/download_service.pyindex 7f98ef8257..ec33cf00eb 100644--- a/src/local_deep_research/research_library/services/download_service.py+++ b/src/local_deep_research/research_library/services/download_service.py@@ -1637,12 +1637,18 @@ class DownloadService:f"{page_number} pages")break+ # The count includes the next join separator. Stop+ # before extraction when no text from this page fits.+ remaining = MAX_PDF_EXTRACTED_CHARS - extracted_chars+ if remaining <= 0:+ logger.warning(+ "PDF extraction stopped at character ceiling "+ f"({MAX_PDF_EXTRACTED_CHARS})"+ )+ breakreleaser.before_page()page_text = page.extract_text()if page_text:- remaining = (- MAX_PDF_EXTRACTED_CHARS - extracted_chars- )if len(page_text) > remaining:# Keep the slice that fits instead of dropping# the tripping page: a first page over the@@ -1700,12 +1706,18 @@ class DownloadService:f"after {page_number} pages")break+ remaining = MAX_PDF_EXTRACTED_CHARS - extracted_chars+ if remaining <= 0:+ logger.warning(+ "PDF extraction (PyPDF fallback) stopped at "+ f"character ceiling ({MAX_PDF_EXTRACTED_CHARS})"+ )+ breakreleaser.before_page()text = page.extract_text()# pypdf caches what it decodes and parses on the reader too.releaser.after_page()if text:- remaining = MAX_PDF_EXTRACTED_CHARS - extracted_charsif len(text) > remaining:if remaining > 0:text_parts.append(text[:remaining])diff --git a/tests/research_library/services/test_pdf_extraction_character_boundary.py b/tests/research_library/services/test_pdf_extraction_character_boundary.pynew file mode 100644index 0000000000..57592adc99--- /dev/null+++ b/tests/research_library/services/test_pdf_extraction_character_boundary.py@@ -0,0 +1,60 @@+"""Downloaded PDFs retain their prefix when no more text can fit."""++import pytest++from local_deep_research.research_library.downloaders import base+from local_deep_research.research_library.services import download_service+from tests.document_loaders.test_bounded_pdf_bytes import _text_pdf++[email protected](params=["base", "pdfplumber", "fallback"])+def extractor(request, monkeypatch):+ if request.param == "base":+ return base, base.BaseDownloader.extract_text_from_pdf, "\n"+ if request.param == "fallback":+ from pdfplumber.page import Page++ # Exercise the real pypdf fallback after a textless first attempt.+ monkeypatch.setattr(Page, "extract_text", lambda self: "")+ service = download_service.DownloadService.__new__(+ download_service.DownloadService+ )+ return download_service, service._extract_text_from_pdf, "\n\n"++[email protected]("extra_chars", [0, 1, 2])+def test_no_next_page_extraction_when_only_separator_fits(+ extractor, monkeypatch, extra_chars+):+ module, extract, separator = extractor+ if extra_chars > len(separator):+ pytest.skip("One text character still fits on this path")+ monkeypatch.setattr(module, "MAX_PDF_EXTRACTED_CHARS", 5 + extra_chars)++ text = extract(_text_pdf("Hello", "unread", invalid_page=1))++ assert text == "Hello"+++def test_extract_next_page_when_one_character_still_fits(+ extractor, monkeypatch+):+ module, extract, separator = extractor+ monkeypatch.setattr(+ module, "MAX_PDF_EXTRACTED_CHARS", 5 + len(separator) + 1+ )++ text = extract(_text_pdf("Hello", "World"))++ assert text == "Hello" + separator + "W"+++def test_empty_pages_do_not_spend_separator_budget(extractor, monkeypatch):+ module, extract, separator = extractor+ monkeypatch.setattr(+ module, "MAX_PDF_EXTRACTED_CHARS", 5 + len(separator) + 1+ )++ text = extract(_text_pdf("", "Hello", "", "World"))++ assert text == "Hello" + separator + "W"