How would you implement CI/CD for ML pipelines?
Written by the mlprep team · Updated
How would you implement CI/CD for machine learning pipelines? What testing strategies would you use, and how does ML CI/CD differ from traditional software CI/CD?
ML CI/CD shares the goals of traditional software CI/CD — automate validation, catch regressions early, ship with confidence — but the artifacts and failure modes are fundamentally different. Code can be diffed; a model weight file can't. Data is a first-class citizen alongside code, which software CI/CD never has to handle.
Why ML CI/CD is harder#
In software, if the tests pass and the build is green, you ship. In ML, a training run that produces no Python errors can still produce a terrible model — because the data changed, because a feature has a new distribution, because hyperparameter sensitivity means today's training diverged from yesterday's. The "artifact" being tested (model weights) is stochastic and data-dependent.
A second challenge: experiments are fast to run, but training is slow. A full ML pipeline might take hours. CI/CD needs to be smart about what to rerun.
The testing pyramid for ML#
Unit tests for data transforms: Test your feature engineering code — the Python/SQL functions that compute features from raw data. These should be fast, deterministic, and run on every commit. Mock the data sources; test the transformation logic directly.
def test_compute_rolling_average():
input_df = pd.DataFrame({"value": [1, 2, 3, 4, 5], "ts": ...})
result = compute_rolling_average(input_df, window=3)
assert result["rolling_avg"].iloc[-1] == pytest.approx(4.0)Data validation tests: Validate that incoming data conforms to expected schema, value ranges, and distributions. Great Expectations or Pandera are popular here. Fail the pipeline if null rate on a required feature exceeds a threshold, or if a categorical feature has unexpected cardinality.
Model training tests: Run a shortened training job (small data slice, fewer epochs) and assert the model meets basic sanity checks: loss is decreasing, gradient norms are in range, output distribution is reasonable. This catches bugs like gradient computation errors, wrong loss function, or data loading bugs.
Evaluation gate: Compare the newly trained model against the current production model on a held-out test set. Only promote the model if it meets predefined thresholds — e.g., accuracy ≥ production accuracy, AUC ≥ 0.90, no regression on key slices (fairness-critical demographic groups, high-value user segments). This is the most important gate: it explicitly tests whether the new model is better.
Integration tests: End-to-end test the serving path with the new model — load the model, pass it sample inputs from the feature store, assert output shape, latency, and basic sanity of predictions.
The ML CI/CD pipeline structure#
Code push → Lint / Unit tests (fast, always run)
→ Data validation (on latest snapshot)
→ Training run (triggered on schedule or significant data change)
→ Evaluation gate vs. baseline
→ Shadow deployment / canary
→ Full rollout
Shadow deployment is underused but powerful: run the new model alongside production, log both outputs, compare behavior in production traffic without affecting users. Great for catching distribution shift between your held-out test set and actual live traffic.
Continuous training#
ML systems also need continuous training — periodic retraining triggered by new data, concept drift detection, or time-based schedules. This is orthogonal to CD but part of the broader MLOps CI/CD story. Every retrain should go through the same evaluation gate before promoting.
Tooling#
MLflow for experiment tracking and model registry. Kubeflow Pipelines or Metaflow for pipeline orchestration. DVC for data versioning (committing data checksums to git, storing actual data in S3). GitHub Actions or Jenkins for triggering pipelines. Weights & Biases for comparing evaluation metrics across runs.
The key discipline: treat trained models like build artifacts in software — versioned, evaluated before promotion, and deployable to a specific serving environment with a clear rollback path.
The key takeaway unlocks after you reveal an expert answer.
Key Takeaway
ML CI/CD extends software CI/CD by treating data and model artifacts as first-class citizens under test. Layer your tests: unit tests for transforms → data validation → training sanity checks → evaluation gate vs. production baseline → shadow deployment. The evaluation gate (does the new model beat the old one?) is the critical step that software CI/CD has no equivalent for.
Follow-up Questions to Test Yourself
Interviewers often pivot here once you cover the core concept.
- How would you version control datasets? What's the difference between DVC's approach and just storing data in S3 with timestamped paths?
- What does a good model card look like, and why is it important in an MLOps context?
- How would you handle the case where a new model passes all offline evaluation gates but then underperforms in production A/B testing?
How confident do you feel explaining this?
Track your understanding or flag it to revisit before your next loop.
Related Questions