Sub topic 13
Grouping features for a model with feature services
This chapter assumes you have a working Feast repository with an entity, a data source and a feature view, as built in the earlier chapters. You should also know how get_historical_features and get_online_features are called. Here you will group features into a named, versioned unit that a model can depend on.
The problem: models and feature views don't line up
A feature view is organized around a data source and an entity. A model is organized around what it was trained on. These two shapes rarely match.
- One feature view may hold ten features, and a model may use only three.
- A model may need features from several feature views, such as driver, customer and location.
- Two models may share a feature view but use different columns from it.
Without a grouping object, every training script and every serving endpoint carries its own list of strings like "driver_hourly_stats:conv_rate". When one list is edited and the other is not, training and serving no longer use the same features. That is a source of training-serving skew.
A feature service is a named, registered list of features, which Feast stores in the registry. It is a reference to features, not a copy of data. You define it once, and both retrieval paths accept it.
Defining a feature service
A feature service takes a name and a list of feature views. To pick only some columns of a view, index it with a list of feature names.
from datetime import timedelta
from feast import Entity, FeatureService, FeatureView, Field, FileSource
from feast.types import Float32, Int64
driver = Entity(name="driver", join_keys=["driver_id"])
driver_stats_source = FileSource(
name="driver_hourly_stats_source",
path="data/driver_stats.parquet",
timestamp_field="event_timestamp",
created_timestamp_column="created",
)
driver_stats_fv = FeatureView(
name="driver_hourly_stats",
entities=[driver],
ttl=timedelta(days=1),
schema=[
Field(name="conv_rate", dtype=Float32),
Field(name="acc_rate", dtype=Float32),
Field(name="avg_daily_trips", dtype=Int64),
],
online=True,
source=driver_stats_source,
)
# Only two of the three features
driver_activity_v1 = FeatureService(
name="driver_activity_v1",
features=[driver_stats_fv[["conv_rate", "acc_rate"]]],
)
# All features of the view
driver_activity_v2 = FeatureService(
name="driver_activity_v2",
features=[driver_stats_fv],
)Two forms appear here.
| Form in `features=[...]` | Meaning |
|---|---|
| `driver_stats_fv` | Every feature in the view's schema |
| `driver_stats_fv[["conv_rate", "acc_rate"]]` | Only the named features (a projection) |
The service name must be unique within the project. This chapter puts a version suffix such as _v1 in the name. A service name is the contract with a model, so a model with a different feature set gets a new service instead of an edited one.
After you run feast apply, the service is stored in the registry. You can check it from the command line.
feast apply
feast feature-services listUsing a service in training
Pass the feature service object to get_historical_features in place of a list of strings. Fetch the object from the registry with get_feature_service.
from datetime import datetime
import pandas as pd
from feast import FeatureStore
store = FeatureStore(repo_path=".")
entity_df = pd.DataFrame(
{
"driver_id": [1001, 1002],
"event_timestamp": [datetime(2025, 1, 1, 12, 0), datetime(2025, 1, 1, 13, 0)],
}
)
feature_service = store.get_feature_service("driver_activity_v1")
training_df = store.get_historical_features(
entity_df=entity_df,
features=feature_service,
).to_df()
print(training_df.columns.tolist())The result contains driver_id, event_timestamp, conv_rate and acc_rate. avg_daily_trips is absent because the service does not list it. Point-in-time correctness works as before, because the service only decides which features are joined.
Using the same service at serving time
The online call accepts the same object.
from feast import FeatureStore
store = FeatureStore(repo_path=".")
feature_service = store.get_feature_service("driver_activity_v1")
online_features = store.get_online_features(
features=feature_service,
entity_rows=[{"driver_id": 1001}],
).to_dict()
print(online_features)Training and inference now ask for the same name. If a model is retrained on driver_activity_v1, the serving code does not need to enumerate columns, so the two cannot drift apart through a typo or a forgotten feature.
Features from several views
A service may list projections from more than one feature view. Feast joins each view through its own entity's join keys. For historical retrieval, the entity dataframe must contain every join key that the views need. For online retrieval, each entity row must carry those keys as well. If a model uses a driver view and a customer view, each row needs both driver_id and customer_id.
Output column names
When two views have a feature with the same name, the output columns would collide. Setting full_feature_names=True prefixes each column with its view name, as in driver_hourly_stats__conv_rate.
training_df = store.get_historical_features(
entity_df=entity_df,
features=store.get_feature_service("driver_activity_v1"),
full_feature_names=True,
).to_df()Choose one setting per model and use it on both paths. A model trained on conv_rate will fail or misbehave if serving hands it driver_hourly_stats__conv_rate.
Common errors and edge cases
- Editing a service in place. Changing the features of
driver_activity_v1after a model was trained on it changes what the model receives at serving time. Createdriver_activity_v2instead, deploy the new model, then retire the old service. - Removing a feature a service lists. If you delete a column from a feature view's schema while a service still names it, the service no longer resolves. Update the service in the same change and re-run
feast apply. - Forgetting
feast applyafter edits.get_feature_servicereads from the registry, not from your Python file. An unapplied service raises a not-found error. - Assuming a service materializes data. It does not. Materialization is per feature view. A service over views that were never materialized returns nulls online.
- Different TTLs inside one service. A service with views that have different TTLs can return fresh values for one feature and null for another. Check the TTL of every view it lists.
When not to use one
For quick exploration in a notebook, a plain list of "view:feature" strings is fine. Use a feature service when a model is going to be trained repeatedly or served, because that is when the training and serving lists must stay identical.
Exercise
A model was trained on driver_activity_v1, which lists conv_rate and acc_rate. A teammate edits that service to add avg_daily_trips and runs feast apply, then the serving code, which calls get_online_features(features=store.get_feature_service("driver_activity_v1"), ...), is left unchanged. What goes wrong, and what should the teammate have done instead?
Show answer
The serving code now returns three features, but the model was trained on two, so the input no longer matches what the model expects. This can cause an error or silently wrong predictions. The teammate should have created a new service, such as driver_activity_v2, trained and deployed a model against it, and left driver_activity_v1 unchanged for the old model until it is retired.
Exercise
You want a model to use conv_rate from driver_hourly_stats and one feature from a customer_stats view keyed by customer_id. What must each row of the entity dataframe contain for get_historical_features?
Show answer
It must contain the event_timestamp column and both join keys, driver_id and customer_id, because Feast joins each feature view through its own entity's join keys.