Sub topic 8
Quality Gates: Thresholds and Baseline Comparison
Prerequisites: you should know Python, pytest, pull requests, and basic model evaluation. You should also understand that a model metric is meaningful only when the evaluation data and metric definition stay consistent.
A quality gate is an automated rule that decides whether a candidate model can continue through the ML pipeline. The gate usually checks two conditions:
- The candidate meets an absolute minimum, such as accuracy of at least
0.85. - The candidate does not regress too far compared with a trusted baseline.
The baseline is the model or metric result currently accepted for delivery. It should have a recorded model version, data version, code revision, and evaluation configuration. Without that information, a comparison may look precise but cannot be reproduced.
Absolute thresholds and baseline limits
An absolute threshold protects the minimum user-visible quality. A baseline comparison protects against unexpected regressions when the current model is already above that minimum.
For example, suppose the minimum accuracy is 0.85, the baseline accuracy is 0.91, and the permitted drop is 0.02:
| Check | Result | Decision |
|---|---|---|
| Candidate accuracy | 0.90 | Passes the 0.85 minimum |
| Drop from baseline | 0.01 | Passes the 0.02 limit |
| Overall gate | — | Pass |
A candidate with accuracy 0.84 fails the absolute threshold. A candidate with accuracy 0.88 passes the minimum but fails the baseline comparison because it dropped by 0.03.
The metric's direction matters. For accuracy, precision, and recall, higher is usually better. For latency, error rate, and cost, lower is better. A quality-gate function must encode this deliberately instead of assuming that every metric should increase.
Implementing a readable gate
Return a list of failures rather than only True or False. CI logs can then explain exactly why a pull request failed.
def check_quality_gate(
candidate: dict[str, float],
baseline: dict[str, float],
min_accuracy: float = 0.85,
max_drop: float = 0.02,
) -> list[str]:
failures = []
candidate_accuracy = candidate["accuracy"]
baseline_accuracy = baseline["accuracy"]
if not 0.0 <= candidate_accuracy <= 1.0:
failures.append(
f"accuracy {candidate_accuracy:.3f} is outside [0, 1]"
)
if candidate_accuracy < min_accuracy:
failures.append(
f"accuracy {candidate_accuracy:.3f} is below "
f"the minimum {min_accuracy:.3f}"
)
drop = baseline_accuracy - candidate_accuracy
if drop > max_drop:
failures.append(
f"accuracy dropped by more than {max_drop:.3f} "
f"(baseline {baseline_accuracy:.3f}, "
f"candidate {candidate_accuracy:.3f})"
)
return failures
def test_gate_passes_for_small_change():
assert check_quality_gate(
{"accuracy": 0.90},
{"accuracy": 0.91},
) == []
def test_gate_fails_for_regression():
failures = check_quality_gate(
{"accuracy": 0.80},
{"accuracy": 0.91},
)
assert len(failures) == 2The example treats a drop equal to max_drop as acceptable. State this boundary in the test and documentation. If the evaluation process has sampling noise, use a confidence interval, repeated evaluation, or a statistical comparison instead of reacting to every small change.
Choosing and operating a gate
Set thresholds from product requirements, not from a convenient result on one experiment. Record the reason for each threshold. Review it when the data distribution, model purpose, or business cost changes.
Keep gate inputs as versioned artifacts:
- candidate metrics
- baseline metrics
- evaluation-data identifier
- model and code revisions
- metric configuration
- gate configuration
A gate should block delivery when it detects a known unacceptable result. It should not silently approve missing metrics, invalid ranges, or a missing baseline. Treat those conditions as infrastructure failures that require investigation.
Exercise
The baseline accuracy is 0.90, the minimum is 0.85, and the maximum allowed drop is 0.02. Does a candidate accuracy of 0.87 pass? Explain.
Show answer
No. It passes the absolute minimum, but the drop is 0.03, which exceeds the allowed drop of 0.02.