skip to content

In Databricks Feature Store, why can a model logged with FeatureEngineeringClient.log_model be scored with only primary keys, and what breaks that?

level: middleimportance: should knowfreq 8%

answer

  1. the model remembers its lookups
  2. FeatureLookup inside the training set
  3. score_batch and Model Serving
  4. load_df changes are not replayed
  5. timestamp_lookup_key for time series

basics

~20 s

fe.log_model stores the training set's FeatureLookup specifications with the model, so score_batch and Model Serving fetch the features by primary key themselves. Anything you did to load_df's DataFrame before training is not replayed at inference.

solid answer

~40 s

`create_training_set` takes a label DataFrame and a list of `FeatureLookup`s, each giving `table_name`, `feature_names` and `lookup_key` (plus `timestamp_lookup_key` for a time series table). `fe.log_model(..., training_set=training_set)` then packages those lookups with the model. At inference, `fe.score_batch(model_uri=..., df=batch_df)` needs only the key columns: it joins the features from the offline Delta tables and adds a `prediction` column. A Model Serving endpoint does the same against the online store the tables were published to. It breaks when you transform `load_df()` output before fitting, because those changes are not applied at scoring. It also breaks when the training frame kept extra non-feature-store columns that the scoring frame lacks. The price is coupling: the model now depends on Unity Catalog tables and Databricks' scoring paths.

code

python · 24 lines
python
import mlflow
from databricks.feature_engineering import FeatureEngineeringClient, FeatureLookup

fe = FeatureEngineeringClient()
lookups = [
    FeatureLookup(
        table_name="edu.learning.learner_activity",   # timeseries_columns="activity_ts"
        feature_names=["lessons_completed_7d", "days_since_login"],
        lookup_key="learner_id",
        timestamp_lookup_key="observed_at",
    ),
]
# labels_df columns: learner_id, observed_at, dropped_out
training_set = fe.create_training_set(
    df=labels_df, feature_lookups=lookups, label="dropped_out",
    exclude_columns=["learner_id", "observed_at"],
)
with mlflow.start_run():
    model = train(training_set.load_df().toPandas())   # your training function
    fe.log_model(model=model, artifact_path="model", flavor=mlflow.sklearn,
                 training_set=training_set, registered_model_name="edu.learning.dropout")

# batch_df columns: learner_id, observed_at -> features are looked up, prediction added
preds = fe.score_batch(model_uri="models:/edu.learning.dropout/1", df=batch_df)

go deeper

for a junior

Recall the chain in order: FeatureLookup, then create_training_set, then fe.log_model, then score_batch called with only the key columns.

for a middle

Explain what the logged model stores (its lookups) and where each scoring path reads from: offline Delta tables for score_batch, the published online store for Model Serving.

for a senior

Spot the silent failures: transforms on load_df output, missing extra columns, accidental overrides, and time-series models sent through plain pyfunc batch scoring.

for a principal

Treat the packaging as deliberate coupling to Unity Catalog and Databricks serving, and decide per model whether that convenience outweighs portability.

## What gets packaged with the model In **Databricks Feature Store** (Feature Engineering in Unity Catalog), a model can carry the instructions for fetching its own features. That depends on two calls on `FeatureEngineeringClient`: - `create_training_set(df=..., feature_lookups=[...], label=..., exclude_columns=[...])` joins features from feature tables onto your label DataFrame and returns a `TrainingSet`; - `log_model(model=..., artifact_path=..., flavor=..., training_set=..., registered_model_name=...)` logs the model **together with the feature specifications** from that training set. Each `FeatureLookup` names a `table_name`, the `feature_names` to take and the `lookup_key` columns in your DataFrame that match the table's primary keys. On a time series feature table — one created with `timeseries_columns` — you add `timestamp_lookup_key`. Each training row then gets the latest feature values *before* its timestamp, or null if none exist. ## Scoring with only keys Because the model knows its lookups, the caller supplies only the keys: | Path | Call | Where features come from | |---|---|---| | Batch | `fe.score_batch(model_uri=..., df=batch_df)` | Offline feature tables (Delta in Unity Catalog) | | Real time | A Model Serving endpoint serving the logged model | The online store the tables were published to | `score_batch` returns your DataFrame augmented with the looked-up feature columns and a `prediction` column. For a real-time endpoint, the feature tables must first be published to an online store. Databricks recommends its Lakebase-backed Online Feature Store, and that publishing may happen even after training. The endpoint uses Unity Catalog lineage to find the right online tables. ## What breaks the contract The packaging replays the **lookups**, not your notebook. These are the common ways it goes wrong: 1. **Transforming `load_df()` output before fitting.** If you scale, encode or filter the DataFrame returned by `TrainingSet.load_df()` and then train, those changes are *not* applied at inference. The features arrive raw, and the docs warn this lowers model performance. Put such logic inside the model or into the feature tables. 2. **Columns from outside the feature store.** A column kept in the training DataFrame (not excluded and not looked up) must also be present in the scoring DataFrame. Otherwise the model's inputs no longer match. 3. **Overriding by accident.** If `batch_df` already contains a column with a feature's name, `score_batch` uses that value instead of looking it up. That is useful for deliberate overrides and surprising otherwise. 4. **Using the wrong timestamp column.** Timestamp columns should not be used as a `lookup_key`. A DATE or TIMESTAMP primary key that is not declared as a time series column makes `create_training_set` return an error. 5. **Scoring time-series models through plain MLflow.** `mlflow.pyfunc` batch inference needs MLflow 2.11 or above and does not support models that use time series feature tables; use `score_batch` for those. A model can use at most 50 tables and 100 functions for training. ## The real-time path in practice On a Model Serving endpoint the same contract holds, with three details worth knowing: - every feature the logged model needs is looked up from the online store, unless the request payload carries a value for it. Such an override must match the feature's data type; - the online store can be a Databricks Online Feature Store or a supported third-party store, such as Amazon DynamoDB, published with read-only credentials; - for endpoints created since February 2025, you can log the augmented DataFrame — the looked-up values and function results — to the endpoint's inference table. That lets you check what the model actually received. ## The cost of the convenience The packaged model is convenient precisely because it is coupled: - its artifact refers to Unity Catalog feature tables by name; - scoring goes through `FeatureEngineeringClient.score_batch` or a Databricks Model Serving endpoint that performs the lookup; - the older Workspace Feature Store used `FeatureStoreClient.log_model` instead, and that store is deprecated. Models logged there belong to the legacy path. Moving such a model to another platform means re-logging it without feature metadata and giving the new serving path its own lookup step. With Feast, for example, the caller fetches features before calling the model. That coupling is the price of callers needing to send only `learner_id`, and it should be priced in when you weigh a managed store against Feast. ## Choosing when to use it Use the packaging when the model will be scored on Databricks and the team wants the feature list kept in one place. Skip it, and log a plain model, when the model must be portable or will be served where Databricks cannot perform the lookup.

  • How do you supply a feature value yourself at scoring time instead of having it looked up?
    Include a column with that feature's name in the DataFrame passed to `score_batch`. The model then uses your value and looks up only the remaining features. For a Model Serving endpoint, include the value in the request payload, and it must match the feature's data type.
  • What must happen before a Model Serving endpoint can look these features up in real time?
    The feature tables must be published to an online store, preferably a Databricks Online Feature Store created with `create_online_store` and filled with `publish_table`. Publishing can happen after training. The endpoint finds the right online tables through Unity Catalog lineage, so the caller still sends only the keys.

It is like a recipe card that lists which shelf each ingredient sits on: anyone can cook from it given only the order number. But if the cook chopped the vegetables differently when writing the card, that step is not on the card and will not be repeated.

  • score_batch needs every feature column in the batch DataFrame.
  • Scaling applied to load_df output is saved with the model and replayed.
  • Timestamp columns belong in lookup_key for point-in-time joins.
  • A model logged with plain mlflow.sklearn.log_model looks up features automatically.
  • Packaging lookups with the model keeps it portable to any serving platform.