Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

Run OCR fallback for image-free scanned PDFs after #1386

Bingxi Zhao (Frank)9f751974a983
diff --git a/deeptutor/services/rag/pipelines/llamaindex/document_loader.py b/deeptutor/services/rag/pipelines/llamaindex/document_loader.py
index 94e4331e6..f5fded8ce 100644
--- a/deeptutor/services/rag/pipelines/llamaindex/document_loader.py
+++ b/deeptutor/services/rag/pipelines/llamaindex/document_loader.py
@@ -90,7 +90,6 @@ class LlamaIndexDocumentLoader:
scanned_pdf_needs_ocr = (
file_path.suffix.lower() == ".pdf"
and not text.strip()
- and bool(extracted_images)
and parse_engine != _PDF_OCR_FALLBACK_ENGINE
)
if scanned_pdf_needs_ocr:
diff --git a/tests/services/rag/test_llamaindex_document_loader.py b/tests/services/rag/test_llamaindex_document_loader.py
index f324163a2..a48ed2e75 100644
--- a/tests/services/rag/test_llamaindex_document_loader.py
+++ b/tests/services/rag/test_llamaindex_document_loader.py
@@ -575,3 +575,29 @@ def test_loader_skips_images_when_llm_client_is_unavailable(
assert "requires both multimodal embedding and multimodal LLM support" in caplog.text
assert "LLM client is unavailable" in caplog.text
assert "no LLM configured" in caplog.text
+
+
+def test_loader_falls_back_to_ocr_when_scanned_pdf_has_no_extracted_assets(
+ tmp_path: Path,
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ pytest.importorskip("llama_index.core")
+ from deeptutor.services.parsing.types import ParsedDocument
+ from deeptutor.services.rag.pipelines.llamaindex import document_loader as loader_module
+ from deeptutor.services.rag.pipelines.llamaindex.document_loader import LlamaIndexDocumentLoader
+
+ pdf_path = tmp_path / "scan.pdf"
+ pdf_path.write_bytes(b"stub")
+ calls = _install_sequential_parse_service(
+ monkeypatch,
+ [
+ ParsedDocument(markdown="", engine="pymupdf4llm"),
+ ParsedDocument(markdown="Recovered scan", engine="liteparse"),
+ ],
+ )
+ monkeypatch.setattr(
+ loader_module, "get_embedding_client", lambda: _text_only_embedding_client()
+ )
+ documents = asyncio.run(LlamaIndexDocumentLoader().load([str(pdf_path)]))
+ assert calls == [("scan.pdf", None), ("scan.pdf", "liteparse")]
+ assert [item.text for item in documents] == ["Recovered scan"]