ConditionalRouter
Use ConditionalRouter to route data to different pipeline branches based on conditions you define. Each condition is a Jinja2 expression evaluated at runtime against the component's inputs.
Key Features
- Routes data to different branches based on configurable Jinja2 conditions
- Supports multiple routes, each with its own condition, output value, output type, and output name
- Connects to downstream components by output name
- Supports custom Jinja2 filters for advanced condition logic
- Handles optional variables gracefully with fallback routes
Configuration
- Drag the
ConditionalRoutercomponent onto the canvas from the Component Library. - Click the component to open the configuration panel.
- On the General tab:
- Define the
routeslist. Each route is a dictionary with four fields:condition: A Jinja2 string expression that determines if the route is selected (for example,"{{ streams|length > 2 }}").output: A Jinja2 expression defining the route's output value (for example,"{{ streams }}").output_type: The type of the output data (for example,str,List[int]).output_name: The name used to connect this route to other components in the pipeline (for example,enough_streams).
- Define the
- Go to the Advanced tab to configure custom Jinja2 filters, unsafe template execution, output type validation, and optional variables.
Connections
ConditionalRouter accepts any variables referenced in your route condition and output expressions as inputs. It creates one output connection for each route, named after the route's output_name. Connect each output to the appropriate downstream component.
Usage Example
components:
ConditionalRouter:
type: components.routers.conditional_router.ConditionalRouter
init_parameters:
Parameters
Inputs
| Parameter | Type | Default | Description |
|---|---|---|---|
kwargs | Any | All variables used in the condition expressions in the routes. When used in a pipeline, these variables are passed from the previous component's output. |
Outputs
The component creates one output per route, named after the route's output_name. The output type is determined by the route's output_type.
Init Parameters
These are the parameters you can configure in Pipeline Builder:
| Parameter | Type | Default | Description |
|---|---|---|---|
routes | List[Route] | A list of dictionaries, each defining a route. Each route has these four elements: condition (a Jinja2 string expression that determines if the route is selected), output (a Jinja2 expression defining the route's output value), output_type (the type of the output data, for example str or List[int]), and output_name (the name used to connect the router to other components in the pipeline). | |
custom_filters | Optional[Dict[str, Callable]] | None | A dictionary of custom Jinja2 filters used in the condition expressions. For example, passing {"my_filter": my_filter_fcn} where my_filter is the filter name and my_filter_fcn takes a string and returns a transformed value. Use as {{ my_var|my_filter }} in a condition expression. |
unsafe | bool | False | Enables execution of arbitrary code in the Jinja template. Only use this if you trust the source of the template, as it can lead to remote code execution. |
validate_output_type | bool | False | Enables validation of routes' output types. If a route output doesn't match the declared type, a ValueError is raised at runtime. |
optional_variables | Optional[List[str]] | None | A list of variable names that are optional in your route conditions and outputs. If these variables are not provided at runtime, they are set to None. This lets you write routes that handle missing inputs gracefully without raising errors. |
Run Method Parameters
These are the parameters you can configure for the component's run() method. This means you can pass these parameters at query time through the API, in Playground, or when running a job. For details, see Modify Pipeline Parameters at Query Time.
| Parameter | Type | Default | Description |
|---|---|---|---|
kwargs | Any | All variables used in the condition expressions in the routes. When used in a pipeline, these variables are passed from the previous component's output. |
Was this page helpful?