Turn source files into searchable passages while retaining the page, section, revision and extraction quality needed to verify an answer.
Document extraction: preserve passage provenance through OCR and parsing
Define the extracted record
A text string alone is not a source. Each passage needs document ID, immutable revision ID, page or section, span offsets when available, extraction method, language and access label. A PDF policy may contain a scanned page, two-column layout and a table whose cells change meaning when read in the wrong order. Store the source file hash and parser version so a changed extraction can be rebuilt without pretending the document itself changed. Corpus identity is the parent contract.
Separate clean text from uncertain text
Parse machine-readable text first. Route image-only pages to OCR, and mark uncertain spans rather than silently normalizing them into authoritative policy. A table value of 47 under ‘hours’ is different from 47 under ‘days’; flattening the headers can invert a support rule. Keep table headers with row text or represent the table as structured cells. Human review should focus on low-confidence pages that answer high-impact questions, not every document equally.
Retain location through cleanup
Whitespace normalization, header removal and dehyphenation can improve search. They also shift character offsets. Maintain a map from cleaned spans back to source-page coordinates or stable section IDs. When the answer cites a passage, the reader should land on the source location that actually supports the claim. If an OCR run changes, rebuild the chunk IDs and invalidate answers tied to old offsets. Citation integrity depends on this map.
Gate ingestion with concrete failures
Feed the parser a missing page, duplicate footer, rotated scan, repeated table header and an inaccessible file. Reject a document if its required metadata or access label is absent. Record page count, extracted character count, empty pages and confidence distribution. A parser yielding text is not evidence that it preserved meaning. Compare a reviewed sample of passages against the original page, especially around headings, tables and footnotes.
Worked contract
from dataclasses import dataclass
from hashlib import sha256
@dataclass(frozen=True)
class ExtractedPassage:
document_id: str
revision_id: str
page: int
section: str
text: str
source_sha256: str
def passage_record(document_id: str, revision_id: str, page: int,
section: str, text: str, source_bytes: bytes) -> ExtractedPassage:
if page < 1 or not document_id or not revision_id or not section or not text.strip():
raise ValueError("incomplete extraction provenance")
return ExtractedPassage(document_id, revision_id, page, section,
text.strip(), sha256(source_bytes).hexdigest())
source = b"Policy r8 page 4"
passage = passage_record("refund-policy", "r8", 4, "Exceptions", " 47 hours ", source)
assert passage.page == 4 and passage.text == "47 hours"
assert passage.source_sha256 == sha256(source).hexdigest()Performance and operating cost
Extraction is O(B) in source bytes before OCR; OCR cost also depends on page count and resolution. Provenance adds small metadata per passage, while page images and offset maps add storage. Budget retries by document, not an unbounded queue. Measure empty-page rate and reviewed semantic errors separately from parser throughput.
Common Mistakes
- Treating OCR output as exact text without a confidence or review path.
- Dropping table headers or section titles before chunking.
- Reusing offsets after normalization changes the text.
- Indexing a passage when its access label or revision is missing.
Validation drill
Extract a 23-page policy with a two-column page, one rotated scan and a table. Check five answer-bearing passages against their original page and record which parser change would invalidate each citation.
Read next
- Chunking documents: preserve section identity and answer boundaries
- Claim-level citations: verify each answer against its passage
- Retrieval corpus contracts: identity, permissions and document lifecycle
- Long documents: evidence indexing and section coverage
Continue this retrieval path
- Table retrieval: keep numeric cells attached to their labels
- Multimodal retrieval: connect images, OCR and text to one asset identity
