Model Deployment: From My Laptop to Production Without Crying

There's a special kind of silence that follows the question "great model, how do we use it?" Somewhere, a notebook named final_v3_REALLY_final.ipynb holds a model that scored beautifully. It depends on a dataset on someone's desktop, three cells that must be run in a specific order, and a pandas version nobody remembers.
Getting from there to production is mostly not about machine learning. It's about engineering.

1. Turn the notebook into a pipeline
Every preprocessing step that happened in the notebook must happen identically at prediction time. Bundle preprocessing and model together so they can't drift apart:
import joblib
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder, StandardScaler
from sklearn.ensemble import GradientBoostingClassifier
pre = ColumnTransformer([
("num", StandardScaler(), ["age", "income"]),
("cat", OneHotEncoder(handle_unknown="ignore"), ["city", "plan"]),
])
model = Pipeline([("pre", pre), ("clf", GradientBoostingClassifier())])
model.fit(X_train, y_train)
joblib.dump(model, "churn-model-2025-06-01.joblib")
Version the artifact with a date or hash, and record which data and code produced it.
2. Put it behind an API with validation
from fastapi import FastAPI
from pydantic import BaseModel, Field
import joblib
import pandas as pd
app = FastAPI()
model = joblib.load("churn-model-2025-06-01.joblib")
class Customer(BaseModel):
age: int = Field(ge=18, le=120)
income: float = Field(ge=0)
city: str
plan: str
@app.post("/predict")
def predict(c: Customer):
proba = model.predict_proba(pd.DataFrame([c.model_dump()]))[0, 1]
return {"churn_probability": round(float(proba), 4), "model_version": "2025-06-01"}
Input validation catches the ages of -3 and 4,000 before the model tries to make sense of them. Returning the model version makes debugging a hundred times easier.
3. Package and deploy
Containerise it, pin library versions, add a health check endpoint, and deploy it like any other service. A model is just software with a large binary file attached.
4. Monitor, because the world changes

Customer behaviour changes, prices change, a competitor launches, a pandemic happens. The data the model sees drifts away from the data it learned from. Monitor:
- input distributions (are ages, cities and plans still similar to training?)
- prediction distributions (did the model suddenly start predicting churn for everyone?)
- actual outcomes when they arrive, to measure real accuracy
- latency and errors, like any service
5. Plan for retraining and rollback
Decide in advance how you'll retrain (schedule or trigger), how you'll compare the new model with the old one, and how to roll back in one command if the new one misbehaves.
The mindset
A model in production is a living system, not a finished artwork. The notebook was the fun part. This is the part that makes it matter, and it's much less likely to make you cry if you plan it before the launch meeting rather than during it.