Sub topic 12
Logging a Model with Its Feature Lookups
This chapter assumes you can build a training set with FeatureEngineeringClient.create_training_set, that you know Unity Catalog's catalog.schema.table naming, and that you have trained a scikit-learn model before.
Why package features with the model
A model trained on joined features needs the same features at inference time. If each consumer rebuilds the join by hand, the logic drifts and you get training-serving skew. Logging the model with its training set stores the lookup definitions inside the model artifact. At inference, Databricks reads those definitions and retrieves the features itself.
Logging the model
Train on the DataFrame from training_set.load_df(). Then log the model with fe.log_model instead of mlflow.sklearn.log_model. The training_set argument is what attaches the feature metadata.
import mlflow
from databricks.feature_engineering import FeatureEngineeringClient
from sklearn.ensemble import RandomForestClassifier
fe = FeatureEngineeringClient()
# training_set comes from fe.create_training_set(...) in the previous chapter
training_df = training_set.load_df()
train_pdf = training_df.toPandas()
X = train_pdf.drop(columns=["label"])
y = train_pdf["label"]
model = RandomForestClassifier(n_estimators=100, random_state=42).fit(X, y)
mlflow.set_registry_uri("databricks-uc")
fe.log_model(
model=model,
artifact_path="model",
flavor=mlflow.sklearn,
training_set=training_set,
registered_model_name="main.ml.churn_model",
)The call does three things:
- It logs the model with the MLflow flavor you pass, here
mlflow.sklearn. - It saves the feature lookups, including table names, feature names, and lookup keys, as metadata.
- It registers the model in Unity Catalog under the three-level name.
What the model expects as input
The model's input signature contains the lookup keys, not the looked-up features. In this example the caller supplies only user_id. Columns you passed in exclude_columns during create_training_set are not features either. Any column that was in the labeled DataFrame and kept in the training set must still be supplied by the caller.
Common errors
| Symptom | Likely cause |
|---|---|
| Model registers but inference asks for all features | Logged with `mlflow.sklearn.log_model`, so no lookup metadata was stored |
| Permission error on registration | Missing `CREATE MODEL` on the schema, or the registry URI is not `databricks-uc` |
| Lookup fails at inference | The caller lacks `SELECT` on the feature table, or the table was dropped or renamed |
| Missing feature values in predictions | Keys absent from the feature table return nulls, which the model may not handle |
Check the logged lineage
Open the model in Catalog Explorer to see the feature tables it depends on. This is the same lineage view covered later in the chapter on discovery. It lets you see which models would break before you change a feature table.
Exercise
You log a model with mlflow.sklearn.logmodel after training on the output of createtrainingset. A colleague scores new users by passing only userid and gets an error about missing columns. What went wrong, and how do you fix it?
Show answer
mlflow.sklearn.logmodel does not store the feature lookups, so the model expects every feature as an input column. Re-log the model with fe.logmodel, passing trainingset=trainingset and flavor=mlflow.sklearn. The new version stores the lookups, so scoring needs only user_id.