Topic 4 of 5 · 4 min read
Cheap assertions that catch regressions
Goal: Add cheap assertions to an eval config that catch a known regression without an LLM call.
A heuristic assertion is a small rule that checks one visible property of an output, such as a banned phrase, a length limit or a required citation. It does not prove the answer is good. It catches known failures at no cost on every run.
Check yourself · 1 of 3
After a prompt change, answers start with "I cannot browse the internet". Which assertion catches this cheaply on every run?
Why it matters
Most regressions are specific. A prompt edit makes the bot start with "I cannot browse the internet". A new model version doubles the answer length. A refactor stops attaching source links. Each of these is a one-line rule. Running them on every case, on every change, gives you a smoke alarm before you spend money on judges or human review.
How to do it
- Start from a real incident. Write the assertion after you see the failure, so it protects against something that happened.
- Pick the property you can see. Common ones: contains or does not contain a string, a regex, word or character count, a link or citation marker, language, JSON validity, email or phone patterns for leaked PII.
- Keep each assertion single-purpose. One rule, one name, so a failure points at the cause.
- Run them in your eval tool. promptfoo has built-in types such as
contains,not-contains,regex,is-jsonand customjavascriptorpythonchecks. - Review each new rule against 20 known good outputs to see that it does not fire on them.
Before and after
# Weak: one vague check
assert:
- type: contains
value: "refund"
assert:
- type: not-contains
value: "I cannot browse the internet"
- type: javascript
value: output.split(/\s+/).length <= 120
- type: regex
value: "\\[source: [a-z0-9-]+\\]"
- type: not-regex
value: "\\b\\d{3}-\\d{2}-\\d{4}\\b"
The second set checks tone boilerplate, length, a citation marker and a leaked ID pattern. Each line maps to a failure the team has seen.
What goes wrong
- Rule pile-up. Fifty assertions, nobody remembers why. Link each to its incident.
- Brittle patterns. A regex for a date format fails the day the format changes, and a banned word list misses synonyms.
- False comfort. Passing every rule does not mean the answer is correct, helpful or faithful.
- Gaming. Once a length limit exists, a model or prompt may satisfy it with a truncated, useless answer. Pair it with a content check.
Examples
Catching a citation regression
A support bot must end answers with a source tag like [source: kb-142]. After a prompt rewrite, a third of answers lose it. The assertion regex: "\\[source: kb-\\d+\\]" turns that into a failing count on the next run, before any customer sees it.
Length cap with a floor
A summary assertion allows 40 to 120 words. The cap catches rambling. The floor catches the shortcut where a model satisfies the cap by answering "See the document." Both bounds come from reading real accepted summaries.
Key points
- A heuristic assertion checks one visible property, and it runs on every case at no model cost.
- Write each assertion after a real failure and name the incident it guards against.
- Typical checks are contains and not-contains, regex, length limits, citation markers, JSON validity and PII patterns.
- Test every new rule against known good outputs to make sure it does not fire on them.
- Passing assertions show that known failures are absent, not that the answer is good.
Common mistakes
- Adding assertions for imagined problems instead of observed ones.
- Writing a regex that matches one phrasing of a banned idea and treating the idea as covered.
- Setting a length limit with no minimum, which rewards empty answers.
- Never testing a new rule against good outputs, so it fails correct answers.
Check yourself · 2 of 3
Why should a word-count cap come with a minimum length?
Check yourself · 3 of 3
You add a regex rule that flags phone numbers, to catch PII leaks in support replies. What should you do before relying on it?
Practice items, not counted towards your mastery.
Start here
- promptfoo: Assertions and metrics (opens in a new tab)DocsFree
Lists the deterministic assertion types (equals, contains, is-json, regex, javascript, python) with config examples.
- JSON Schema (opens in a new tab)StandardFree
The specification and learning material for required fields, types, enums and additionalProperties.
- Evaluating Large Language Models Trained on Code (Chen et al.) (opens in a new tab)PaperFree
Introduces HumanEval, functional-correctness grading by unit tests, and the unbiased pass@k estimator.