By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
Here is the assertion almost everybody writes first:
assert response.text == (
'Sale items may be returned within 14 days of delivery for store credit.'
)It passes once, during development, on the one day the model happened to phrase it that way. The next morning the model writes 'You can return sale items within 14 days of delivery; the refund comes as store credit.' — a strictly better answer — and the test fails. Congratulations: you have built a test that enforces the model's typing style.
Instead of comparing strings, you score the answer against a criterion and compare the score to a threshold. That single change is the foundation of everything else in this module.
There are four sources of scores, in increasing order of cost and flexibility:
| Scorer | How it works | Good for | Weakness |
|---|---|---|---|
| Deterministic rules | Regex, schema checks, keyword presence, length | Format, citations, forbidden phrases, PII patterns | Cannot judge meaning |
| Similarity | Compare embeddings of answer vs reference | Fast, cheap regression signal | Confuses 'similar words' with 'correct answer' |
| LLM-as-judge | A second model scores against a rubric | Semantic quality: faithfulness, relevance, tone | Costs calls; can be biased; needs calibration |
| Human review | A person scores against the rubric | Ground truth, calibration, ambiguous cases | Slow and expensive; not automatable |
Real suites use all four. Rules for things that are literally checkable, similarity as a cheap tripwire, a judge for meaning, humans to keep the judge honest.
Rules are underrated because they catch the boring failures that cost the most money:
import re
FORBIDDEN = [
r'\bI (have|will) (processed|issued) (a )?refund', # promising refunds
r'\bguarantee(d)? (delivery|arrival)', # promises we cannot keep
r'\b\d{4}[- ]?\d{4}[- ]?\d{4}[- ]?\d{4}\b', # card-like numbers
]
def check_rules(answer: str) -> list[str]:
return [pat for pat in FORBIDDEN if re.search(pat, answer, re.I)]
assert check_rules("I have processed a refund for you.") != [] # caught
assert check_rules("I can start a return for you.") == [] # fineAn AI test that says AssertionError is useless. Aim for output like:
FAIL q_014 'Can I return a gift card?'
rule : passed
faithfulness: 0.42 (threshold 0.85)
unsupported claims:
- 'Gift cards can be returned within 30 days'
retrieved : returns-policy, gift-cards-faq
fix hint : gift-cards-faq says non-returnable; answer contradicts itThat message tells an engineer which document to look at. A bare assertion tells them to run it locally and start guessing.
Key takeaways
Exact-match tests enforce phrasing, not correctness.
Score the answer against a criterion, then threshold the score.
Layer scorers: rules first, similarity next, judge last, humans to calibrate.
A failing AI test must explain which claim was unsupported and which document was used.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.