When an AI summary changes a price, date, or measurement, compare the summary against values you extract from the original source, then normalize equivalent formats before comparing. This worked example uses Python’s standard library to check a USD amount, a calendar date, and a metric weight; the code rejects values it cannot parse instead of silently treating them as correct.
Prerequisites and intended result
Use Python 3 and save the complete program below as check_summary.py, then run python check_summary.py in a terminal. No extra package or online service is required. The example assumes the original text is reliable and that the summary should preserve its three values exactly.
The check uses normalization, meaning conversion of different written forms into a shared representation. It converts both dates to YYYY-MM-DD and both weights to grams. It reads decimal numbers as Decimal values from text, avoiding binary floating-point representation differences described in the Python decimal documentation.
Worked example: compare the source with the summary
The fixture is invented and illustrative. It represents a deliberately narrow task: compare one USD amount, one delivery date, and one metric weight. The script looks only for the formats shown; add reviewed formats explicitly before using it on different documents.
Example input: The invented source says Invoice total: USD 1,250.00, Delivery date: 15 Apr 2026, and Package weight: 1,250 g. The invented AI summary says Total: USD 1,250.00, Delivery date: 2026-04-15, and Package weight: 1.25 kg.
Copy this entire program into check_summary.py:
import re
from datetime import datetime
from decimal import Decimal
source = """
Invoice total: USD 1,250.00
Delivery date: 15 Apr 2026
Package weight: 1,250 g
"""
summary = """
Total: USD 1,250.00
Delivery date: 2026-04-15
Package weight: 1.25 kg
"""
def one_match(pattern, text, label):
matches = re.findall(pattern, text, flags=re.IGNORECASE)
if len(matches) != 1:
raise ValueError(
f"{label}: expected one match, found {len(matches)}"
)
return matches[0]
def amount_usd(text):
value = one_match(
r"\bUSD\s*([0-9][0-9,]*(?:\.[0-9]{1,2})?)\b",
text,
"USD amount",
)
return Decimal(value.replace(",", ""))
def delivery_date(text):
value = one_match(
r"\b(?:\d{4}-\d{2}-\d{2}|\d{1,2} [A-Za-z]{3} \d{4})\b",
text,
"date",
)
formats = ("%Y-%m-%d", "%d %b %Y")
for date_format in formats:
try:
return datetime.strptime(value, date_format).date()
except ValueError:
pass
raise ValueError(f"Unrecognized date: {value}")
def weight_in_grams(text):
value, unit = one_match(
r"\b([0-9][0-9,]*(?:\.\d+)?)\s*(kg|g)\b",
text,
"metric weight",
)
number = Decimal(value.replace(",", ""))
if unit.lower() == "kg":
return number * Decimal("1000")
return number
checks = [
("USD amount", amount_usd(source), amount_usd(summary)),
("Delivery date", delivery_date(source), delivery_date(summary)),
("Weight in grams", weight_in_grams(source), weight_in_grams(summary)),
]
for label, source_value, summary_value in checks:
if source_value != summary_value:
raise AssertionError(
f"{label} differs: source={source_value}, summary={summary_value}"
)
print(f"PASS {label}: {source_value}")
The date parser uses explicit formats rather than guessing whether a number-first date means month/day or day/month. Python’s datetime.strptime() parses against the supplied format, and date.isoformat() provides the common year-month-day form; see the Python datetime documentation.
Expected output: The following is illustrative output based on the fixture; it is not a claim that this program was executed.
PASS USD amount: 1250.00
PASS Delivery date: 2026-04-15
PASS Weight in grams: 1250.00
Acceptance checks and normalization
Verify the result: Accept the summary only if the program reaches all three PASS lines without an exception. For the fixture, the amount must compare as 1250.00 USD, the date as 2026-04-15, and the weight as 1250.00 grams. The trailing zero in a Decimal value can preserve how the number was written; equality here checks the numeric value.
The regular expressions define the accepted input formats: a USD code before a comma-separated amount with up to two decimal places, either of two date forms, and a number followed by g or kg. Python’s re module matches the forms specified by a pattern; it does not infer every possible way people may write a value. See the Python re documentation.
The unit conversion is explicit: kilograms are multiplied by 1,000 to compare them as grams. This script does not round the result to a preferred display precision. If your task requires rounding, specify the rule and apply it consistently to both values; Decimal arithmetic uses a configurable precision and rounding context, as the documentation explains.
Specific failures and recovery
A missing or repeated match raises ValueError. For example, if the summary says Package weight: 1.25 without a unit, the program does not guess that the number means kilograms. Recover by checking the original and summary, then either correct the summary or add a format only after confirming its meaning.
A changed value raises AssertionError; for instance, USD 125.00 will not equal the source’s USD 1,250.00. A parseable value can still be wrong in context, so inspect the cited source passage before accepting or correcting it. Pattern matching is a format check, not proof that a statement is true.
This method is unsuitable when the source has conflicting figures, the unit is implicit, or dates are ambiguous, such as 04/05/2026. It also does not handle other currencies, non-metric units, exchange rates, date ranges, time zones, or values written in words. For those cases, resolve the meaning first, then extend the code with explicit, reviewed rules; do not silently treat a new format as equivalent.
Frequently Asked Questions (FAQ)
Q1) Can I use this for currencies other than USD?
Not as written: the amount pattern accepts only the USD code, and comparison does not convert currencies. Add an explicit currency field and compare both currency and amount. A $ symbol alone can be ambiguous when documents include more than one dollar currency.
Q2) What if dates use a different order or include a time?
Add an explicit format only after determining what the source means, then parse and compare the time zone as well if the time matters. A date-only check intentionally ignores time and time-zone differences. If the source contains ambiguous dates, confirm the intended calendar date before normalizing them.
Q3) Does a passing check prove the AI summary is accurate?
No; it shows only that these three extracted values match under the rules in the script. The patterns may miss a differently written value, and they cannot determine which figure is authoritative when the source conflicts with itself. Review the surrounding source text and check any facts beyond the amount, date, and weight yourself.