Sub topic 13
Batch Scoring with Automatic Feature Retrieval
This chapter assumes you can train a model with scikit-learn and that you have logged a model with fe.log_model, as covered in the previous chapter. It also assumes you know Unity Catalog's catalog.schema.table naming.
The working model
A model logged with fe.log_model stores its feature lookups as metadata. At scoring time you do not rebuild the feature join by hand. You pass a DataFrame that holds only the lookup keys, and score_batch does the rest.
Running a batch score
from databricks.feature_engineering import FeatureEngineeringClient
fe = FeatureEngineeringClient()
# The input needs only the lookup key. Features are retrieved from the feature table.
batch_df = spark.table("main.ml.users_to_score").select("user_id")
predictions = fe.score_batch(
model_uri="models:/main.ml.churn_model/1",
df=batch_df,
)The model_uri points to a registered model version in Unity Catalog. The returned DataFrame contains the input columns, the looked-up feature columns, and a prediction column.
Use a model alias instead of a hard-coded version when you want the job to follow a promoted version. For example, models:/main.ml.churn_model@champion resolves to whichever version holds that alias.
What happens to the feature values
score_batch reads the feature table as it exists at scoring time. You get the current values, so the result depends on how fresh the table is. If the feature pipeline has not run, you score with stale features. The next chapters cover scheduling for freshness.
If the feature table was created with a time series column, score_batch uses the lookup definition saved with the model. The timestamp lookup key must then be present in your input DataFrame.
Overriding a feature
Columns you include in the input take precedence over the same-named features in the feature table. This lets you test "what if" values for specific rows. It also causes silent errors if a column name in your input collides with a feature name by accident.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Lookup key column not found | The input DataFrame lacks the key, or it is named differently | Rename the column to match the `lookup_key` used in training |
| Null predictions or null features | Keys in the input do not exist in the feature table | Check key coverage and decide how to handle missing rows |
| Permission denied | The scoring identity cannot read the feature table or model | Grant `SELECT` on the table and `EXECUTE` on the model |
| Stale results | The feature pipeline did not refresh the table | Check the pipeline schedule before scoring |
| Registry error | The registry URI is not set to Unity Catalog | Call `mlflow.set_registry_uri("databricks-uc")` |
Saving the output
Write predictions to a Delta table so downstream users can query them:
(predictions
.write
.mode("overwrite")
.saveAsTable("main.ml.churn_predictions"))Include a scoring date column if you want a history rather than a single latest snapshot.
Exercise
A batch scoring job passes a DataFrame with user_id and avg_order_amount_90d columns to score_batch. The model was trained using avg_order_amount_90d from the feature table. What happens to that feature, and why is it a risk?
Show answer
The value in the input DataFrame is used instead of the value in the feature table, because input columns take precedence over features with the same name. This is a risk because an accidental name collision silently replaces the governed feature value, which can reintroduce training-serving skew.
Exercise
Why should you check key coverage before writing predictions to a table?
Show answer
Keys that do not exist in the feature table produce null features instead of an error, so predictions for those rows may be wrong or missing.