Skip to content

Turn any technical book PDF into a Claude Code skill — ready to study, reference, and use while you work.

fix(pdf): fall back on invalid inspector metadata (#218)

Stamina901f8a742aeb9
diff --git a/book_to_skill/pdf_inspector_integration.py b/book_to_skill/pdf_inspector_integration.py
index d784955..8f9ce9d 100644
--- a/book_to_skill/pdf_inspector_integration.py
+++ b/book_to_skill/pdf_inspector_integration.py
@@ -58,6 +58,41 @@ def inspect_pdf(path: str | Path) -> tuple[str | None, dict[str, Any] | None]:
try:
result = pdf_inspector.process_pdf(str(path))
+ pdf_type = _normalise_pdf_type(getattr(result, "pdf_type", None))
+ confidence = float(getattr(result, "confidence", 0.0) or 0.0)
+ pages_needing_ocr = list(getattr(result, "pages_needing_ocr", None) or [])
+ has_encoding_issues = bool(getattr(result, "has_encoding_issues", False))
+ markdown = getattr(result, "markdown", None)
+
+ native_markdown_trusted = bool(
+ isinstance(markdown, str)
+ and markdown.strip()
+ and pdf_type == "text_based"
+ and confidence >= _MIN_NATIVE_CONFIDENCE
+ and not pages_needing_ocr
+ and not has_encoding_issues
+ )
+
+ metadata: dict[str, Any] = {
+ "engine": "pdf-inspector",
+ "version": _package_version(),
+ "pdf_type": pdf_type,
+ "confidence": round(confidence, 4),
+ "native_markdown_trusted": native_markdown_trusted,
+ "pages_needing_ocr": pages_needing_ocr,
+ "ocr_reasons_by_page": _ocr_reasons(
+ getattr(result, "ocr_reasons_by_page", None)
+ ),
+ "pages_with_tables": list(
+ getattr(result, "pages_with_tables", None) or []
+ ),
+ "pages_with_columns": list(
+ getattr(result, "pages_with_columns", None) or []
+ ),
+ "has_encoding_issues": has_encoding_issues,
+ "is_complex_layout": bool(getattr(result, "is_complex_layout", False)),
+ "page_count": int(getattr(result, "page_count", 0) or 0),
+ }
except Exception as exc:
print(
f" [warn] pdf-inspector preflight failed: {type(exc).__name__}: {exc}",
@@ -65,37 +100,6 @@ def inspect_pdf(path: str | Path) -> tuple[str | None, dict[str, Any] | None]:
)
return None, None
- pdf_type = _normalise_pdf_type(getattr(result, "pdf_type", None))
- confidence = float(getattr(result, "confidence", 0.0) or 0.0)
- pages_needing_ocr = list(getattr(result, "pages_needing_ocr", None) or [])
- has_encoding_issues = bool(getattr(result, "has_encoding_issues", False))
- markdown = getattr(result, "markdown", None)
-
- native_markdown_trusted = bool(
- isinstance(markdown, str)
- and markdown.strip()
- and pdf_type == "text_based"
- and confidence >= _MIN_NATIVE_CONFIDENCE
- and not pages_needing_ocr
- and not has_encoding_issues
- )
-
- metadata: dict[str, Any] = {
- "engine": "pdf-inspector",
- "version": _package_version(),
- "pdf_type": pdf_type,
- "confidence": round(confidence, 4),
- "native_markdown_trusted": native_markdown_trusted,
- "pages_needing_ocr": pages_needing_ocr,
- "ocr_reasons_by_page": _ocr_reasons(
- getattr(result, "ocr_reasons_by_page", None)
- ),
- "pages_with_tables": list(getattr(result, "pages_with_tables", None) or []),
- "pages_with_columns": list(getattr(result, "pages_with_columns", None) or []),
- "has_encoding_issues": has_encoding_issues,
- "is_complex_layout": bool(getattr(result, "is_complex_layout", False)),
- "page_count": int(getattr(result, "page_count", 0) or 0),
- }
return (markdown if native_markdown_trusted else None), metadata
diff --git a/tests/test_pdf_inspector_integration.py b/tests/test_pdf_inspector_integration.py
index e174b51..a3adb59 100644
--- a/tests/test_pdf_inspector_integration.py
+++ b/tests/test_pdf_inspector_integration.py
@@ -2,6 +2,8 @@ import json
import sys
from types import SimpleNamespace
+import pytest
+
import book_to_skill.pdf_inspector_integration as integration
@@ -55,6 +57,51 @@ def test_inspect_pdf_rejects_native_markdown_when_ocr_is_recommended(monkeypatch
]
+ "overrides",
+ [
+ {"confidence": "not-a-number"},
+ {"page_count": "not-an-integer"},
+ {"pages_needing_ocr": object()},
+ ],
+)
+def test_inspect_pdf_falls_back_on_invalid_metadata(monkeypatch, capsys, overrides):
+ fake_module = SimpleNamespace(
+ process_pdf=lambda _path: _fake_result(**overrides)
+ )
+ monkeypatch.setitem(sys.modules, "pdf_inspector", fake_module)
+
+ assert integration.inspect_pdf("book.pdf") == (None, None)
+ assert "pdf-inspector preflight failed" in capsys.readouterr().err
+
+
+def test_hook_uses_existing_pipeline_when_inspector_metadata_is_invalid(
+ tmp_path, monkeypatch
+):
+ integration._reset_state_for_tests()
+ pdf = tmp_path / "book.pdf"
+ pdf.write_bytes(b"%PDF-1.7\nfixture")
+ fake_module = SimpleNamespace(
+ process_pdf=lambda _path: _fake_result(confidence="not-a-number")
+ )
+ monkeypatch.setitem(sys.modules, "pdf_inspector", fake_module)
+
+ original_calls = []
+
+ def original(*args):
+ original_calls.append(args)
+ return {"extraction_method": "legacy"}
+
+ fake_utils = SimpleNamespace(extract_single_file=original)
+
+ integration.install_pdf_inspector_hook(fake_utils)
+ result = fake_utils.extract_single_file(pdf, "text", "no")
+
+ assert result == {"extraction_method": "legacy"}
+ assert original_calls == [(pdf, "text", "no")]
+ assert integration._INSPECTIONS == {}
+
+
def test_hook_uses_inspector_for_clean_text_pdf(tmp_path, monkeypatch):
integration._reset_state_for_tests()
pdf = tmp_path / "book.pdf"