DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.
Python ai-agentsai-tutorclawbotcli-tooldeepresearchinteractive-learninglearningmulti-agent-systemsrag
fix(attachments): retain native PDF fallback on parser failure
Bingxi Zhao (Frank)d721d21d09e7
diff --git a/deeptutor/services/session/attachment_parsing.py b/deeptutor/services/session/attachment_parsing.pyindex ead8b8492..05d50e561 100644--- a/deeptutor/services/session/attachment_parsing.py+++ b/deeptutor/services/session/attachment_parsing.py@@ -46,6 +46,7 @@ async def parse_chat_pdf_attachments()error = ""text = ""+ fallback_text = str(record.get("extracted_text") or "").strip()if path is None:error = "stored attachment could not be found for configured parsing"elif limits.max_chars_total - total_chars <= 0:@@ -72,17 +73,37 @@ async def parse_chat_pdf_attachments(record["extracted_text"] = textrecord["extracted_chars"] = len(text)record.pop("extraction_error", None)+ record.pop("parser_error", None)total_chars += len(text)+ phase = "completed"+ detail = "Document parsing completed"+ elif fallback_text:+ # A configured cloud/local engine can be temporarily unavailable.+ # Preserve usable native text rather than turning a readable PDF+ # into a failed attachment; the parser error remains observable.+ text = fallback_text+ record["extracted_text"] = text+ record["extracted_chars"] = len(text)+ record.pop("extraction_error", None)+ record["parser_error"] = error+ total_chars += len(text)+ _replace_context(contexts, filename, text, "")+ phase = "fallback"+ detail = f"Configured parser failed; using local PDF text: {error}"else:record["extracted_text"] = ""record["extracted_chars"] = 0record["extraction_error"] = error- _replace_context(contexts, filename, text, error)+ _replace_context(contexts, filename, text, error)+ phase = "failed"+ detail = f"Document parsing failed: {error}"+ if text and phase == "completed":+ _replace_context(contexts, filename, text, "")if on_progress:on_progress(attachment_id,- "completed" if text else "failed",- "Document parsing completed" if text else f"Document parsing failed: {error}",+ phase,+ detail,)return updated, contextsdiff --git a/deeptutor/services/session/turns/executor.py b/deeptutor/services/session/turns/executor.pyindex 6f9e1ea61..7b2362344 100644--- a/deeptutor/services/session/turns/executor.py+++ b/deeptutor/services/session/turns/executor.py@@ -345,22 +345,15 @@ class TurnExecutor:from deeptutor.utils.document_extractor import extract_documents_from_records- # PDFs are parsed exclusively by the configured Document Parsing- # engine. Keep them out of the legacy local extractor so a native- # text layer can never mask missing images or scanned pages.+ # Keep the built-in extractor as a graceful fallback, then replace+ # every PDF's result with the configured parser output. Running the+ # configured parser regardless of a native text layer is what lets+ # OCR/layout engines see scanned pages and figures.+ document_texts, attachment_records = extract_documents_from_records(attachment_records)pdf_records = [- record for record in attachment_records- if str(record.get("filename") or "").lower().endswith(".pdf")- ]- non_pdf_records = [- record for record in attachment_records- if not str(record.get("filename") or "").lower().endswith(".pdf")- ]- document_texts, extracted_non_pdf = extract_documents_from_records(non_pdf_records)- extracted_by_id = {str(record.get("id")): record for record in extracted_non_pdf}- attachment_records = [- extracted_by_id.get(str(record.get("id")), record)+ recordfor record in attachment_records+ if str(record.get("filename") or "").lower().endswith(".pdf")]from deeptutor.services.session.attachment_parsing import parse_chat_pdf_attachments@@ -386,7 +379,7 @@ class TurnExecutor:_attachment_progress(str(record.get("id") or ""),"received",- "Received and stored on Raspberry Pi",+ "Received PDF attachment",)attachment_records, document_texts = await parse_chat_pdf_attachments(diff --git a/tests/services/session/test_attachment_parsing.py b/tests/services/session/test_attachment_parsing.pyindex 4d5ed4ea5..92a7cba99 100644--- a/tests/services/session/test_attachment_parsing.py+++ b/tests/services/session/test_attachment_parsing.py@@ -97,3 +97,40 @@ async def test_parser_failure_is_not_reported_as_empty_success(assert records[0]["extracted_text"] == ""assert "parser unavailable" in records[0]["extraction_error"]assert "could not be read" in contexts[0]+++async def test_parser_failure_preserves_usable_native_pdf_text(+ monkeypatch: pytest.MonkeyPatch, tmp_path: Path+) -> None:+ store = LocalDiskAttachmentStore(root=tmp_path / "attachments")+ await store.put(session_id="session", attachment_id="pdf", filename="notes.pdf", data=b"pdf")+ progress: list[str] = []++ class Parser:+ def parse(self, path: Path, **_: object) -> ParsedDocument:+ raise RuntimeError("configured parser offline")++ monkeypatch.setattr(+ "deeptutor.services.session.attachment_parsing.get_parse_service", lambda: Parser()+ )+ records, contexts = await parse_chat_pdf_attachments(+ [+ {+ "id": "pdf",+ "filename": "notes.pdf",+ "extracted_text": "native text",+ "extracted_chars": 11,+ }+ ],+ attachment_store=store,+ session_id="session",+ document_texts=["[File: notes.pdf]\nnative text"],+ on_progress=lambda _aid, phase, _detail: progress.append(phase),+ )++ assert records[0]["extracted_text"] == "native text"+ assert "configured parser offline" in records[0]["parser_error"]+ assert "extraction_error" not in records[0]+ assert contexts == ["[File: notes.pdf]\nnative text"]+ assert progress == ["submitting", "fallback"]