Sub topic 12
Serving features with get_online_features
This chapter covers how a model service reads the latest feature values at inference time. It assumes you have a feature repository with an entity, a feature view and a feature service, and that you have run feast apply and materialized data into the online store. If you have not, finish the materialization chapter first. You should also be comfortable with Python dictionaries and with the idea of a request/response service.
What the call does
FeatureStore.get_online_features reads the most recently materialized values for a set of entity keys from the online store. It does not touch the offline store and it does not compute a point-in-time join. It does a key lookup, which is why it fits the tens-of-milliseconds budget of an inference request.
from feast import FeatureStore
store = FeatureStore(repo_path=".")
online_features = store.get_online_features(
features=[
"driver_hourly_stats:conv_rate",
"driver_hourly_stats:acc_rate",
],
entity_rows=[{"driver_id": 1001}],
).to_dict()
print(online_features)The result looks like this, with one list per column and one element per entity row:
{
"driver_id": [1001],
"conv_rate": [0.52],
"acc_rate": [0.81],
}The numbers depend on your data. The structure does not.
The two arguments
features
features names what to read. You can pass it in two forms:
- A list of strings in the form
"<feature_view_name>:<feature_name>", such as"driver_hourly_stats:conv_rate". - A
FeatureServiceobject, which expands to the features that service declares.
Using a feature service is the better habit for production code. The model's feature list then lives in the repository, under version control, instead of being copied into the serving code.
from feast import FeatureStore
store = FeatureStore(repo_path=".")
service = store.get_feature_service("driver_activity_v1")
response = store.get_online_features(
features=service,
entity_rows=[{"driver_id": 1001}, {"driver_id": 1002}],
)
print(response.to_dict())Here driver_activity_v1 returns only conv_rate and acc_rate, because the service selected those two columns. avg_daily_trips is not returned even though the feature view defines it.
entity_rows
entity_rows is a list of dictionaries. Each dictionary holds the join key values for one prediction. The key name must match the entity's join key, here driver_id. Unlike get_historical_features, there is no event_timestamp. The online store holds one row per entity key per feature view, and Feast returns that row.
You can batch several keys into one call. The output preserves the order of your input rows. Batching reduces round trips to the online store, so use it when one request scores several candidates, for example ranking ten drivers.
How a request flows
The registry lookup is why the feature repository path or registry location must be reachable from the serving process. Feast caches registry data, so this step is not a network call on every request in normal configurations.
Reading the response
to_dict() returns a dictionary of lists. to_df() returns a pandas DataFrame. For a model, you typically build a DataFrame or array in a fixed column order:
import pandas as pd
from feast import FeatureStore
store = FeatureStore(repo_path=".")
feature_names = ["conv_rate", "acc_rate"]
response = store.get_online_features(
features=[f"driver_hourly_stats:{name}" for name in feature_names],
entity_rows=[{"driver_id": 1001}, {"driver_id": 1002}],
)
frame = pd.DataFrame(response.to_dict())
model_input = frame[feature_names]
print(model_input)Selecting columns by name, as in frame[feature_names], protects you from relying on dictionary ordering. Pass full_feature_names=True if two feature views use the same feature name. The columns are then prefixed with the view name, for example driver_hourly_stats__conv_rate.
Missing values and expired values
An online lookup can return None for a feature. The common causes are these:
| Cause | What you see | Typical fix |
|---|---|---|
| The entity key was never materialized | `None` for every feature of that view | Check the key type and value, and confirm the source contains it |
| Materialization has not run since the latest data landed | Old values, not `None` | Run or schedule `feast materialize-incremental` |
| The stored value is older than the view's TTL | `None` | Materialize more often, or revisit the TTL |
| The value was null in the source | `None` | Handle nulls in the model input |
| The feature was not materialized to the online store | `None` | Check that `online=True` is set on the feature view |
For driver_hourly_stats, the TTL is one day. If the newest materialized row for a driver is more than a day older than the current time, the online read treats it as stale and returns None instead of an outdated number. Silently returning stale data is usually worse than returning nothing, because the model cannot tell the difference.
Your serving code needs an explicit policy for None. Options include a default value that matches how the model was trained, a fallback model, or rejecting the request. Whatever you choose, apply the same imputation during training. Otherwise you create training-serving skew yourself.
Common errors
- Wrong key name. Passing
{"driver": 1001}when the join key isdriver_idraises an error or returns nothing useful. Use the join key declared on the entity. - Wrong key type.
"1001"and1001are different keys. Match the type used in the source data. - Unknown feature reference. A typo in
"driver_hourly_stats:conv_rate"fails at request time. Referencing a feature service avoids this class of error. - Forgot to materialize.
feast applyregisters definitions only. It does not copy any data. An empty online store returnsNonefor every row. - Stale local registry. If you changed a feature view but did not re-run
feast apply, the serving process may read the old definition.
Consistency with the training set
The online call returns the latest value. The training call returns the value as of each row's timestamp. For a given driver at serving time, the two agree only if the online store contains what the offline source would give for "now". That holds when the same feature view definition feeds both paths, materialization is fresh enough, and TTL is chosen consistently. The next chapters on freshness and TTL build on this.
Latency considerations
A few things dominate the cost of a call:
- The online store's own read latency. SQLite is fine for local work. Production systems typically use a networked store such as Redis or DynamoDB.
- The number of feature views touched. Each view is a separate read, so fewer views per model means fewer reads.
- The number of entity rows per call. Batching helps up to a point, then response size grows.
- Process start-up. Creating a
FeatureStoreobject loads configuration and the registry. Create it once when the service starts and reuse it, rather than once per request.
Exercise
A model service calls getonlinefeatures for driver 1001 and receives None for convrate and accrate. The feature view has a TTL of one day, online=True, and feast apply was run. Name two likely causes and one check for each.
Show answer
Cause 1: the driver's data was never materialized, or materialization has not run recently. Check by running feast materialize-incremental and re-reading, or by confirming the source contains driver_id 1001 with the same type. Cause 2: the newest materialized value is older than the one-day TTL, so Feast treats it as expired. Check the event timestamp of the latest source row for that driver against the current time, and schedule materialization more often if needed.
Exercise
Why is it better to pass a FeatureService to getonlinefeatures than a hand-written list of feature strings in the serving code?
Show answer
The feature list is defined once in the repository, versioned with the rest of the feature definitions, and shared with historical retrieval for training. This avoids typos and prevents the serving code from drifting away from the features the model was trained on.