Extracting fields from a document or feed is the easy part. The harder question is whether the extracted values can be trusted. This post walks through why source data is inconsistent and what a structured record needs beyond extraction. The code is illustrative pseudocode, not a production design.
Why source structures drift
Sources are built by different people, for different purposes, in different locales. Two systems describing the same invoice might produce:
{"inv": "INV-0042", "date": "03/04/2025", "total": "1.250,00 EUR"} {"invoice_no": "42", "date": "3 Apr 2025", "total": "$1,250.00"} Field names, identifier conventions, date formats and number formatting all differ. Extraction can read both, but it cannot tell you they describe comparable records.
Missing, malformed and ambiguous are different problems
- Missing: the field is absent or empty.
- Malformed: a value is present but breaks the expected format, such as
2025-13-40. - Ambiguous: a value parses cleanly but supports more than one reading.
03/04/2025is 3 April or 4 March.1.250is one thousand two hundred fifty or one and a quarter.
They need different handling. Missing values may be requested from the source, malformed ones may be normalised or rejected, and ambiguous ones cannot be resolved by the parser at all, because the missing ingredient is context.
Extraction is not validation
Extraction answers "what text is in this region or field?" Validation answers "is this value acceptable?" It helps to think in layers:
- Syntactic: does the value match the expected format?
- Semantic: are values plausible and internally consistent, for example line items summing to the total?
- Referential: does the value match something that exists, such as a known customer ID?
Dates, currency and identifiers
Dates. Store ISO 8601 internally, but keep the original string and the source locale. If the locale is known, 03/04/2025 has one reading. If it isn't, flag it rather than guess.
Currency. Parse separators according to the source locale, store the amount as a decimal type (not a float) and keep an explicit currency code alongside it.
Identifiers. Trimming whitespace and normalising case is usually safe. Stripping leading zeros is not: 0042 and 42 may be different keys. Decide per source and write the rule down.
A small validation sketch
def validate_invoice(rec, ctx): issues = [] if not rec.get("invoice_no"): issues.append(("missing", "invoice_no")) parsed = parse_date(rec.get("date"), ctx.source_locale) if parsed.error: issues.append(("malformed", "date")) elif len(parsed.readings) > 1: issues.append(("ambiguous", "date")) amount = parse_amount(rec.get("total"), ctx.source_locale) if amount is None: issues.append(("malformed", "total")) elif amount != sum(l.amount for l in rec["lines"]): issues.append(("inconsistent", "total")) if rec.get("customer_id") not in ctx.known_customers: issues.append(("unmatched", "customer_id")) return ("accepted" if not issues else "review"), issues The point is the return shape: a status plus typed reasons, not a boolean.
Designing exception handling
Silent coercion is how bad records enter a system unnoticed. A workable pattern:
- Keep the raw input untouched alongside the parsed output.
- Route failures to a quarantine queue with a reason code.
- Record who resolved each item and how.
- Turn recurring resolutions into explicit rules.
That last step matters. An exception queue that never feeds back into the rules will keep refilling with the same cases.
Consistency across multiple sources
Map each source into one canonical schema, and version those mappings so changes are traceable. Keep provenance (source system, original value, mapping version) on every record. Names illustrate the matching problem: Jay Menon, Menon, Jay and J. Menon need a normalisation and matching strategy, and weak matches should go to review instead of being merged automatically.
Where human review still helps
Ambiguity is a context problem, not a logic problem. A reviewer who can see the original document, the candidate readings and related records can often resolve a case that no rule could. The goal is not to review everything, only the cases validation has flagged as unresolvable, with enough information on screen to decide quickly.
Takeaways
- Treat missing, malformed and ambiguous values as separate categories.
- Validate in layers, and keep the raw value next to the parsed one.
- Route exceptions with reason codes instead of coercing silently.
- Feed resolved exceptions back into your rules.