Sub topic 1
Why Prompts and Models Regress Silently
Prerequisites
Assume you can call an LLM API, write basic Python tests, and use Git and CI. This chapter focuses on detecting behavior changes rather than testing whether an API call succeeds.
Why Prompts and Models Regress Silently
A conventional unit test often expects one exact result. LLM output can be valid while still becoming worse. A small prompt edit may remove an instruction, change the answer style, or weaken a safety rule. A model upgrade may interpret the same prompt differently. New context can also change which facts the model notices.
These failures are silent because the request still returns HTTP 200 and produces readable text. Regression tests therefore need to check properties of the response, not only whether the call completed.
What Changes: Prompts, Model Versions, Parameters, and Context
Track every input that can affect behavior:
| Change surface | Example regression |
|---|---|
| Prompt | A deleted format instruction produces prose instead of JSON |
| Model or parameters | A model upgrade or higher temperature changes classifications |
| Context | Retrieved documents omit a required fact or add conflicting text |
Pin model versions when the provider supports it. Record the model, temperature, token limit, system prompt, retrieved context, and application code version with each evaluation result.
Building a Golden Dataset
A golden dataset is a versioned collection of representative test cases with known expectations. Store one case per JSONL line so Git can show small, reviewable changes.
Include normal inputs, boundary cases, adversarial instructions, empty inputs, and cases that require refusal. Store required keys, phrases, labels, or constraints instead of one exact answer when wording may reasonably vary.
Deterministic Checks: Format, Content, and Constraints
Start with checks that do not need another model. Parse JSON, verify required keys, check length limits, and search for required or forbidden phrases. Test safety constraints explicitly. Keep each check small so a failure identifies the broken contract.
import json
case = {
"required_keys": ["answer"],
"must_contain": ["refund"],
}
output = '{"answer": "Your refund is being processed."}'
data = json.loads(output)
assert set(case["required_keys"]) <= set(data)
for phrase in case["must_contain"]:
assert phrase.lower() in output.lower()Do not require exact text unless exact text is part of the product contract. Exact matching creates brittle tests and can hide the distinction between harmless wording changes and real regressions.
Exercise
Why should a golden case usually store required properties instead of one exact response?
Show answer
LLM wording can vary while still satisfying the product contract. Property checks detect meaningful failures without rejecting harmless variation.