import joblib
from skl2onnx import to_onnx
from skl2onnx.common.data_types import FloatTensorType
joblib.dump(model, 'artifacts/model.joblib')
initial_types = [('features', FloatTensorType([None, X_train.shape[1]]))]
onnx_model = to_onnx(model, initial_types=initial_types, target_opset=17)
with open('artifacts/model.onnx', 'wb') as file_handle:
file_handle.write(onnx_model.SerializeToString())
Model serialization is not just a file-format choice. It affects startup time, compatibility, portability, and security boundaries. I use joblib for common scikit-learn pipelines, reserve pickle for trusted internal workflows, and reach for ONNX when I need cross-runtime portability.