Sub topic 12
Grounding values in the source text
This chapter assumes you can call an LLM API, read JSON, and use Pydantic models like the Invoice model from the schema chapter. It also assumes you already have the extracted document text in a variable named text.
Why validation is not enough
A Pydantic model checks that total is a number. It cannot tell whether that number appears in the document. A model can return a well-formed invoice number that it made up, and the output will pass validation.
Grounding checks close this gap. A value is grounded if you can find it in the source text. A value you cannot find is a candidate hallucination, so you flag it for review.
A simple grounding check
The check below normalizes whitespace and case in both the value and the source. It then tests whether the value appears as a substring. A missing value (None) counts as grounded, because the prompt told the model to return null instead of guessing.
def is_grounded(value, source_text):
if value is None:
return True
normalized_source = " ".join(source_text.split()).lower()
normalized_value = " ".join(str(value).split()).lower()
return normalized_value in normalized_source
invoice = extract_invoice(text, call_llm)
suspect = [name for name in ("vendor", "invoice_number")
if not is_grounded(getattr(invoice, name), text)]
print("Fields to review:", suspect)Here extract_invoice and call_llm come from the retry chapter. The result is a list of field names to send for review.
What to check and what to skip
Exact matching works for values the model copies from the page. These include names, invoice numbers, and identifiers.
It fails for values the model transforms. The prompt asks for dates as YYYY-MM-DD, but the document may say "3 March 2025". The model may also convert a currency symbol to a code. Checking these values literally produces false alarms.
| Field type | Example | How to ground it |
|---|---|---|
| Copied string | `invoice_number` | Substring match after normalization |
| Date | `invoice_date` | Parse the date, then look for its parts or several formats in the text |
| Amount | `total` | Format the number the ways the document might (`1,250.00`, `1250`) and search for each |
| Code from a symbol | `currency` | Map known symbols and names to the code |
Common errors
- Skipping normalization. Line breaks inside a PDF's text can split a vendor name across lines. Without collapsing whitespace, a correct value looks missing.
- Short values match by accident. A total of
5is a substring of almost any invoice. For short values, require a match at word boundaries or near a label such as "Total". - Treating a pass as proof. A grounded value can still be the wrong one, such as a subtotal copied into the
totalfield. Grounding catches invented values, not misread ones. - OCR noise. Scanned text often contains small recognition errors. A correct value may fail an exact match. Consider fuzzy matching, and treat a near match as a reason to review rather than to reject.
Using the result
Record which fields failed grounding. In later chapters, these flags feed the accuracy measurements and the routing rules for human review.
Exercise
An invoice says "Date: 3 March 2025". The model returns invoice_date as "2025-03-03". Why does is_grounded flag it, and how would you fix the check for dates?
Show answer
The string "2025-03-03" does not appear in the source text, because the model reformatted the date. Fix it by parsing the extracted date and testing whether the document contains it in other formats, for example "3 March 2025" or "03/03/2025". You could also have the model return the original date text in a separate field.