What kinds of code-graded assertions can you use on LLM outputs, and where do they break down?
Type it the way you would explain it to an interviewer. A few sentences is enough; you can't break anything.
Model answer: the part to lead with
The main families:
- Exact match - output equals reference. Works for classification, multiple choice, canonicalised short answers. Brittle to formatting ("$4.50" vs "4.5 dollars"), so normalise first (case, whitespace, number parsing).
- Contains / regex - the answer string appears somewhere; regex for structured patterns (dates, IDs). Prone to false positives ("the answer is not 42" contains "42") - anchor patterns and check for negations where it matters.
- Structural validation - parse the output: valid JSON, conforms to a schema/Pydantic model, required fields present, enum values legal. This is the workhorse for tool-calling and extraction tasks.
- Numeric with tolerance - extract the number, compare within epsilon; essential for math where formatting varies.
- Execution-based - run generated code against test cases in a sandbox; run generated SQL against a fixture database and compare result sets. The gold standard for code: functional equivalence, not textual similarity.
- Property checks - length bounds, no banned phrases, citation IDs actually exist in the retrieved set, links resolve. Cheap guardrail assertions.
Full worked answer
The main families:
- Exact match - output equals reference. Works for classification, multiple choice, canonicalised short answers. Brittle to formatting ("$4.50" vs "4.5 dollars"), so normalise first (case, whitespace, number parsing).
- Contains / regex - the answer string appears somewhere; regex for structured patterns (dates, IDs). Prone to false positives ("the answer is not 42" contains "42") - anchor patterns and check for negations where it matters.
- Structural validation - parse the output: valid JSON, conforms to a schema/Pydantic model, required fields present, enum values legal. This is the workhorse for tool-calling and extraction tasks.
- Numeric with tolerance - extract the number, compare within epsilon; essential for math where formatting varies.
- Execution-based - run generated code against test cases in a sandbox; run generated SQL against a fixture database and compare result sets. The gold standard for code: functional equivalence, not textual similarity.
- Property checks - length bounds, no banned phrases, citation IDs actually exist in the retrieved set, links resolve. Cheap guardrail assertions.
def grade(output: str, case: dict) -> bool:
data = json.loads(output) # structural
assert set(data) >= {"amount", "currency"} # schema
return abs(data["amount"] - case["amount"]) < 0.01 # tolerance
Where they break down: anything with many valid surface forms (summaries, explanations, conversational answers) - exact/contains checks either miss correct answers or pass wrong ones. Reference-based text metrics like BLEU/ROUGE correlate poorly with quality for open-ended generation, so the honest move is escalating those criteria to model-graded or human eval rather than pretending an n-gram overlap score means something.
The senior habit: design the task output format to be assertable - force JSON, force a final Answer: line, make the model cite chunk IDs - so more of your eval stays in the cheap deterministic tier.
What a good answer covers
- Exact match - output equals reference
- Contains / regex - the answer string appears somewhere; regex for structured patterns (dates, IDs)
- Structural validation - parse the output
- Numeric with tolerance - extract the number, compare within epsilon; essential for math where formatting…
- Execution-based - run generated code against test cases in a sandbox; run generated SQL against a…
Key terms: Exact match · Contains / regex · Structural validation · Numeric with tolerance · Execution-based · Property checks
Interviewer follow-ups to expect
How would you grade a summarisation task without a judge?
Your regex grader shows 95% pass but users complain - what's happening?
Worked answer from AI-Engineer-Interview-Questions by Om (MIT), reformatted for LLM Prep · answer text last updated 6 November 2026 · scoring and review schedule by LLM Prep