With Feast's `@on_demand_feature_view`, how do you compute a minutes-since-last-purchase feature known only at request time, and what must each retrieval call supply?
answer
- a transform, not a stored table
- RequestSource declares request fields
- entity_df or entity_rows carry them
- pandas by default, python optional
- labelled Alpha or Beta in the docs
basics
~20 sDeclare a RequestSource for the request-time field, then an @on_demand_feature_view over it and a stored feature view; Feast runs the function in both retrieval calls. The field must appear in entity_df historically and in entity_rows online.
solid answer
~40 sAn on-demand feature view is a Python transform Feast runs at retrieval time. You declare a `RequestSource` whose `schema` lists the fields only the request knows, such as `request_ts`, and decorate a function with `@on_demand_feature_view(sources=[player_purchases, request_source], schema=[Field(...)])`. In the default `mode="pandas"` it receives a dataframe and returns one; `mode="python"` works on native dicts. Feast runs it in both paths, so `get_historical_features` needs `request_ts` as an `entity_df` column and `get_online_features` needs it in each entity row, or it raises a request-data-not-found error. `write_to_online_store` defaults to `False`, meaning compute on read. Feast 0.66's docs mark on-demand feature views [Alpha] on the concepts page and [Beta] on their reference page, so treat the surface as not yet stable.
code
python · 21 linesimport pandas as pd
from feast import Field, RequestSource
from feast.on_demand_feature_view import on_demand_feature_view
from feast.types import Float64, UnixTimestamp
from features import player_purchases # stores last_purchase_ts
request_time = RequestSource(
name="offer_request",
schema=[Field(name="request_ts", dtype=UnixTimestamp)],
)
@on_demand_feature_view(
sources=[player_purchases, request_time],
schema=[Field(name="minutes_since_last_purchase", dtype=Float64)],
)
def purchase_recency(inputs: pd.DataFrame) -> pd.DataFrame:
out = pd.DataFrame()
delta = inputs["request_ts"] - inputs["last_purchase_ts"]
out["minutes_since_last_purchase"] = delta.dt.total_seconds() / 60
return outgo deeper
Recall that an on-demand feature view is a function Feast runs at retrieval time, fed by stored features and a RequestSource.
Explain the decorator's sources, schema and mode, and that request fields must reach both entity_df and entity_rows.
Judge compute-on-read against write-time storage, keep the surface's Alpha/Beta status in view, and test transforms on every Feast upgrade.
Decide which request-time transforms belong in the feature store and which in the calling service, given latency and one shared definition.
## What an on-demand feature view is Most Feast features are **stored**: a batch or push source fills them, and retrieval reads them. Some features cannot be stored because one input exists only when a request arrives. "Minutes since last purchase" is one: the last purchase time can be stored per player, but the minutes depend on when the game asks. An **on-demand feature view** (ODFV) covers this case. It is a function, registered by `feast apply` like any other object, that Feast runs **during retrieval** over two kinds of input: - **stored features** from ordinary feature views, such as a `player_purchases` view holding `last_purchase_ts`; - **request data** declared with a **`RequestSource`**, whose `schema` lists fields the caller passes in, such as `request_ts`. Its own `schema` lists the output features, and those output names are what a `FeatureService` or a feature reference points at. ## Declaring one The decorator is `on_demand_feature_view` from `feast.on_demand_feature_view`. The arguments that matter here: | Argument | Default | Meaning | |---|---|---| | `sources` | none | feature views and request sources the function reads | | `schema` | required | output fields with their Feast types | | `mode` | `"pandas"` | `"pandas"` passes a DataFrame; `"python"` passes native values | | `singleton` | `False` | with `mode="python"`, call the function once per row as a dict | | `write_to_online_store` | `False` | compute on read (`False`) or at write time and store (`True`) | In pandas mode the function receives one dataframe holding the stored features and the request fields, and returns a dataframe holding the output columns its `schema` declares. Feast 0.66.0 also stopped passing undeclared feature-view columns to the function (changelog #6527), so read only what `sources` declares. ## What each retrieval call must supply Because the function runs in both retrieval paths, both calls must carry the request fields: 1. **Historical**: every request field must be a column of `entity_df`. For training, `request_ts` would simply equal the row's `event_timestamp`, the moment the offer was shown. A missing column raises `RequestDataNotFoundInEntityDfException`. 2. **Online**: every request field must appear in each entity row, for example a row holding `player_id` plus `request_ts` set to the moment the game asks. A missing field raises `RequestDataNotFoundInEntityRowsException`. Through the feature server, the `/get-online-features` body has no separate field for request data: `request_ts` goes into the `entities` map beside `player_id`, one value per requested row. Running one function in both paths means the training value and the served value come from the same code. Why that matters is the subject of the material on training-serving skew; the mechanism here is that Feast executes the registered function itself. ## Compute on read or on write With `write_to_online_store=False` the function runs on every online read, which costs latency on each call. Feast's concepts page also notes the transformations run locally, which suits online serving but does not scale well to large offline retrievals. Setting `write_to_online_store=True` applies the function when data is written and stores the result, which helps when inputs are stored features only. It cannot help this feature, whose input exists only at request time, so it stays compute-on-read. A further interaction: a `FeatureService` with `precompute_online=True` rejects ODFVs that keep `write_to_online_store=False`, because their values are never materialized. ## Status in Feast 0.66 Feast labels this surface as not yet stable, inconsistently: the concepts page heads it "[Alpha] On demand feature views", the reference page is titled "[Beta] On Demand Feature Views" and warns it is experimental, and the class docstring says "[Experimental]". In an interview, say that ODFVs work and appear in Feast's own project templates, but that an Alpha or Beta surface carries no stability promise; 0.66.0 itself changed which columns reach the function. Pin the Feast version and re-test transforms on every upgrade. ## Pitfalls - Forgetting the request column in `entity_df`, which fails the whole training query. - Passing timestamps of different types online and offline, so the arithmetic differs between paths. - Heavy work in compute-on-read functions, which lands on every request. - Assuming an ODFV is stored and can be materialized like a batch view when `write_to_online_store` is `False`. - Naming the output feature the same as an input column, which makes it unclear in the training set which value the model actually saw.
- Why not set `write_to_online_store=True` on this view to save request latency?Storing the result means computing it when data is written, but `request_ts` exists only when the game asks. A value stored at write time would be minutes since purchase as of the write, not the request, so this feature has to be computed on read.
- When is `mode="python"` with `singleton=True` a better fit than pandas mode?For online calls that score one player at a time, native Python on a single dict avoids building a dataframe per request and keeps the code readable. Pandas mode suits batches, such as large historical retrievals.
- on-demand feature views are stored like ordinary feature views
- request fields are needed only for online retrieval
- on-demand feature views are a stable, general-availability API
- write_to_online_store=True works for features needing request-time input
- mode defaults to python
Assumes Feast 0.66; its docs label on-demand feature views Alpha or Beta, and 0.66.0 stopped passing undeclared columns to the function.