AI invoice reader project · Chapter 5 of 12
Validation rules
By the end of this chapter every invoice the model reads is checked against the rules an invoice must obey: each line multiplies out, the lines and taxes add up to the total, the GSTIN's check character fits and the date is believable. Each broken rule comes back as a problem with its field and a sentence, and no rule ever changes a number. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Checking the model
- 3 tasks
- Python
- In the all labs plan
The first 2 chapters of AI invoice reader project are free. This one comes with the all labs plan.
Why you need this
Of Rafiq's 9 mistakes in September, three were of one kind: ₹18,400 typed as ₹14,800, a GSTIN typo that would have lost the GST input credit on Surat Steel's ₹16,002 of IGST, and a date typed month first. The model makes the same kind of mistakes, and chapter 4's POST /invoices/extract answers 200 for all of them. Here is what the model read from oil-mod-2291.pdf, printed Date: 01/10/2026, with a plain Total line before the taxes and a Grand Total after them:
{"supplier_gstin": "27AAECM5512B1ZX", "invoice_date": "2026-01-10", "subtotal": "38060", "cgst": "951.5", "sgst": "951.5", "igst": null, "total": "38060"}
The date is read month first (10 January), and the total is the plain Total, ₹1,903 of GST short. Models really do both. Two more from the inbox: ganesh-sg-1063.pdf prints line 4, Shimla Mirch, as 10 kg at ₹64.00 for ₹604.00, and surat-sst-0937.pdf prints the GSTIN 24AAGFS7671H1ZZ, which is not a valid GSTIN. The model copies both faithfully, because they are printed.
Nothing so far looks at the answer. If Rafiq has to check every field of every invoice himself, the reader saves him nothing.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
rules.py: do the amounts add up?
In app/validate/rules.py write check_lines(inv): for each line i (from 0) whose quantity times rate differs from its amount by more than settings.tolerance (Rs 1.00), Problem(f"line_items[{i}].amount", "line_amount", message). Then check_totals(inv): when there is a subtotal and the lines' amounts differ from it, Problem("subtotal", "subtotal", message); when the subtotal (or, with none, the lines' sum) plus cgst, sgst and igst (None as 0) differs from the total, Problem("total", "total", message). Each message is a sentence with the numbers in it.
The Check runs both on eleven invoices: a good one, a wrong line, 75 paise off (fine), Rs 1.50 off, a wrong subtotal, a total without the taxes, no subtotal, IGST, a bill of supply with no taxes.
- Task 2
gstin.py: the check character and look-alikes
In app/validate/gstin.py write check_char(first14): each character's value is its index in CHARS; multiply the values by 1, 2, 1, 2, ... (1 at index 0), add up product // 36 + product % 36 for each, and return CHARS[(36 - sum % 36) % 36]. Then gstin_problem(gstin): None when it fullmatches SHAPE and ends in check_char of its first 14; otherwise a sentence (the wrong shape, or what it ends in and what its check character should be). Then repair_gstin(raw): spaces and dashes removed, upper case; if not 15 characters or already valid, return that; else swap look-alikes (AS_DIGIT at DIGITS_AT, AS_LETTER at LETTERS_AT) and return the swapped one only if it is valid.
The Check works out the check character of every supplier's GSTIN, tries four bad ones and seven repairs.
- Task 3 · interview question
Interview: rules.py, every rule in order
Write check_dates(inv, today): an invoice_date after today gives [Problem("invoice_date", "date_future", message)]; more than settings.max_age_days (120) days before today gives [Problem("invoice_date", "date_old", message)]; otherwise []. Then check_invoice(inv, today=None): when there is a supplier_gstin, a copy with it repaired (inv.model_copy(update=...) with repair_gstin); today defaults to settings.today; return (the invoice, check_gstin + check_dates + check_lines + check_totals problems).
The Check tries four dates against 31 October, then a messy invoice (a spaced lower-case GSTIN, a future date, a wrong line and a wrong total), then a GSTIN with a typo with today left out, then POST /invoices/validate for ganesh-sg-1063.pdf (its line 4 says 10 x 64 = 604).
Kunal will ask: line 4 says 10 x 64 = 604. Your code knows the right amount is 640. Why not just set it?
What you will learn
- A problem, not an exception: a rule returns a list of Problems.
- Exact money: Decimal arithmetic, and why a tolerance of Rs 1.00.
- Lines and totals: quantity times rate; lines plus taxes make the total.
- The GSTIN: its shape, its check character, and repairing only characters that look alike.
- Dates: not after today, not more than 120 days before it.
- Why a rule never fixes a number.
More in Checking the model
Build "Validation rules" yourself
DabbaDrop's kitchens get piles of supplier invoices, typed and messy. You build the pipeline that reads them, checks every number, and sends the doubtful ones to a person. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of AI invoice reader project are free. This one comes with the all labs plan.