Sub topic 8
Running the suite in CI
This chapter assumes you can write pytest tests, call an LLM API from Python, and read a GitHub Actions workflow. It also assumes you already have a golden dataset and a pass-rate baseline from earlier chapters. Here you will connect them to a pipeline so every relevant change is checked before it merges.
Why CI is the right place
Prompt edits, model changes, and parameter tweaks are code changes. They go through pull requests, so a pull request check is the natural gate. A check that only runs on a developer's laptop gets skipped. A check that runs on every relevant pull request does not.
The pipeline has four jobs. It decides when to run, runs the cases, compares the result with the baseline, and blocks the merge when the result is worse.
Per-case tests with pytest
The first building block turns each line of the JSONL dataset into one pytest case. A failing case then shows up by its id, which makes triage easier.
import json
from pathlib import Path
import pytest
from app.llm import run_prompt # your own wrapper around the model call
CASES = [
json.loads(line)
for line in Path("evals/cases.jsonl").read_text(encoding="utf-8").splitlines()
if line.strip()
]
@pytest.mark.parametrize("case", CASES, ids=[c["id"] for c in CASES])
def test_case(case):
output = run_prompt(case["input"])
data = json.loads(output)
assert set(case["required_keys"]) <= set(data)
for phrase in case.get("must_contain", []):
assert phrase.lower() in output.lower()Each case checks three things. The output must parse as JSON, contain the required keys, and include the required phrases. These are the deterministic checks from earlier chapters.
A suite-level gate
Per-case assertions are strict. If one case in a hundred fails because the model varied its wording, the build goes red even though nothing regressed. For a nondeterministic system, the decision should be made on the whole suite. Compare the current pass rate with the stored baseline, and fail only when it drops by more than the tolerance.
def pass_rate(results):
return sum(1 for passed in results if passed) / len(results)
def check_regression(current, baseline, tolerance=0.02):
if current < baseline - tolerance:
raise SystemExit(
f"Pass rate {current:.2%} is below baseline {baseline:.2%} "
f"minus tolerance {tolerance:.0%}"
)SystemExit with a message makes the script exit with a non-zero status. CI treats that status as a failed step.
Both approaches are valid, but they need different handling. If you keep the per-case tests as hard failures, mark known flaky cases or rerun them. If you gate on the pass rate, collect the results with a pytest plugin or a small runner script, then pass the list of booleans to pass_rate.
The workflow file
This GitHub Actions workflow runs the suite only when files that can change model behavior are touched.
name: prompt-regression
on:
pull_request:
paths:
- "prompts/**"
- "evals/**"
- "app/**"
jobs:
evals:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.12"
- run: pip install -r requirements.txt
- run: pytest evals -q
env:
LLM_API_KEY: ${{ secrets.LLM_API_KEY }}Three details matter here.
- Path filters. The
pathslist keeps documentation-only pull requests from spending API money. Add any directory that holds prompts, templates, retrieval configuration, or model settings. - Secrets. The API key comes from repository secrets and is passed as an environment variable. Never write it into the file or print it in logs.
- Pinned runtime. Fixing the Python version and installing from
requirements.txtremoves one source of change, so a failure points at your prompt or model and not at your tooling.
Common errors
| Problem | Cause | Fix |
|---|---|---|
| Secret is empty on a pull request from a fork | GitHub does not expose secrets to fork workflows | Run evals on a trusted branch, or require maintainer approval |
| Suite is flaky | One run per case against a nondeterministic model | Repeat runs and gate on the pass rate |
| Build fails on rate limits | Cases run in parallel against the API | Limit concurrency and retry with backoff |
| Workflow never triggers | The `paths` filter misses where prompts live | Add the missing directory |
| Baseline file differs by branch | Baseline stored outside version control | Commit it, and change it only in a reviewed pull request |
Putting it together
A practical setup runs the per-case tests, writes the pass/fail results to a file, and then calls check_regression as the final step. The step that fails should print the current rate, the baseline, and the tolerance, as the function above does. The reviewer then knows why the check is red without opening the logs.
Exercise
Your workflow runs on a pull request that only edits README.md, and it spends API credits. What change to the workflow fixes this, and where would you add a new directory such as templates/ that holds prompt templates?
Show answer
Add a paths filter under on.pull_request so the workflow runs only when matching files change. Put "templates/**" in that same paths list, next to prompts/**, evals/**, and app/**, so changes to templates still trigger the suite.
Exercise
The baseline pass rate is 0.92 and the tolerance is 0.02. Does a current pass rate of 0.89 fail check_regression? What about 0.91?
Show answer
0.89 fails, because the threshold is 0.92 - 0.02 = 0.90 and 0.89 is below it. 0.91 passes, because it is not below 0.90.