A practical validation pattern for bank statements and other financial documents.
A bank statement lands in a document-processing queue. The PDF opens. Text extraction finishes without an exception. A downstream service records a balance and marks the document complete.
Then someone notices that the number does not match the statement's closing balance.
This is an illustrative example, informed by my experience automating PDF extraction in banking workflows. It is not an account of a specific customer incident or an internal system. The distinction matters: a successful parse only says that software obtained text. It does not prove that the right field was identified, that the number was interpreted correctly, or that it is safe for another system to use.
How a plausible number becomes the wrong answer
Suppose a statement shows:
| Field | Amount |
| Opening balance | 8,420.50 |
| Credits | 2,000.00 |
| Debits | 1,500.00 |
| Closing balance | 8,920.50 |
| Available balance | 8,290.50 |
The extraction layer returns all five lines. A rule looking for “balance” selects the last match: 8,290.50. The value is well formed and appears on the page, so simple checks such as “is present” and “is a decimal” pass. The mistake is in the mapping from text to business meaning.
Other documents can fail earlier in the path. A scanned PDF may contain only an image, so pypdf cannot read its visible text without OCR. Tables, columns, headers, and footers can make reading order ambiguous. The pypdf documentation explains both the lack of a semantic layer in PDF files and the difference between text extraction and OCR. That is why an extraction success flag should never be treated as a field-validation result.
Put checks between extraction and the downstream API
The pipeline should preserve evidence about each candidate value, then decide whether it can be trusted for this specific field:

- Classify the document. Determine whether it is digitally generated, scanned, or OCRed, and identify the statement layout or template where possible. Route image-only pages to OCR rather than pretending an empty text result is a valid statement.
- Extract candidates with provenance. Keep the original document identifier, page, nearby label, and location when the extraction tool provides them. Store the raw text alongside the normalized value. A number without its label is hard to audit.
- Validate the field's meaning. “Closing balance” must come from the closing-balance field for the relevant account and period. It is not interchangeable with “available balance,” a running balance in the transaction table, or a balance on a different page.
- Reconcile the figures. In this simplified statement, opening balance + credits − debits should equal closing balance. Here, that is 8,420.50 + 2,000.00 − 1,500.00 = 8,920.50. Selecting 8,290.50 would leave a 630.00 discrepancy.
- Review exceptions before publication. A missing label, conflicting candidates, failed reconciliation, or low-confidence OCR result should stop automatic publication and put the field into a review queue. The reviewer needs to see the source page and the exact candidate values.
The reconciliation rule is an example, not a universal banking formula. Statements can contain holds, fees, pending transactions, reversals, multiple currencies, and different definitions of “balance.” The rule must follow the document's business definition and be approved by the people who own the workflow.
A small validation gate in code
Once extraction has produced labeled fields, the gate can be simple. Keep money as decimal values and reject ambiguous or inconsistent data:
from decimal import Decimal
def validate_statement(fields: dict[str, str]) -> Decimal:
required = ("opening_balance", "credits", "debits", "closing_balance")
if any(name not in fields for name in required):
raise ValueError("Missing required statement field; send for review")
values = {name: Decimal(fields[name].replace(",", "")) for name in required}
expected = values["opening_balance"] + values["credits"] - values["debits"]
if expected != values["closing_balance"]:
raise ValueError("Balance does not reconcile; send for review")
return values["closing_balance"]
This function assumes the upstream mapper has already selected the closing balance from a supported layout and has rejected conflicting labels. It is one gate in the workflow, not a PDF parser. For a production implementation, also validate currency, period, account identity, signed amounts, locale-specific number formats, and the provenance of each field. Python's Decimal documentation explains why decimal values are preferable to binary floating point for exact decimal arithmetic.
OCR confidence can help decide what to review, but it is not proof that a value is correct. Amazon Textract's guidance recommends tuning thresholds on representative documents and checking how many errors still pass automatically. A high-confidence reading of the wrong labeled field is still wrong.
Test the failure path, not just the happy path
A useful test set contains more than clean, single-page PDFs. Include scanned statements, rotated pages, merged table cells, duplicate balance labels, negative amounts, different date formats, and statements where the available and closing balances differ. Give each sample a reviewed expected result and keep it as a regression case when the parser, OCR engine, or mapping rules change.
Measure more than “percentage of documents parsed.” Track field accuracy, false acceptance of wrong values, review rate, and time to resolve exceptions. For a sensitive financial workflow, an increase in review volume may be acceptable if it prevents wrong values from silently reaching a loan, fraud, or reporting process. The threshold should reflect the cost of each kind of mistake.
The production boundary is clear: extraction proposes a value; validation decides whether it may become business data. If the evidence is weak, the system should preserve the document, explain the exception, and ask for review. That is a better outcome than returning success with a convincing but incorrect number.
What document field has caused the most trouble in your own extraction workflow: amounts, dates, names, or table rows?
References