Complete Example
Here’s the completeConditionalFlow from the test suite:
How It Works
Start step evaluates the condition
The
start step:- Receives a
valueparameter (default: 42) - Evaluates the condition:
self.route = "high" if self.value >= 50 else "low" - Uses the condition parameter to select which branch to execute
Only one branch executes
Based on
self.route:- If
value >= 50: Onlyhigh_branchruns, creatingresult = "HIGH: {value}" - If
value < 50: Onlylow_branchruns, creatingresult = "LOW: {value}"
Join receives the executed branch result
Unlike static branch joins, the
join step receives results from only the executed branch. No inputs parameter is needed since there’s only one input.Runtime Efficiency: Only the selected branch executes, saving compute resources compared to static branching where all branches run.
The Conditional Pattern
The conditional syntax uses a dictionary mapping and a condition parameter:Key Components
- Branch mapping dictionary: Maps string keys to step methods
- Condition attribute: Stores the routing key as a string
- condition parameter: Tells Metaflow which attribute contains the routing key
Join Differences
Conditional joins differ from static branch joins:Creating the Dagster Asset
Convert this flow to a Dagster asset:Generated Dagster Graph
When materialized in Dagster, the conditional flow shows all possible paths, but only one executes:Difference from Static Branching: In a static branching flow, ALL branches execute in parallel and the join receives results from all. In conditional flows, ONLY ONE branch executes and the join receives only that result.
Advanced: Multi-way Conditionals
You can extend this pattern to support more than two branches:Use Cases
- Environment-based routing: Different processing for dev/staging/prod
- Data quality checks: Different remediation based on validation results
- Feature flags: Enable/disable features based on configuration
- Cost optimization: Choose cheaper processing for small datasets
- Error handling: Route to different recovery strategies based on error type
- A/B test routing: Send users down different flow paths
Key Takeaways
- Use a dictionary with
conditionparameter for dynamic branching - Store the routing key in a string attribute
- Only the selected branch executes (unlike static branching)
- Join steps don’t need an
inputsparameter - Use Parameters to control routing from outside the flow
- Supports any number of conditional branches
- More efficient than static branching when only one path is needed