C.W.K.
Stream
Lesson 02 of 12 · published

Prompt Regression Testing

~12 min · prompts, regression, drift

Level 0Apprentice
0 XP0/101 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

Prompts rot silently

Prompts are code. They have bugs. They drift when the underlying model changes. They get tweaked for one use case and break another. Prompt regression tests are unit tests for prompts.

The shape of a prompt regression suite

Each test:

  • Calls the production prompt with a known input.
  • Asserts properties of the output: 'contains the user's name', 'is JSON-parseable', 'doesn't include forbidden phrase X', 'cites at least one source'.
  • Doesn't assert exact wording (LLMs vary). Asserts structural / semantic invariants.

Common regression patterns

  1. JSON schema — the prompt is supposed to return JSON; the test validates against a schema.
  2. Forbidden phrases — the answer must not say 'I cannot help with that' for a topic we explicitly support.
  3. Citation density — the answer must reference at least N sources from the context.
  4. Tone / persona — answer must be in the agreed voice (Pippa's sassy reading; a brand's professional tone).
  5. Refusal correctness — for genuinely off-topic / unsafe inputs, the model must refuse.

Code

Prompt regression test (Python)·python
# tests/test_prompt_regression.py
import pytest, json
from myapp.llm import answer

@pytest.mark.eval
def test_returns_valid_json_for_summary_prompt():
    out = answer(prompt='summarize-doc', input={'doc': SAMPLE_DOC})
    parsed = json.loads(out)  # raises if not valid JSON
    assert 'summary' in parsed
    assert isinstance(parsed['summary'], str)
    assert len(parsed['summary']) > 100

@pytest.mark.eval
def test_does_not_refuse_on_supported_topic():
    out = answer(prompt='biology-q', input={'q': 'How does photosynthesis work?'})
    assert 'cannot help' not in out.lower()
    assert 'sorry' not in out.lower()

@pytest.mark.eval
def test_cites_at_least_one_source():
    out = answer(prompt='rag-q', input={'q': 'When was X founded?'})
    # answer must include a citation pattern like [1] or (source: ...)
    assert any(p in out for p in ['[1]', '(source:', 'According to'])

External links

Exercise

Pick the most-trafficked prompt in your service. Write 3 regression tests covering: shape (JSON / format), content (must include / must not include), and refusal behavior. Add to CI under a @pytest.mark.eval marker.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.