Overview
TheHydrator trait defines how candidates are enriched with additional data. Hydrators run in parallel and populate specific fields on candidate objects by making async calls to external services.
Trait Definition
Type Parameters
generic
The query type that contains request context and parametersConstraints:
Clone + Send + Sync + 'staticgeneric
The candidate type that will be hydrated with additional fieldsConstraints:
Clone + Send + Sync + 'staticMethods
enable
fn
hydrate
async fn
Performs async operations to fetch data and returns candidates with hydrated fields
&Q
Reference to the query object
&[C]
Slice of candidates to hydrate
Result<Vec<C>, String>
Returns a vector with the same length and order as input, with only this hydrator’s fields populated. Returns an error message on failure.
update
fn
update_all
fn
name
fn
Returns a stable name for logging and metrics
&'static str
A short type name derived from the implementing struct
Example Implementation
Here’s a real example that hydrates tweet author information from the Gizmoduck user service:Usage Notes
- Multiple hydrators run in parallel to minimize latency
- Batch operations in
hydratefor efficiency (e.g., fetch all users in one request) - Only populate fields relevant to this hydrator in the returned candidates
- Use
Default::default()for other fields to avoid accidentally overwriting data - The pipeline merges results by calling
updatefor each hydrator - Consider implementing custom
update_allfor optimized batch updates
Best Practices
- Maintain Order: Always return candidates in the same order as received
- Batch Requests: Deduplicate IDs and make bulk requests to external services
- Partial Hydration: Handle missing data gracefully with
Optiontypes - Error Handling: Provide descriptive error messages for debugging
- Field Isolation: Only update fields owned by this hydrator in the
updatemethod
Related: QueryHydrator
TheQueryHydrator trait is used for hydrating the query/request object itself before candidate retrieval, rather than hydrating candidates.
Key Differences from Hydrator
Purpose
Purpose
- QueryHydrator: Enriches the request/query with user context (e.g., user action sequences, feature flags)
- Hydrator: Enriches candidates with additional metadata (e.g., author profiles, media info)
Execution Timing
Execution Timing
- QueryHydrator: Runs early in the pipeline, before candidate sourcing
- Hydrator: Runs after candidates are retrieved from sources
Type Parameters
Type Parameters
- QueryHydrator: Only generic over query type
Q - Hydrator: Generic over both query
Qand candidateCtypes
Example: UserActionSeqQueryHydrator
home-mixer/query_hydrators/user_action_seq_query_hydrator.rs
Query hydrators are essential for Phoenix models, which require user engagement history to make accurate predictions.
See Also
- Source - Fetches initial candidates
- Filter - Removes candidates after hydration
- Scorer - Assigns scores after hydration
- Candidate Pipeline Architecture - Pipeline execution flow