Sub topic 5
Entities and join keys
This chapter covers how Feast identifies the "thing" a feature describes. It assumes you know basic Python and pandas, and that you have seen the registry, the offline store and the online store described in earlier chapters. You do not need a running Feast project to follow the ideas, but the code is written to run inside the driver_project repository used throughout this tutorial.
What an entity is
An entity is the real-world object that features describe. In a ride-hailing example the entity is a driver. In a fraud model it could be a customer, a card or a merchant. Every feature value belongs to exactly one entity instance, such as driver 1001.
In Feast an Entity object does two jobs:
- It names the concept (
driver), so that feature views can say which concept they describe. - It declares the join key, the column that identifies one instance of that concept in your data.
The join key is the link between your three data locations. It is a column in the offline source, a lookup key in the online store, and a column in the entity DataFrame you pass when building a training set.
from feast import Entity
driver = Entity(
name="driver",
join_keys=["driver_id"],
description="A driver registered on the platform",
)The name is how Feast refers to the entity inside the registry. The join_keys list names the column that must exist in every data source attached to a feature view for this entity. In the example above, the Parquet file behind driver_hourly_stats_source must have a driver_id column.
Why join keys matter
Feast uses the join key in three places.
- Historical retrieval.
get_historical_featurestakes an entity DataFrame. Feast joins feature rows to it by the join key, then picks the right row by timestamp (covered in the point-in-time chapter). - Materialization. When Feast copies values into the online store, it stores them under a key built from the entity's join key value.
- Online retrieval.
get_online_featuresreceivesentity_rowssuch as{"driver_id": 1001}. Feast uses that value to look up the stored row.
entity_rows = [{"driver_id": 1001}, {"driver_id": 1002}]If the key name in your request does not match the join key declared on the entity, Feast cannot find the features. The request fails, or returns nulls, depending on the call and version.
The next diagram shows how one join key value flows through the system.
Choosing a good join key
An entity should be something that is stable and available at both training time and serving time. Some practical rules follow.
- Use an identifier that exists at inference time. If the serving system only knows a session ID but your features are keyed by customer ID, you must resolve the customer ID first. Feast will not do that mapping for you.
- Keep the key unique per feature row in time. For one join key value, the source can hold many rows, one per timestamp. It must not hold several different rows with the same key and the same timestamp. If it does, Feast picks using the
created_timestamp_column, and anything beyond that is ambiguous. - Keep the type consistent. If
driver_idis an integer in the source and a string in the entity DataFrame, the join can fail or silently match nothing. Check the types on both sides. - Do not use a feature as a key. A value that changes over time, such as a current city, does not identify a stable object.
Entity names versus column names
The entity name and the join key column do not have to be the same. Here the entity is called driver and the column is driver_id. Feature views refer to the entity by object (entities=[driver]), and data retrieval uses the column name (driver_id).
This difference trips people up. In entity_rows, use the column name. In a feature view definition, pass the Entity object.
Entities in a feature view
A feature view connects an entity to a source and a schema. The entity tells Feast which source column is the key.
from datetime import timedelta
from feast import Entity, 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,
)Notice that the schema lists only features. It does not list driver_id. Feast adds the join key columns from the entity. If you also listed driver_id in the schema, you would be declaring it as a feature, which is usually a mistake.
Composite keys
Some objects are identified by more than one column. A product in a store may be identified by store_id and sku together. Feast supports this by listing several columns in join_keys, or by attaching several entities to one feature view.
The two designs behave differently:
| Design | Definition | When to use it |
|---|---|---|
| One entity, several join keys | `Entity(name="store_product", join_keys=["store_id", "sku"])` | The pair is a single concept, and you never look up by one column alone. |
| Several entities on one view | `entities=[store, product]` | Each part is a concept of its own and is reused by other feature views. |
Support for several join keys on a single Entity differs between Feast versions, so check the documentation for the version you run. Using several entities on one feature view is the long-standing and better-supported route. With that design, you must supply every key in each online request:
from feast import FeatureStore
store = FeatureStore(repo_path=".")
result = store.get_online_features(
features=["store_product_stats:units_sold_7d"],
entity_rows=[{"store_id": 12, "sku": "A-100"}],
).to_dict()Leaving out one key causes an error, because the stored row is addressed by the full combination.
Sharing entities across feature views
Entities are what let different feature views be combined in one request. Suppose you have a second feature view, driver_profile, also keyed on driver. A single call can fetch features from both views, because both use the same join key:
features = [
"driver_hourly_stats:conv_rate",
"driver_profile:years_active",
]Feast reads each view separately and then joins the results on driver_id. This is why you should define an entity once and reuse the object, rather than creating a second Entity for the same concept. Duplicate definitions with different names or key columns make joins across views impossible or confusing.
If two views have different entities, for example driver and customer, a request for features from both needs both keys in the entity rows.
Entities and the registry
When you run feast apply, Feast writes each entity to the registry along with its name, join keys and description. The registry does not hold entity instances. It does not know that driver 1001 exists. It only stores the definition of the concept. The actual keys live in your data source and, after materialization, in the online store.
Changing a join key on an existing entity is a breaking change. Feature views that reference it, and any data already materialized under the old key, no longer line up. Treat a join key rename like a schema migration: update the definition, re-apply, and re-materialize.
Common errors
- Key column missing from the source. The source file or table lacks the column named in
join_keys. Apply or retrieval fails with a message about a missing column. - Wrong key name in the request. Using
driverinstead ofdriver_idinentity_rows. Use the column name. - Type mismatch. Integer keys in the source and string keys in the request. Convert before calling Feast.
- Duplicate entity definitions. Two
Entityobjects for one concept. Define it once and import it. - Key not available at serving time. A key that training data has but live traffic does not. Settle this in design, before you build features.
Exercise
A feature view merchant_stats uses the entity Entity(name="merchant", join_keys=["merchant_id"]). A teammate calls get_online_features(features=["merchant_stats:chargeback_rate"], entity_rows=[{"merchant": 77}]) and gets no usable values. What is wrong, and how do you fix it?
Show answer
The request uses the entity name merchant instead of the join key column name merchant_id. Feast looks up rows by the join key, so it cannot find a key for the lookup. Change the request to entity_rows=[{"merchant_id": 77}], and make sure the value has the same type (integer) as the merchant_id column in the source.
Exercise
You have a feature view of product stats that must be looked up by store_id and sku together, and another feature view that has only store-level features keyed by store_id. Which design lets you request features from both views in one call, and which keys must the entity rows contain?
Show answer
Define separate store and product entities, and put entities=[store, product] on the product stats view and entities=[store] on the store-level view. A request for features from both views needs both keys in each entity row: {"store_id": 12, "sku": "A-100"}. Feast joins the store-level view on store_id and the product view on the pair.