Sub topic 9
Regression Testing Prompt Changes
This chapter assumes you can render a template with validated variables, store templates as versioned files, and load a pinned version. Here you will build a small test set that compares prompt versions before you release one.
Why prompts need regression tests
A prompt edit can fix one case and break another. You often can't see the damage by reading the diff. A regression test runs the same fixed inputs against a prompt version and checks the outputs. If an earlier behavior stops working, the test shows it before users do.
Model output varies, so you can't compare it to an exact expected string. Instead, check properties that should always hold. Examples are required words, a maximum length, or a valid JSON shape.
Build a test set
A test set is a list of cases. Each case has an input and the checks its output must pass. Start with five to ten cases. Pick them from real inputs, and add a case every time a bug is reported.
cases = [
{"text": "Cats sleep a lot.", "must_contain": ["cat"]},
{"text": "The meeting moved to Friday.", "must_contain": ["Friday"]},
]
def run_suite(generate, prompt_version):
failures = []
for case in cases:
output = generate(prompt_version, case["text"])
for word in case["must_contain"]:
if word.lower() not in output.lower():
failures.append((case["text"], word))
return failuresgenerate is a function you supply. It loads the given prompt version, renders it with the input, calls the model, and returns the text. Passing it in keeps the suite independent of any one model client. The suite returns a list of failures. An empty list means every check passed.
Compare two versions
Run the same suite against the current pinned version and the candidate. Compare the failure lists.
def compare(generate, current, candidate):
before = set(run_suite(generate, current))
after = set(run_suite(generate, candidate))
return {
"new_failures": sorted(after - before),
"fixed": sorted(before - after),
}The new_failures entry lists regressions, which are the cases that passed on the current version and fail on the candidate. The fixed entry lists improvements. Both matter. A candidate that fixes three cases and breaks one needs a decision from a person.
Reduce flakiness
The same prompt can pass on one run and fail on the next. Three habits help:
- Set the temperature low, or to zero where the model allows it, during tests.
- Check for content, not exact wording.
"friday" in output.lower()survives rephrasing.output == "The meeting moved to Friday."does not. - Run a flaky case several times. Treat it as failed if it fails in more than an agreed share of runs.
Common errors
| Mistake | Effect | Fix |
|---|---|---|
| Testing only the candidate | You can't tell a new failure from an old one | Run the pinned version too and compare |
| Exact string matching | Harmless rewording fails the test | Check required content or structure |
| Cases that all look alike | Edits that affect other inputs go unnoticed | Include short, long, and awkward inputs |
| Changing cases and prompt together | You can't tell which change caused a result | Commit test changes separately |
| Never adding cases | The suite stops reflecting real problems | Add a case for each reported bug |
What a passing suite means
A passing suite says the candidate keeps the behaviors you wrote down. It says nothing about behaviors you didn't test. Treat the result as one input to the review step, along with the change note and a read of a few sample outputs.
Keep the test file in the same repository as the prompts, so a prompt change and its test changes appear in the same pull request. Run the suite in your continuous integration job, so a failing candidate blocks the merge.
Exercise
A candidate prompt version passes all cases in your suite, but it fails two cases that the pinned version passed. Why can't that happen, and what does the situation tell you?
Show answer
It can't happen. If the candidate fails two cases that the pinned version passed, then the candidate does not pass all cases. The statement contains a contradiction, so one of the two reports is wrong. The likely causes are a suite that was run against the wrong version, or flaky cases that passed on one run and failed on another. Check which version generate loaded, rerun both versions, and fix any flaky cases.
Exercise
Your suite checks that the summary of "The meeting moved to Friday." contains "Friday". The candidate prompt now returns "The meeting is on Fri." Does the check pass, and how would you change the case?
Show answer
The check fails because "friday" is not in "the meeting is on fri." If "Fri." is acceptable, change the case to accept either form, for example by allowing a list of alternatives where any one match passes. If only the full word is acceptable, the failure is a real regression and the prompt needs editing.