Sub topic 16
Routing uncertain results to human review
This chapter assumes you have a validated Invoice model, a grounding check like is_grounded, and a labeled test set with field_accuracy. Here you will use those signals to decide which extractions a person must look at.
Why route instead of trusting everything
No extraction pipeline is perfect. Human review of every document is slow and expensive. The goal is to review only the documents that are likely to be wrong, and to let the rest pass automatically.
An LLM does not give you a reliable confidence score. It can state a wrong value with full certainty. So build confidence from signals you can check yourself.
Signals that suggest a result is uncertain
- Validation failed after all retries.
- A required field is null, such as
invoice_number. - A value failed the grounding check, so it does not appear in the source text.
- The text came from OCR and was very short or noisy.
- Two extraction runs on the same document disagreed on a field.
- A business rule failed, such as a total that is negative or a date in the future.
Each signal is cheap to compute. Together they catch many errors that the schema alone cannot.
A routing function
The function below collects reasons and returns a decision. It reuses is_grounded, Invoice and text from earlier chapters.
from datetime import date
def review_reasons(invoice, source_text, from_ocr=False):
reasons = []
for name in ("vendor", "invoice_number"):
value = getattr(invoice, name)
if not value:
reasons.append(f"{name} is missing")
elif not is_grounded(value, source_text):
reasons.append(f"{name} not found in source")
if invoice.total is None:
reasons.append("total is missing")
elif invoice.total <= 0:
reasons.append("total is not positive")
if invoice.invoice_date and invoice.invoice_date > date.today().isoformat():
reasons.append("invoice date is in the future")
if from_ocr and len(source_text.strip()) < 200:
reasons.append("OCR text is very short")
return reasons
def route(invoice, source_text, from_ocr=False):
reasons = review_reasons(invoice, source_text, from_ocr)
return ("human_review", reasons) if reasons else ("auto_accept", [])Dates in YYYY-MM-DD format compare correctly as strings, which is why the future-date check works without parsing.
Store the reasons with the document. A reviewer who sees "total not found in source" knows where to look first.
The overall flow
Closing the loop
Reviewer corrections are labeled data. Add them to your test set and recompute field_accuracy regularly. This shows which fields fail most often, and it tells you whether a prompt change helped.
Also sample a small share of auto-accepted documents for review. If errors appear there, your rules are too lenient.
Common errors
- Treating the model's own "confidence" text as a reliable number.
- Sending too much to review, so reviewers stop paying attention.
- Sending too little, so silent errors reach downstream systems.
- Dropping the reasons, which makes each review slower.
Exercise
Your pipeline auto-accepts a document whose invoice number passed validation and was found in the source text, but the total is wrong. Name one rule you could add to catch this kind of error, and one way to find out how often it happens.
Show answer
Add a check that the total is grounded in the source text, or that it matches the sum of line items if you extract them. To measure how often it happens, review a random sample of auto-accepted documents and compare their totals with the source, then track the result with field_accuracy on the total field.