Sub topic 5
Entities and Join Keys
This chapter assumes you know Python basics, pandas DataFrames, training rows, labels, and model inference. It also assumes you understand that batch data is processed in larger groups, while online serving must return values quickly for one request.
What an entity represents
An entity is the thing that a feature describes. In a driver prediction system, a driver is an entity. In another system, the entity might be a customer, account, product, device, or store.
An entity has a stable identifier called a join key. The join key connects an entity row to its feature values. For a driver, driver_id might be the join key:
| Entity | Join key | Example value |
|---|---|---|
| Driver | `driver_id` | `1001` |
| Customer | `customer_id` | `5012` |
| Product | `product_id` | `8834` |
The identifier should describe identity, not a changing property. A driver's current city is not usually a good join key because the city can change. The driver's ID remains the same.
In Feast, define an entity with Entity. The join_keys argument names the columns Feast uses when it connects entity rows to feature data.
from feast import Entity
driver = Entity(
name="driver",
join_keys=["driver_id"],
)The name is the logical name used by the feature repository. The join_keys value is the physical column name expected in entity data and feature sources.
How joins use the key
Suppose a training DataFrame contains the rows for which you want predictions:
import pandas as pd
entity_df = pd.DataFrame(
{
"driver_id": [1001, 1002],
"event_timestamp": [
"2025-01-01 12:00:00",
"2025-01-01 13:00:00",
],
}
)The driver_id column tells Feast which driver each row describes. The event_timestamp column tells Feast when that row existed. Feast uses both values during historical retrieval: the join key selects the entity, and the timestamp selects the correct historical feature value for that entity.
For online retrieval, the same key identifies the entity whose current materialized features should be returned:
entity_rows = [
{"driver_id": 1001},
{"driver_id": 1002},
]The key name must match across the entity definition, source data, training DataFrame, and online request. A mismatch such as id in one place and driver_id in another prevents the join or returns missing values.
Single and composite entities
Many features belong to one entity, such as a driver's average trip count. Some features belong to a combination of entities. For example, a route feature may describe a driver on a particular route. Its identity requires both driver_id and route_id.
from feast import Entity
driver_route = Entity(
name="driver_route",
join_keys=["driver_id", "route_id"],
)This is a composite key because the pair identifies the entity. The pair (1001, "airport-city") is different from (1002, "airport-city").
Common errors
- Using a display name instead of a stable identifier.
- Changing the key column name between offline and online data.
- Omitting one part of a composite key.
- Treating a null key as a valid entity.
- Assuming two entities with the same numeric ID are the same when their ID namespaces differ.
Exercise
A feature source uses customer_id, but online requests send id. What should you change?
Show answer
Use the same join-key name in the Entity definition, feature source, and online request, or explicitly rename the request field before retrieval.