🧠 AI with Python – 🔁 Model Versioning Strategy
Posted on: April 14, 2026
Description:
In real-world machine learning systems, deploying a model is not a one-time event. Models evolve over time — new data, better features, and improved algorithms lead to newer versions. But deploying a new model blindly is risky.
What if the new model performs worse in production? This is where model versioning and rollback strategies become essential.
Understanding the Problem
In a typical ML lifecycle:
- A model is trained and deployed
- A better version is developed later
- The new model replaces the old one
However, in production:
- new models may behave differently
- unseen data can cause performance drops
- business impact can be significant
We need a safe way to manage model updates.
What Is Model Versioning?
Model versioning is the practice of maintaining multiple versions of a model, such as:
- v1 → currently deployed model
- v2 → new candidate model
- rollback version → previous stable model
Each version is stored separately and can be loaded when needed.
1. Training Multiple Model Versions
We train two models with different configurations.
model_v1 = RandomForestClassifier(n_estimators=100)
model_v2 = RandomForestClassifier(n_estimators=200)
Each version represents a different stage of model improvement.
2. Saving Model Versions
We store each model using versioned filenames.
joblib.dump(model_v1, "model_v1.pkl")
joblib.dump(model_v2, "model_v2.pkl")
This allows us to track and reuse specific versions easily.
3. Comparing Model Performance
We evaluate both versions on the same dataset.
acc_v1 = accuracy_score(y_test, model_v1.predict(X_test))
acc_v2 = accuracy_score(y_test, model_v2.predict(X_test))
This helps decide which model should be deployed.
4. Selecting the Active Model
We choose the better-performing model.
active_model = model_v2 if acc_v2 >= acc_v1 else model_v1
This simulates a deployment decision.
5. Rollback Strategy
If the new model fails in production, we revert to a previous version.
rollback_model = joblib.load("model_v1.pkl")
Rollback ensures that we can quickly recover from bad deployments.
Why Versioning Matters in Production
Model versioning provides:
- safer deployments
- easy comparison of models
- quick rollback in case of failure
- reproducibility and traceability
- reduced risk in production systems
Without versioning, managing models becomes chaotic and error-prone.
Key Takeaways
- ML models should always be versioned in production.
- Multiple versions allow safe comparison and deployment.
- Rollback is critical when new models underperform.
- Versioning improves reliability and traceability.
- A foundational practice in production ML systems.
Conclusion
Model versioning is a crucial step in building robust machine learning systems. By maintaining versions like v1, v2, and having a rollback mechanism, we ensure that model updates are safe, controlled, and reversible.
This makes versioning a core concept in the Production ML track of the AI with Python series — moving from model building to real-world system reliability.
Code Snippet:
# 📦 Import Required Libraries
import joblib
import pandas as pd
from sklearn.datasets import load_wine
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
# 🧩 Load the Dataset
data = load_wine()
X = pd.DataFrame(data.data, columns=data.feature_names)
y = data.target
# ✂️ Split Data into Train and Test Sets
X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size=0.3,
random_state=42,
stratify=y
)
# 🤖 Train Model Version 1 (v1)
model_v1 = RandomForestClassifier(
n_estimators=100,
max_depth=4,
random_state=42
)
model_v1.fit(X_train, y_train)
# 💾 Save Version 1
joblib.dump(model_v1, "model_v1.pkl")
# 🚀 Train Model Version 2 (v2)
model_v2 = RandomForestClassifier(
n_estimators=200,
max_depth=6,
random_state=42
)
model_v2.fit(X_train, y_train)
# 💾 Save Version 2
joblib.dump(model_v2, "model_v2.pkl")
# 📊 Evaluate Both Versions
pred_v1 = model_v1.predict(X_test)
pred_v2 = model_v2.predict(X_test)
acc_v1 = accuracy_score(y_test, pred_v1)
acc_v2 = accuracy_score(y_test, pred_v2)
print("v1 Accuracy:", acc_v1)
print("v2 Accuracy:", acc_v2)
# ✅ Select Active Model
if acc_v2 >= acc_v1:
active_model = model_v2
active_version = "v2"
else:
active_model = model_v1
active_version = "v1"
print("Active model version:", active_version)
# 🔁 Simulate Rollback Strategy
rollback_model = joblib.load("model_v1.pkl")
rollback_preds = rollback_model.predict(X_test)
print("Rollback model accuracy:", accuracy_score(y_test, rollback_preds))
No comments yet. Be the first to comment!