When should you use mlflow.pyfunc.PythonModel to wrap a custom model class?
Select an answer to reveal the explanation.
Short Explanation and Infographic
PythonModel is your escape hatch — when your inference logic involves custom pre-processing, post-processing, business rules, or chaining multiple models, you wrap it all in a class and MLflow treats the whole thing as one deployable unit.
Full explanation below image
Full Explanation
mlflow.pyfunc.PythonModel is used when: 1) Custom pre/post-processing — feature engineering at inference time that can't be expressed in the model's native framework. 2) Ensemble/chaining — combining a scikit-learn model with a rule-based system or multiple models. 3) Unsupported frameworks — custom C++ models, proprietary libraries, or any framework without native MLflow support. Implementation: class MyModel(mlflow.pyfunc.PythonModel): def load_context(self, context): (loads artifacts from context.artifacts dict) def predict(self, context, model_input): (takes pandas DataFrame, returns numpy array or DataFrame). Logged with mlflow.pyfunc.log_model(python_model=MyModel(), artifacts={'model': model_path}). The logged model behaves identically to any other MLflow model — it can be served, registered, and loaded with mlflow.pyfunc.load_model().