Sub topic 15
Request-time transformations with on-demand feature views
This chapter assumes Python, pandas DataFrames, Feast entities and feature views, and the difference between offline training data and online inference. It also assumes that driver_stats_fv from the earlier chapter is already defined.
An on-demand feature view calculates a feature when a request arrives. It combines stored features with values supplied by the request. The result is useful when a value depends on both reusable data and request-specific data.
For example, a model might use a driver's stored conversion rate and the fare of the current trip:
adjusted_score = driver_conversion_rate * current_trip_fareThe conversion rate belongs in the feature store. The current fare exists only for this request, so storing every possible calculation would be wasteful.
Stored features and request features
A normal feature view reads from a data source such as a table or Parquet file. An on-demand feature view also has a RequestSource. A request source describes fields that the caller must provide at retrieval time.
| Input | Example | Stored by Feast? |
|---|---|---|
| Stored feature | `conv_rate` for driver 1001 | Yes |
| Request field | `trip_fare` for this trip | No |
| Derived feature | `fare_weighted_conv_rate` | Calculated on request |
The transformation function receives a pandas DataFrame. Its columns contain the stored feature values and request fields. The function returns the derived feature columns.
Define an on-demand feature view
The following definition uses the driver_stats_fv feature view from the previous chapter. The request source accepts the fare for the current trip.
import pandas as pd
from feast import Field, RequestSource, on_demand_feature_view
from feast.types import Float32
trip_request = RequestSource(
name="trip_request",
schema=[
Field(name="trip_fare", dtype=Float32),
],
)
@on_demand_feature_view(
sources=[driver_stats_fv, trip_request],
schema=[
Field(name="fare_weighted_conv_rate", dtype=Float32),
],
)
def fare_weighted_driver_features(inputs: pd.DataFrame) -> pd.DataFrame:
inputs["fare_weighted_conv_rate"] = (
inputs["conv_rate"] * inputs["trip_fare"]
)
return inputs[["fare_weighted_conv_rate"]]The sources list identifies the inputs. driver_stats_fv supplies conv_rate, while trip_request supplies trip_fare. The schema declares the output type. Feast uses the function name as the default name of the on-demand feature view.
An on-demand feature view does not materialize its derived value into the online store. Feast retrieves the stored inputs, passes them with the request fields to the transformation, and returns the result.
Training and serving behavior
The same transformation can be used during historical retrieval and online retrieval. During training, the entity DataFrame must contain the request fields, such as trip_fare. Feast joins stored features according to each entity row's timestamp, then runs the transformation.
During serving, the online request supplies the entity key and request fields. Feast reads the latest materialized stored features and applies the transformation immediately.
This shared definition helps reduce training-serving skew. However, it does not remove every source of inconsistency. The transformation must be deterministic, use the same units in both paths, and avoid dependencies on local time or external services.
When to use one
Use an on-demand feature view when:
- the calculation needs request-specific values;
- the calculation is cheap enough for request-time execution;
- the inputs already exist as Feast features or request fields;
- storing every possible derived value would create unnecessary data.
Use a regular feature view instead when the result can be computed ahead of time and reused by many requests. Use a materialized feature when low and predictable latency matters more than calculating the value at request time.
Exercise
A model needs the driver's seven-day average trips and the distance of the current trip. Which value should be a request field, and where should the multiplication happen?
Show answer
The current trip distance should be a request field because it is known only for the current request. The multiplication can happen in an on-demand feature view that combines it with the stored seven-day average trips.