Schedule icon
Download icon
Script icon
UploadFiles icon
Switch icon
Log icon
Set icon
Fail icon
If icon
SlackIncomingWebhook icon

Retrain a model on drift and promote it only if it beats the champion

Detect data drift with Evidently in Kestra, retrain a challenger, compare it with the champion on held-out data, promote only when it wins.

Categories
AIData

A model trained on last quarter's data keeps scoring after the world moved. Retraining on a schedule is wasteful when nothing changed, and retraining blindly can ship a worse model. This flow retrains only when Evidently detects that the input data drifted, then runs a champion / challenger comparison: the new model must beat the current one on held-out rows of the new batch by at least min_improvement_pct, or a person has to look.

It runs with no setup. The data is a public salaries dataset of about 3,000 rows, the model is a scikit-learn regressor that predicts salary_in_usd, and the models live in namespace files. Run it with simulate_drift: true to raise salaries by 60% and make every job remote, as a market shift would.

This blueprint was created by zkasuran.

How it works

  1. download_batch (io.kestra.plugin.core.http.Download) fetches the data.
  2. evaluate (io.kestra.plugin.scripts.python.Script, evidently==0.7.23, scikit-learn==1.7.2) loads the champion and its reference window from namespace files, then:
    • runs Evidently DataDriftPreset with fixed tests (ks for numbers, chisquare for categories) and saves the HTML report,
    • BOOTSTRAP when there is no champion: trains the first one,
    • STABLE when the drift share is under drift_share_threshold: no training,
    • otherwise trains a challenger on 70% of the new batch, scores both models on the other 30% by mean absolute error, and returns PROMOTE or HOLD.
  3. publish_drift_report (io.kestra.plugin.core.namespace.UploadFiles) keeps every report under ml/salary/reports/.
  4. decide (io.kestra.plugin.core.flow.Switch):
    • BOOTSTRAP: store the first champion and its reference window, record version 1 in KV.
    • PROMOTE: archive the old champion to ml/salary/archive/champion-v<n>.joblib, install the challenger, make its training data the new reference window, and bump the version in KV.
    • STABLE: log only.
    • HOLD: the run fails with both errors. The champion stays.
  5. log_outcome prints the verdict and the current champion. Slack is optional.

The reference window moves with the champion. Without that, a lasting market shift would be flagged as drift forever and every run after the first retrain would end in HOLD.

Inputs

  • source_url (URI): the data.
  • simulate_drift (BOOL, default false).
  • drift_share_threshold (FLOAT, default 0.3): share of columns with a p-value under 0.05.
  • min_improvement_pct (FLOAT, default 5.0): how much lower the challenger's error must be.
  • notify_slack (BOOL, default false).

Prerequisites

  • A Kestra worker that can run Docker containers. The pinned libraries install in about a minute per run.

Secrets

  • SLACK_WEBHOOK_URL: only when notify_slack is true.

Quick start

Run Input Outcome
1 defaults BOOTSTRAP, champion v1, MAE about 41,500
2 defaults STABLE, drift share 0
3 simulate_drift: true PROMOTE: remote_ratio and salary_in_usd drifted, champion MAE 95,501 vs challenger 67,092, v2 installed
4 simulate_drift: true STABLE: the shifted market is now the reference
5 simulate_drift: true, min_improvement_pct: 90 after a revert HOLD: the challenger is better, but not by 90%

Expected outputs

  • outputs.evaluate.vars: verdict, drift_share, drifted_columns, champion_mae, challenger_mae, improvement_pct.
  • Namespace files ml/salary/champion.joblib, ml/salary/reference.csv, ml/salary/archive/, ml/salary/reports/.
  • KV salary_model_champion: {version, mae, reason, replaced_mae, execution_id}.

How to extend

  • Register the promoted model in MLflow or another registry instead of namespace files.
  • Score the new batch with the champion in a task after decide.
  • Use your own features and target by editing CATS, NUMS and TARGET.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.