Pick a champion model with guardrail metrics

MediumMLOpsPro

Why it matters Automated model promotion in CI pipelines uses rules like this.

Promotion rules optimise one metric subject to guardrails.

Write pick_champion(candidates, primary, guards). candidates is a list of dicts with a "name" and metric values. guards is a dict {metric: (op, threshold)} with op "<=" or ">=". Keep candidates that pass every guard, then return the name with the highest primary metric (ties broken by name). Return None if none pass.

Signaturedef pick_champion(candidates, primary, guards)

Example

two models where the more accurate one has latency 300 and the guard is latency <= 200 returns the other one.

Understand the idea

Picking which model to promote is usually one goal with hard limits: maximise accuracy, but only among models fast enough and safe enough. Guardrails filter first, and the primary metric only ranks the survivors. Worked example: candidates A (accuracy 0.92, latency 300, recall 0.90), B (0.90, 150, 0.80), C (0.90, 120, 0.85). Guards: latency <= 200 and recall >= 0.8. A fails latency, so it is out even though it is most accurate. B and C both pass (B's recall 0.80 meets >= 0.8 exactly). They tie on accuracy 0.90, so the name decides and 'B' comes first alphabetically. If every model failed a guard, return None. Rule: keep candidates where, for every guard (metric, (op, threshold)), the value satisfies <= threshold or >= threshold as stated; among survivors, choose the highest primary value, breaking ties by name; return None when none survive. Handle both '<=' and '>=' guards. Used in: automated model promotion in MLOps pipelines and choosing an LLM by quality under latency and cost limits.

solution.pyPython idle
Ctrl/Cmd + Enter runs sample tests

Run checks the first 2 tests. Submit checks the hidden tests with Pro.

  • Sample 1: examplepick_champion([{'name': 'a', 'acc': 0.9, 'lat': 300}, {'name': 'b', 'acc': 0.85, 'lat': 150}], 'acc', {'lat...
  • Sample 2: two guardspick_champion(_C, 'acc', {'lat': ('<=', 200), 'recall': ('>=', 0.8)})