By WebNest Studio Engineering Team
Python Tutorial
Saving, Serving and Next Steps in Machine Learning
A model is only useful when it can make predictions for real users. This final lesson shows how to save a trained pipeline with joblib, load it in another program, and serve predictions from a FastAPI endpoint — combining this module with the web module. It ends with the practices that keep models trustworthy in production and a roadmap into deep learning and working with large language models.
Why it matters
A model in a notebook helps nobody. To be useful it has to be saved, loaded by an application and asked for predictions, usually through an API. This last step is where many projects stall, so being able to take a model from training to a working service makes you far more useful to a team.
Saving and Loading Models
joblib.dump(pipeline, "model.joblib") saves the whole fitted pipeline — preprocessing and model — and joblib.load() restores it. Save the pipeline, not only the model, so production applies exactly the same preprocessing. Record the scikit-learn version and training date alongside it: loading a model with a different library version is not guaranteed to work. Only load model files you trust — joblib/pickle files can execute code when loaded. For cross-language serving, formats such as ONNX (skl2onnx) are an alternative.
ML in Production
- Load the model once at start-up (a FastAPI lifespan handler), not on every request.
- Validate inputs with a Pydantic model that mirrors the training features.
- Log predictions and, later, the true outcomes to measure real-world accuracy.
- Watch for data drift — when incoming data stops looking like the training data — and retrain regularly.
- Check models for bias across groups of users before and after deployment.
Where to Go Next
- Gradient boosting libraries — XGBoost, LightGBM, CatBoost: top performers on tabular data.
- Deep learning — PyTorch (most popular in research and industry) or TensorFlow/Keras for images, audio, text and time series.
- Large language models — calling LLM APIs from Python, embeddings for semantic search, retrieval-augmented generation (RAG) and agents.
- MLOps — experiment tracking (MLflow), model registries, automated retraining and monitoring.
- Practice on real datasets (Kaggle, UCI Machine Learning Repository) and build end-to-end projects.
Examples
Training, saving and loading a pipeline with joblib
# train_model.py
import json
import joblib
import sklearn
from datetime import date
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
iris = load_iris(as_frame=True)
pipeline = make_pipeline(StandardScaler(), LogisticRegression(max_iter=1000))
pipeline.fit(iris.data, iris.target)
joblib.dump(pipeline, "iris_model.joblib")
metadata = {"sklearn_version": sklearn.__version__, "trained_on": str(date(2026, 9, 28)),
"features": list(iris.data.columns), "classes": iris.target_names.tolist()}
json.dump(metadata, open("iris_model.json", "w"), indent=2)
print("saved model with features:", metadata["features"])
# later, in another program
loaded = joblib.load("iris_model.joblib")
sample = iris.data.iloc[[0, 75, 140]]
print("predictions:", [metadata["classes"][i] for i in loaded.predict(sample)])
print("same as original:", (loaded.predict(sample) == pipeline.predict(sample)).all())
saved model with features: ['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)']
predictions: ['setosa', 'versicolor', 'virginica']
same as original: True
Serving predictions with FastAPI
# app.py (run with: fastapi dev app.py)
from contextlib import asynccontextmanager
import joblib
import pandas as pd
from fastapi import FastAPI
from fastapi.testclient import TestClient
from pydantic import BaseModel, Field
CLASSES = ["setosa", "versicolor", "virginica"]
ml = {}
@asynccontextmanager
async def lifespan(app: FastAPI):
ml["model"] = joblib.load("iris_model.joblib") # load once at start-up
yield
ml.clear()
app = FastAPI(title="Iris classifier", lifespan=lifespan)
class Flower(BaseModel):
sepal_length: float = Field(gt=0, le=10)
sepal_width: float = Field(gt=0, le=10)
petal_length: float = Field(gt=0, le=10)
petal_width: float = Field(gt=0, le=10)
@app.post("/predict")
def predict(flower: Flower):
row = pd.DataFrame([[flower.sepal_length, flower.sepal_width, flower.petal_length, flower.petal_width]],
columns=["sepal length (cm)", "sepal width (cm)", "petal length (cm)", "petal width (cm)"])
probabilities = ml["model"].predict_proba(row)[0]
best = int(probabilities.argmax())
return {"species": CLASSES[best], "confidence": round(float(probabilities[best]), 3)}
with TestClient(app) as client:
print(client.post("/predict", json={"sepal_length": 5.1, "sepal_width": 3.5,
"petal_length": 1.4, "petal_width": 0.2}).json())
print(client.post("/predict", json={"sepal_length": 6.7, "sepal_width": 3.0,
"petal_length": 5.2, "petal_width": 2.3}).json())
print(client.post("/predict", json={"sepal_length": -1, "sepal_width": 3.0,
"petal_length": 5.2, "petal_width": 2.3}).status_code)
{'species': 'setosa', 'confidence': 0.985}
{'species': 'virginica', 'confidence': 0.961}
422
Common Mistakes
- Saving only the model and re-implementing preprocessing differently in production.
- Loading the model inside the request handler, making every prediction slow.
- Loading joblib/pickle files from untrusted sources.
- Deploying once and never checking accuracy or data drift again.
- Ignoring library versions, so a saved model fails to load after an upgrade.
Exercise
Train a linear regression model, save it to a file with joblib, load it back, and print whether the loaded model gives the same prediction as the original, followed by that prediction. A temporary folder is used so that nothing is left behind.
Expected output: True then 100.0
import tempfile
from pathlib import Path
import joblib
from sklearn.linear_model import LinearRegression
model = LinearRegression().fit([[1], [2], [3], [4]], [20, 40, 60, 80])
with tempfile.TemporaryDirectory() as folder:
path = Path(folder) / "model.joblib"
# TODO: save the model to path
# TODO: load it back into a new variable
# TODO: print whether both models predict the same value for [[5]]
# TODO: print the loaded model's prediction, rounded to 1 decimal
pass
Show hints
- The two functions are
joblib.dump(model, path)andjoblib.load(path). - Compare the first element of each prediction array, and round the value before printing it.
Show solution
import tempfile
from pathlib import Path
import joblib
from sklearn.linear_model import LinearRegression
model = LinearRegression().fit([[1], [2], [3], [4]], [20, 40, 60, 80])
with tempfile.TemporaryDirectory() as folder:
path = Path(folder) / "model.joblib"
joblib.dump(model, path)
loaded = joblib.load(path)
original_prediction = model.predict([[5]])[0]
loaded_prediction = loaded.predict([[5]])[0]
print(bool(original_prediction == loaded_prediction))
print(round(float(loaded_prediction), 1))
Quick quiz
1. Which library is commonly used to save a scikit-learn model to a file?
- A. json
- B. sqlite3
- C. csv
- D. joblib
Show answer
D. It stores models containing large NumPy arrays efficiently.
2. Why should the whole pipeline be saved, and not only the model?
- A. So that the same preprocessing is applied to new data at prediction time
- B. To make the file larger
- C. Because the model cannot be saved alone
- D. To hide the code
Show answer
A. A model given unscaled or unencoded input produces wrong predictions.
3. Why should a model file from an untrusted source never be loaded?
- A. It may be too large
- B. Loading uses pickle, which can execute arbitrary code
- C. It may be out of date
- D. It will overwrite your model
Show answer
B. Only load model files that you or your team created.
Interview questions
How do you deploy a machine-learning model?
Show answer
Train and save the full pipeline as a file. Build a small web service, for example with FastAPI, that loads the file once at start-up and exposes a prediction endpoint, validating the input with a schema. Package the service and its exact library versions in a container and run it like any other API. In production, log the inputs and predictions so that the model's behaviour can be monitored.
What can go wrong with a model after it has been deployed?
Show answer
The data it receives can drift away from the data it was trained on, as behaviour, prices or products change, so its accuracy declines without any error being raised. The library versions may differ from those used in training and make the saved file unloadable or subtly different. Inputs may arrive in an unexpected form. Monitoring the predictions, retraining on recent data and pinning versions address these.
Key Points to Remember
- joblib.dump/load save and restore complete fitted pipelines.
- Store metadata: library version, features, classes and training date.
- Serve models from FastAPI: load once in lifespan, validate inputs with Pydantic.
- Monitor predictions, drift and fairness; retrain as data changes.
- Next steps: gradient boosting, PyTorch deep learning, LLM applications and MLOps.
Practice in the playground
Write and run your own code for this topic, or start from the exercise or an example. Runs Python in your browser. Your code for each topic is kept in this browser.
Run the examples
Change an input, predict the result, then compare it with the output. Explain why the result changes.
These examples need a project environment on your computer, so they can be copied but not run here. Codelab runs Python, FastAPI backends with SQLite, plain Java 17 and HTML/CSS/JavaScript.
Written by
WebNest Studio Engineering Team
Practical lessons from developers working with Java, Python, React, databases and production systems.
Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering