Suppose an invoice shows a total of 128.04. The extraction service returns 128.40 in valid JSON. A parser accepts the response, but the amount is wrong.
A useful document workflow needs three checks: structure, source evidence, and business rules. Each check answers a different question.
Keep the source available
A source reference identifies the document revision, page, and text used for a field. This reference lets a reviewer compare the result with the original.
A PDF can contain selectable text, scanned images, or both. An empty text result does not prove that a page is empty.
PyMuPDF supports OCR with engines such as Tesseract. Its documentation explains when OCR is necessary and how to preserve existing text.[1]
- Keep the original file and its document revision.
- Check each page for usable text.
- Use optical character recognition (OCR) where necessary.
- Keep page numbers and source references with the extracted text.
The saved source should include the extraction method and its version. A later OCR change can then be compared with the earlier result.
Check the response structure
A schema defines the expected fields and types. A strict schema can reject an unexpected field or an incorrect type.
OpenAI Structured Outputs constrains response structure. The application still needs to handle model refusals and content errors.[2]
A field contract can include a value, a page number, and a short source quotation. A missing value needs an explicit representation, such as null.
Pydantic strict mode can reject type conversions that the default mode permits. The contract still needs explicit limits for each field.[3]
- Define required fields and permitted values.
- Reject extra fields where the contract requires a fixed structure.
- Handle refusals and incomplete responses before data validation.
- Keep the schema version with the result.
Check the evidence for each field
A page number alone gives weak evidence. The page can contain several dates, totals, or customer names. The quotation must describe the intended field.
A correct quotation from a subtotal line does not support the invoice total. A source match needs a field check as well.
Document text can also contain instructions for the model. Such instructions remain untrusted input. Prompt separation and external permission checks reduce this risk.[4]
- Check that the source reference belongs to the requested document revision.
- Compare the quotation with the saved page text.
- Check that the quotation supports the intended field.
- Send missing or conflicting evidence to human review.
The model should not write directly to the business database. A separate application step should control that write after the checks succeed.
Use explicit rules for values
A valid value also needs a defined interpretation. The date 03/04/2026 is ambiguous without a regional date convention. A currency amount needs a currency code.
The function below accepts a non-negative amount with up to twelve integer digits and two decimal places. The format uses a decimal point.
import re
from decimal import Decimal
AMOUNT = re.compile(r"(?:0|[1-9][0-9]{0,11})\.[0-9]{2}")
def parse_amount(value: str) -> Decimal:
if not isinstance(value, str):
raise TypeError("Amount must be a string")
if AMOUNT.fullmatch(value) is None:
raise ValueError("Use up to 12 digits, a dot, and two decimals")
return Decimal(value)
The function rejects grouping marks, leading zeros, and exponent notation. Regional format conversion belongs in a separate step with explicit rules.
This function checks the amount format only. It does not check source evidence, tax rules, or the meaning of the amount.
A schema error, an evidence error, and a business-rule error should remain separate outcomes. Repeated model calls are unlikely to correct every category.
Measure the result that users need
A small test collection should include clear documents and difficult documents. Rotated pages, missing totals, and mixed date formats expose different failures.
Field correctness measures agreement with reviewed values. Evidence correctness measures whether the source supports each value. Review rate measures the share that needs a person.
Automatic acceptance needs a separate error measure. A service can reduce review work by accepting more results, but that change can increase incorrect database writes.
- Keep reviewed expected values for each test document.
- Count incorrect fields among automatically accepted results.
- Measure review rate and cost per accepted document.
- Repeat the same tests after model, prompt, schema, or OCR changes.
The final acceptance decision belongs to the application. The model supplies candidate data and evidence. Clear checks turn those candidates into usable records.