Checks in code: exact match, contains, schema checks and regression runs
Evals & Guardrails · 10 questions
Short notes
Key idea
Many qualities can be checked by plain code, which is fast, free and exact: does the output match, contain the right fact, parse as valid JSON, stay under a length? Run these checks on every change, like unit tests.
Syntax
def check(case, output):
return {
"exact": output.strip().lower() == case["expected"],
"contains": all(k in output for k in case["must_include"]),
"no_banned": not any(b in output for b in case.get("banned", [])),
"json_ok": valid_json(output, Schema),
"short": len(output.split()) <= 80,
}
results = [check(c, run(c["input"])) for c in cases]
pass_rate = sum(all(r.values()) for r in results) / len(results)
Example
A regression run after editing the prompt: 47/50 pass, versus 49/50 before. The two new failures both drop the order number from the reply. Fix the prompt and re-run before merging.
Traps
- Exact match is too strict for free text; use "contains" or a judge.
- Checks that are too loose pass bad answers.
- Flaky results: run each case a few times at the settings you ship.
- Save outputs with scores so you can compare runs case by case.
Want the full lesson with step-by-step examples, a project and a certificate? Evals & Guardrails course, ₹180 →
- Question 1easy
What does this print?
labels = ["refund", "return", "payment", "delivery"] outputs = ["refund", "refund", "payment", "delivery"] print(sum(o == l for o, l in zip(outputs, labels)))
- Question 2medium
How many outputs pass the schema?
from typing import Literal from pydantic import BaseModel, ValidationError class T(BaseModel): category: Literal["refund", "delivery"] outputs = ['{"category": "refund"}', '{"category": "Refund"}', 'refund', '{"category": "delivery", "extra": 1}'] ok = 0 for o in outputs: try: T.model_validate_json(o) ok += 1 except ValidationError: pass print(ok) - Question 3medium
Versions A and B both pass 40 of 50 cases. What should you do?
- Question 4easy
Why must a regression run use the same cases for both versions?
- Question 5medium
What does this print?
answer = "Refunds reach your bank in 5 to 7 working days. Please share your OTP." forbidden = ["otp", "password", "pin"] print([f for f in forbidden if f in answer.lower()])
Checks in code: exact match, contains, schema checks and regression runs
Checks in code: exact match, contains, schema checks and regression runs
Checks in code: exact match, contains, schema checks and regression runs
Checks in code: exact match, contains, schema checks and regression runs
Checks in code: exact match, contains, schema checks and regression runs
5 more questions on this topic, with hints, and tests
Sign up free for 21 days of every question and test.