A PDF can contain selectable text on one page and only a scanned image on the next. If an ingestion pipeline chooses extraction once per file, the scanned page can quietly enter the index as an empty string. A search result then jumps from page 8 to page 10, and the answer on page 9 is never available to retrieve.
A page-level routing decision catches that failure without paying the OCR cost for every page. It also needs an escape hatch: a page with selectable text can still have a broken character map or an image containing the actual answer.
Start with page-level evidence
For each page, keep the extracted text, the page number, and a few signals from the parser: character count, image coverage if available, and a sample of the output. A blank page and a scanned page may both yield zero characters. The difference is visible in the rendered page, so send low-text pages for inspection or OCR rather than declaring them empty.
The following router illustrates the decision. Its input is a page record produced by your PDF extraction and rendering stages; it does not inspect a PDF by itself.
def choose_route(page):
text = page.get("text", "").strip()
if not text:
return "inspect_or_ocr"
if page.get("image_contains_content", False):
return "text_plus_image_review"
if page.get("text_quality", "unknown") != "ok":
return "text_review"
return "native_text"
assert choose_route({"text": ""}) == "inspect_or_ocr"
assert choose_route({"text": "Title", "image_contains_content": True}) == "text_plus_image_review"
assert choose_route({"text": "Revenue", "text_quality": "ok"}) == "native_text"
The defaults are deliberate. A short text layer saying "Page 9" does not prove the rest of the page was extracted. A quality classifier or a reviewer must supply the text_quality and image_contains_content flags; the function cannot infer them from a character count. For a page containing a diagram with labels, retain both native text and the image-derived result until a later step can reconcile them.
Keep both versions when OCR runs
OCR can introduce errors too. A decimal point, minus sign, or subscript may disappear, and a two-column page may come back in the wrong order. Preserve the native extraction and OCR output separately, along with their source page and processing version. Pick a representation for indexing, but retain the other for review. Avoid concatenating both versions blindly: duplicate passages can crowd the top search results and make repeated text look like independent evidence.
For example, an extracted page might have a header and footer as native text while the body is a scan. Indexing only the native layer loses the body. Replacing the whole page with OCR can instead degrade a correctly encoded formula in the header. A region-aware merge can retain each source where it works, provided the pipeline records which region came from which method. If region matching is unreliable, flag the page for review instead of guessing.
Test the route and the answer
Build a small fixture set from documents you are allowed to process. Include an image-only page, a genuinely blank page, a scan with a selectable page number, and a page where native text and an image both carry content. Add a page with corrupted selectable text if your corpus contains one. Record the expected route and the page location for each fixture.
Then ask answerable questions whose evidence lies on those pages. Measure two things separately: whether the route selected the intended processing path, and whether retrieval returned the page containing the evidence. A correct route can still produce bad OCR. A retrieved page can still contain the wrong number. For numeric answers, compare the retrieved text against the page image before accepting it as ground truth.
When comparing configurations, hold the fixture set and retrieval settings fixed. Count pages sent to OCR, reviewed pages, missing evidence, and incorrect extracted values. These counts expose the cost of aggressive OCR alongside the failures caused by a permissive native-text route. Actual latency and charges depend on the OCR provider and document mix, so measure them on your own corpus.
Store the decision with the chunk
Attach a document identifier, source page number, extraction method, and processing version to every indexed chunk. If the page was flagged for review, keep that status too. A citation should lead back to the page image, not just the OCR text, so a reader can check a questionable symbol in context.
This extends the page-boundary approach in PDF RAG citations break when chunks lose page boundaries. Page provenance tells you where a chunk came from; the routing record tells you how its text was obtained and which pages need another look.
Top comments (0)