Sub topic 3
Regression Testing Prompt Changes
Assume you know basic Python, Git, YAML, and how to call a large language model. A prompt change is a code change in behavior. Test it before releasing it.
Regression testing prompt changes
A regression test checks that a new prompt still handles known inputs correctly. Keep a small, fixed suite of representative cases. Each case can require words, structure, or safety behavior in the output.
Run the same cases against the current and candidate prompt versions. Review failures before changing the pinned production version.
cases = [
{"text": "Cats sleep a lot.", "must_contain": ["cat"]},
{"text": "The meeting moved to Friday.", "must_contain": ["Friday"]},
]
def generate(prompt_version, text):
# Replace this function with your model call.
return f"{text} (processed by prompt {prompt_version})"
def run_suite(prompt_version):
failures = []
for case in cases:
output = generate(prompt_version, case["text"])
for expected in case["must_contain"]:
if expected.lower() not in output.lower():
failures.append((case["text"], expected))
return failures
for version in ["1.1.0", "1.2.0"]:
print(version, run_suite(version))Tests should check meaning, not only exact wording. Include short, long, ambiguous, empty, and adversarial inputs when they matter to your application.
Reviewing and releasing a new version
Put the prompt, test changes, and change note in one Git pull request. Review whether the new instructions improve the intended behavior and whether they introduce unwanted behavior. Run the regression suite in continuous integration.
Release a new version only after review. Keep the previous version available so you can roll back without reconstructing it.
Tracing outputs back to prompt versions
Production logs should record the prompt name, exact version, model, temperature, and a request identifier. Avoid logging sensitive user text unless your privacy policy permits it.
import json
import logging
logger = logging.getLogger("llm")
def log_call(name, version, model, temperature, output):
logger.info(json.dumps({
"prompt": name,
"prompt_version": version,
"model": model,
"temperature": temperature,
"output_chars": len(output),
}))Prompt registries and tooling options
A prompt registry is a shared place to store, review, retrieve, and audit prompt versions. A Git directory is often enough for a small application. Larger teams may use a registry that adds access control, deployment aliases, evaluation results, and an API.
Choose the simplest tool that supports review, version pinning, rollback, and searchable history.
Exercise
Why should production use a pinned prompt version instead of always using the newest version?
Show answer
Pinning makes behavior reproducible and allows a known-good version to be restored when a new prompt causes failures.