How to Intermediate · 4 min read

How to build reproducible ML pipelines

Quick answer
Build reproducible ML pipelines by using version control for code and data, containerization (e.g., Docker) for environment consistency, and workflow orchestration tools like Airflow or Prefect to automate and track pipeline steps. This ensures consistent results across runs and environments.

PREREQUISITES

  • Python 3.8+
  • Docker installed
  • pip install scikit-learn pandas prefect
  • Git installed and basic knowledge
  • Basic understanding of ML workflows

Setup environment and tools

Install necessary Python packages and tools to manage reproducibility. Use Docker to containerize your environment and Git for version control. Install workflow orchestration tools like Prefect to automate pipeline steps.

bash
pip install scikit-learn pandas prefect
# Verify Docker installation
docker --version
# Verify Git installation
git --version
output
pip installs packages
Docker version 24.0.2
git version 2.40.0

Step by step reproducible pipeline

This example builds a simple ML pipeline with data loading, preprocessing, training, and evaluation steps orchestrated by Prefect. The environment is containerized with Docker to ensure consistent dependencies.

python
import os
from prefect import flow, task
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score

@task
def load_data():
    iris = load_iris(as_frame=True)
    df = iris.frame
    return df

@task
def preprocess(df):
    X = df.drop(columns=['target'])
    y = df['target']
    return train_test_split(X, y, test_size=0.2, random_state=42)

@task
def train_model(X_train, y_train):
    model = RandomForestClassifier(random_state=42)
    model.fit(X_train, y_train)
    return model

@task
def evaluate_model(model, X_test, y_test):
    preds = model.predict(X_test)
    acc = accuracy_score(y_test, preds)
    print(f"Model accuracy: {acc:.4f}")
    return acc

@flow
def ml_pipeline():
    df = load_data()
    X_train, X_test, y_train, y_test = preprocess(df)
    model = train_model(X_train, y_train)
    evaluate_model(model, X_test, y_test)

if __name__ == "__main__":
    ml_pipeline()
output
Model accuracy: 1.0000

Common variations and best practices

  • Use Dockerfile to define exact environment dependencies and share with your team.
  • Track data and model versions with tools like DVC or MLflow.
  • Use workflow schedulers like Airflow or Prefect for complex pipelines and retries.
  • Set fixed random seeds in all steps to ensure deterministic results.
  • Store pipeline metadata and logs for auditing and debugging.
python
FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt ./
RUN pip install -r requirements.txt
COPY . ./
CMD ["python", "pipeline.py"]

Troubleshooting reproducibility issues

  • If results differ across runs, check for uncontrolled randomness and set random_state everywhere.
  • Ensure all dependencies are pinned to specific versions in requirements.txt or environment.yml.
  • If environment differs, rebuild your Docker container to sync dependencies.
  • Use logs and metadata to trace pipeline execution and identify failures.

Key Takeaways

  • Use version control for code and data to track changes and enable rollback.
  • Containerize your environment with Docker to guarantee consistent dependencies.
  • Automate pipeline steps with workflow tools like Prefect or Airflow for reliability.
  • Fix random seeds and pin package versions to ensure deterministic results.
  • Track metadata and logs to debug and audit pipeline runs effectively.
Verified 2026-04
Verify ↗