Sub topic 7
Defining feature views
This chapter assumes you have read the earlier chapters on entities and data sources. You should know what a join key is and what a timestamp column does in a source. You should also be comfortable reading a Python file that builds objects from a library.
What a feature view is
A feature view is a Feast object that describes one group of features. It ties together four things:
- The entity (or entities) the features describe.
- The data source where the values live.
- The schema, meaning the names and types of the feature columns.
- The TTL, which says how old a value may be and still count as valid.
A feature view does not hold data. It is a definition. Feast stores it in the registry when you run feast apply. Later calls such as get_historical_features and get_online_features use it to find the source columns and to decide which values are valid.
Feast also uses the feature view as the unit of storage and materialization. Features that change at the same rate and come from the same source belong in the same view.
A complete definition
The repository file below defines an entity, a source, a feature view and a feature service. It is the same file the registry chapter applies.
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,
)
driver_activity_v1 = FeatureService(
name="driver_activity_v1",
features=[driver_stats_fv[["conv_rate", "acc_rate"]]],
)The arguments one by one
| Argument | Purpose | Notes |
|---|---|---|
| `name` | Identifier in the registry | Must be unique within the project. Retrieval strings use it as the prefix, as in `driver_hourly_stats:conv_rate`. |
| `entities` | Entities the rows are keyed by | Pass the `Entity` objects, not strings of join keys. |
| `schema` | Feature columns and types | A list of `Field` objects with `dtype` from `feast.types`. |
| `ttl` | Maximum age of a value | A `timedelta`. See the next section. |
| `online` | Whether the view can be materialized | Defaults to `True`. Set it to `False` for views used only for training. |
| `source` | Where values are read from | The source supplies the timestamp columns. |
| `tags` | Free-form key-value metadata | Useful for ownership and search. Optional. |
Entities and the schema
The join keys of the entities are columns in the source, so you do not list them in schema. Only list feature columns there. Feast reads the entity key columns through the entity definition.
A feature view can take more than one entity, for example a driver and a customer. Every row is then keyed by the combination of both join keys. Every retrieval call must supply all of those keys.
The dtype should match what the source actually holds. Feast has no automatic cast that rescues a wrong type. A column stored as a double in Parquet and declared Int64 can fail at retrieval or truncate values. Use Float32 or Float64, Int32 or Int64, String, Bool and the other types in feast.types deliberately.
The online flag
online=True means feast materialize can copy this view's latest values into the online store. With online=False the view still works for get_historical_features, and Feast writes nothing to the online store for it. Turn it off for large views that no model reads at serving time. This saves storage and materialization time.
TTL: how far back a value is valid
The ttl argument is a time window. In a historical join, Feast looks for the latest source row whose event_timestamp is at or before the entity row's timestamp. It accepts that row only if it falls within the TTL before that timestamp.
Here is an example. A driver row has event_timestamp of 2025-01-01 12:00, and the view has a one-day TTL. A feature value stamped 2025-01-01 09:00 is used. A value stamped 2024-12-30 09:00 is too old. The result for that feature is null, not the stale value.
This protects training data from silently using facts that were true long ago. Choose the TTL from the real change rate of the data:
- Hourly statistics might use a TTL of a day or two.
- A slowly changing profile attribute might need weeks.
- A TTL that is too short produces nulls. A TTL that is too long hides stale data.
In recent Feast versions a TTL of timedelta(0) means values never expire. Check the version you run before relying on this. A later chapter covers how TTL and freshness interact when serving online.
Inspecting what Feast registered
After feast apply, read the definition back from the registry. This confirms that the schema Feast holds matches what you intended.
from feast import FeatureStore
store = FeatureStore(repo_path=".")
fv = store.get_feature_view("driver_hourly_stats")
print(fv.name, fv.ttl, fv.online)
for field in fv.features:
print(field.name, field.dtype)Common errors
- Schema column missing in the source. If a
Fieldname does not match a column in the Parquet file, retrieval fails with a missing-column error. Fix the name, or in a source that supports it, map the column explicitly. Compare the source's real columns with the schema first. - Wrong or missing timestamp field. Feast needs
timestamp_fieldon the source. Without a timestamp, point-in-time joins and incremental materialization cannot work. - Join key mismatch. The entity's
join_keysmust be a column in the source, and the entity rows you pass at retrieval must use the same name.driver_idin one place anddriverIdin another causes a failure. - Forgetting to re-apply. The registry holds the old definition until you run
feast applyagain. Edits to the Python file do not take effect before that. - Changing a view in place. Renaming a view or changing types creates a new registry state. Models that request old names break. Keep a renamed view alongside the old one until consumers have moved.
- TTL shorter than the update interval. If the source updates daily and the TTL is one hour, most historical lookups return null.
Feature views and feature services
A feature view can be sliced with indexing, such as driver_stats_fv[["conv_rate", "acc_rate"]]. That expression produces a projection, and a FeatureService uses it to expose only part of a view to a model. The projection does not change the view. It only controls what that service returns. Feature services have their own chapter.
Exercise
A feature view has ttl=timedelta(hours=6). A training row has event_timestamp 2025-03-01 12:00. The latest source row for that driver has event_timestamp 2025-03-01 04:00. What does get_historical_features return for that feature, and why? Which change would fix it without leaking future data?
Show answer
It returns null. The latest valid row is eight hours before the entity timestamp, which is outside the six-hour TTL, so Feast treats it as expired. You can raise the TTL to at least eight hours if that matches how often the source updates, or fix the pipeline so fresh rows are written more often. Do not use a row stamped after 12:00, because that would leak future information.
Exercise
You add a new column rating to the Parquet source but do not touch the Python file. A teammate runs get_historical_features with driver_hourly_stats:rating. What happens, and what two steps are needed to make it work?
Show answer
Retrieval fails because the feature is not part of the registered feature view. The steps are to add Field(name="rating", dtype=Float32) (or the matching type) to the view's schema and then run feast apply so the registry holds the new definition.