Sub topic 7
Gating Promotion on Evaluation Results
Assume you know Python functions, dataclasses, and the basic train–validate–test workflow. You should also know that a model artifact is the saved model and related files that another process can load for prediction. This chapter shows how to stop a model from being promoted when its evaluation results are not good enough.
Why add an evaluation gate?
Training code can finish successfully while producing a poor model. A successful process only means that the program did not crash. It does not mean that the model meets the quality required by your application.
An evaluation gate is a rule that compares measured results with release criteria. The gate passes only when every required condition is true. A failed gate stops promotion to the next environment, such as staging or production.
For a classifier, a simple gate might require accuracy of at least 0.80. A production gate may also check recall, fairness measures, prediction latency, model size, or the difference between validation and test results.
The gate should run after evaluation and before registration or deployment:
Define the gate as code
Keep the gate separate from training logic. A training function should create a model. An evaluation function should calculate metrics. A gate should decide whether those metrics satisfy the release policy.
Representing the policy as data makes it visible and testable:
from dataclasses import dataclass
@dataclass(frozen=True)
class EvaluationPolicy:
min_accuracy: float = 0.80
min_recall: float = 0.75
max_latency_ms: float = 100.0
def evaluate_gate(metrics: dict[str, float], policy: EvaluationPolicy) -> None:
failures = []
if metrics["accuracy"] < policy.min_accuracy:
failures.append(
f"accuracy {metrics['accuracy']:.3f} < {policy.min_accuracy:.3f}"
)
if metrics["recall"] < policy.min_recall:
failures.append(
f"recall {metrics['recall']:.3f} < {policy.min_recall:.3f}"
)
if metrics["latency_ms"] > policy.max_latency_ms:
failures.append(
f"latency {metrics['latency_ms']:.1f} ms > "
f"{policy.max_latency_ms:.1f} ms"
)
if failures:
raise ValueError("Evaluation gate failed: " + "; ".join(failures))
metrics = {
"accuracy": 0.84,
"recall": 0.79,
"latency_ms": 62.0,
}
evaluate_gate(metrics, EvaluationPolicy())
print("Model passed the evaluation gate")The function raises an exception when a rule fails. A command-line runner or orchestrator can treat that exception as a failed pipeline step. The error includes each failed condition, which makes the result easier to investigate.
Do not silently replace missing metrics with zero or skip a rule because a value is unavailable. Missing data should fail the gate clearly. Otherwise, a broken evaluation step could accidentally allow promotion.
Choose thresholds and comparisons
A threshold is meaningful only when it reflects a product requirement or an agreed baseline. A value such as 0.80 should have a reason. It may come from a business requirement, a previous model, or an offline study.
Use the metric that matches the error cost:
- Accuracy measures the fraction of all predictions that are correct.
- Precision measures how often positive predictions are correct.
- Recall measures how many actual positives the model finds.
- A latency limit protects the prediction service from slow responses.
Some applications need multiple conditions. For example, a fraud detector may require recall above a minimum while limiting the false-positive rate. Requiring only one metric can hide a serious regression in another.
Compare the candidate with a baseline when absolute thresholds are not enough. A candidate might pass an accuracy threshold but still be much worse than the current production model. Store the baseline metrics with the run and make the comparison part of the gate.
Make promotion auditable
Record the candidate model identifier, data version, code revision, configuration fingerprint, metrics, policy values, gate result, and failure messages. These records answer why a model was promoted or rejected.
Promotion should also be a separate action from training. The pipeline can save an evaluated artifact first. Only a later step, after the gate passes, can register it as a candidate for deployment. This separation prevents a failed run from changing the production pointer.
Exercise
A model has accuracy 0.86, recall 0.71, and latency 50 ms. The policy requires accuracy at least 0.80, recall at least 0.75, and latency at most 100 ms. Should the model be promoted?
Show answer
No. Accuracy and latency pass, but recall fails the minimum requirement, so the evaluation gate must reject the model.