Custom PyFunc models with real-time feature engineering
Databricks Machine Learning Professional · Lesson 9 of 22 · 1:05:23
YouTube loads only after you press play. This chapter opens at its timestamp in the full course. Watch on YouTube ↗
Learning with these videos? Subscribe for new courses and exam-style questions.
Subscribe on YouTube (opens in a new tab)In this lesson
Prepare for the Databricks Certified Machine Learning Professional exam. In this lesson: the PythonModel class (load_context and predict), computing features from a raw request at inference time, artifacts, and logging and validating the model before serving. Includes three original practice questions.
Study notes
The text of this lesson, to read at your own pace. Information as of November 2026.
The raw payment
When a guest pays, Pebblecove's checkout sends the raw payment to the fraud model: an amount, a currency, two countries, and a check-in date. The model was trained on derived features, and if another team computes them with its own code, any small difference silently degrades the predictions. This lesson covers custom PyFunc models, which carry their feature engineering inside the model.
The PythonModel class
A custom PyFunc model is a Python class that extends mlflow dot pyfunc dot PythonModel. Its load_context method runs once, when the model is loaded, to open what the model needs, like a trained classifier or a lookup file, through context dot artifacts. Its predict method runs on every call, with the context and the model input, usually a pandas DataFrame, and it can preprocess, predict, and post process. You need it when the model needs preprocessing, uses a framework that MLflow does not support directly, returns outputs that need reshaping, or follows custom logic for each request.
Features at request time
Real time feature engineering means that predict receives the raw request, and computes the features itself, with the same code that built the training features. For a payment, it converts the amount to euros, flags a card country that differs from the home's country, counts the days before check-in, and takes the hour of the payment. Because one function serves both training and serving, the two cannot drift apart, which avoids training serving skew. The client sends only raw fields, so a new feature only needs a new model version, not a change in every caller. And features that need history, like the guest's past bookings, come from the feature store instead, as lesson twelve shows.
Log and validate
You log the model with mlflow dot pyfunc dot log_model, passing python_model, an instance of your class, and artifacts, a dictionary of names and paths, which MLflow copies into the model. Model Serving requires files from Unity Catalog volumes to be packaged this way, so that every dependency is captured when the endpoint is deployed. Add an input example, so MLflow infers the signature that Unity Catalog requires, and list pip_requirements with the full mlflow package and its version, since the machine learning runtime only includes a slim MLflow package. Before deployment, mlflow dot models dot predict runs the logged model in a fresh environment, with its own dependencies, to catch a missing package. And registered_model_name registers the result in Unity Catalog, ready for a serving endpoint.
Recap
Let's recap. A custom PyFunc model is a class that extends PythonModel. load_context runs once, to open the artifacts, and predict runs on every call. With real time feature engineering, predict turns the raw request into features with the training code, which avoids training serving skew. log_model packages python_model and its artifacts, with an input example, and pip_requirements that name the full mlflow package. And mlflow dot models dot predict validates the model before deployment.
Quick checks for this lesson
The 3 questions of this lesson, as plain text. The button above runs them with shuffled choices and the reason behind every choice.
A fraud PyFunc opens a large classifier file inside predict, and every call takes seconds, even for one payment. Where should the loading move?
- Into predict, once per batch of rows
- Into the client application
- Into the pip requirements
- Into load_context, through context.artifacts
Show the answer
Into load_context, through context.artifacts
Load_context runs once when the model is loaded, so the classifier is opened once, instead of on every call. “Into predict, once per batch of rows” would still load it on every call. “Into the client application” would move part of the model out of the model, which defeats the purpose. And “Into the pip requirements” only lists packages to install, not files to open.
Training computed the days before check-in from the payment date, but the checkout team uses today's date. What does a PyFunc with the feature code inside prevent?
- Training-serving skew
- Data drift in the guest population
- Overfitting of the classifier
- Latency spikes at peak hours
Show the answer
Training-serving skew
With the feature code inside the model, training and serving use the same function, so the two computations cannot differ. “Data drift in the guest population” is wrong, since the guests can still change over time, and that needs monitoring. “Overfitting of the classifier” is a training problem, unrelated to where the features are computed. And “Latency spikes at peak hours” is wrong, since the extra code does nothing against slow responses.
The fraud PyFunc was logged on an ML runtime without pip_requirements, and the serving endpoint fails to build its container. What is the likely cause?
- The model has no signature
- The model version has no alias
- The environment lists mlflow-skinny, not mlflow
- The input example has too many rows
Show the answer
The environment lists mlflow-skinny, not mlflow
The machine learning runtime ships a slim MLflow package, which was captured in the environment, and Model Serving needs the full mlflow package to build the container. “The model has no signature” is wrong, since Unity Catalog would have refused to register a model without a signature. “The model version has no alias” is wrong, since an endpoint can serve a version that has no alias. And “The input example has too many rows” would not affect how the container is built.