Sub topic 13
Measuring extraction accuracy
This chapter assumes you can call an LLM, validate output with a Pydantic model, and read JSON. Validation tells you the output has the right shape. It does not tell you the values are right. For that you need a labeled test set and a metric.
Build a small labeled test set
Collect 20 to 50 real documents that represent what you will see in production. Include clean files, scans, and odd layouts. For each one, write the correct values by hand in the same shape as your schema. These are the gold labels.
Keep the labels in a file, for example one JSON object per document. Do not tune your prompt on the same documents you use for the final score. Set aside a few as a held-out set.
Score each field
Overall accuracy hides problems. A pipeline can get the vendor right 98% of the time and the total right only 70% of the time. Score each field on its own.
def field_accuracy(predictions, labels, fields):
correct = {f: 0 for f in fields}
for pred, gold in zip(predictions, labels):
for f in fields:
if pred.get(f) == gold.get(f):
correct[f] += 1
return {f: correct[f] / len(labels) for f in fields}
labels = [
{"vendor": "Acme Ltd", "invoice_number": "A-1", "total": 120.0},
{"vendor": "Birch GmbH", "invoice_number": "B-7", "total": None},
{"vendor": "Cedar Inc", "invoice_number": "C-3", "total": 45.5},
]
predictions = [
{"vendor": "Acme Ltd", "invoice_number": "A-1", "total": 120.0},
{"vendor": "Birch GmbH", "invoice_number": "B-7", "total": 99.0},
{"vendor": "Cedar Inc", "invoice_number": "C-8", "total": 45.5},
]
print(field_accuracy(predictions, labels, ["vendor", "invoice_number", "total"]))This prints {'vendor': 1.0, 'invoice_number': 0.6666666666666666, 'total': 0.6666666666666666}. The second document shows why null labels matter. The label says the total is absent, and the model invented 99.0. That is a hallucination, and exact match counts it as an error.
Common errors in scoring
- Format mismatches.
"2025-01-05"and"5 Jan 2025"mean the same date but fail exact match. Normalize dates, currency codes, and whitespace in both predictions and labels before comparing. - Float comparison. Compare numbers with a small tolerance, or round to the cents you care about.
- Counting only found values. Track missing values separately. A model that returns null too often looks safe on precision and bad on recall.
- Scoring too few documents. With 10 documents, one error moves a field by 10 points. Treat small differences as noise.
Read the failures
The number tells you where to look. The errors tell you what to fix. Print every wrong field next to its label and the source text, then group the errors by cause.
| Cause | Typical fix |
|---|---|
| Text extraction lost a table | Change the ingestion path or send the page image |
| Prompt is ambiguous about a field | Tighten the field description |
| Model guessed a missing value | Stress the null rule, add a grounding check |
| Label was wrong | Correct the gold data |
Rerun the evaluation after each change and keep the scores. A change that improves one field and breaks another is common.
Exercise
A model scores 95% exact-match accuracy on invoice_date, but most errors are cases where the model wrote "March 3, 2025" and the label says "2025-03-03". Is the model wrong 5% of the time? What should you change?
Show answer
Not necessarily. The values mean the same date, so the mismatch comes from format, not extraction. Normalize dates in both predictions and labels before comparing, or enforce YYYY-MM-DD in the prompt and schema. Then recompute the score to see the real error rate.