Sub topic 2
Handling Nondeterministic Output
Assume you can call an LLM, write Python assertions, and run tests in CI. LLM responses can vary even when the prompt and input stay the same. Sampling settings, backend changes, and model updates can change wording or structure. Regression tests must tolerate harmless variation without accepting real failures.
Repeating Tests Without Hiding Failures
Run important cases several times when measuring a change. First apply deterministic checks, such as valid JSON, required keys, length limits, and safety rules. Then calculate the case pass rate.
def pass_rate(results):
if not results:
raise ValueError("results must not be empty")
return sum(results) / len(results)
runs = [True, True, False, True, True]
rate = pass_rate(runs)
if rate < 0.80:
raise AssertionError(f"Case pass rate too low: {rate:.0%}")A single failure may be sampling noise. Repeated failures across the same case are stronger evidence of a regression. Keep the individual results so a failed aggregate can be investigated.
Judge-Based Checks
Some requirements are semantic, such as “the answer explains the trade-off accurately.” An LLM judge can score these requirements against a rubric. Use a structured result, for example a Boolean decision and a short reason.
Validate the judge before trusting it. Have people label a representative sample, including borderline cases. Compare judge decisions with those labels. If agreement is weak, revise the rubric, judge prompt, or test set. Do not use a judge to replace checks that ordinary code can perform.
Baselines and Pass Thresholds
Store a baseline with the dataset version, model identifier, parameters, and pass rate. A regression gate should fail only when the current result is below the baseline by more than an allowed tolerance.
def check_regression(current, baseline, tolerance=0.02):
if current < baseline - tolerance:
raise AssertionError(
f"{current:.2%} is below the allowed baseline "
f"{baseline:.2%} - {tolerance:.2%}"
)
check_regression(current=0.94, baseline=0.95)Choose thresholds from repeated measurements, not convenience. A tolerance that is too small creates noisy failures. One that is too large hides real regressions. Update a baseline only after reviewing the failure and confirming that the new behavior is intended.
Exercise
A case passes 96 of 100 repeated runs. Its baseline is 98%, with a 3% tolerance. Should CI fail?
Show answer
No. The lower limit is 95%, and the current 96% pass rate is above it. Investigate the four failures, but do not fail this gate.