DocumentationAPI Reference📓 Tutorials🧑‍🍳 Cookbook🤝 Integrations💜 Discord🎨 Studio
Documentation

ConditionalRouter

ConditionalRouter routes your data through different paths down the Pipeline by evaluating the conditions that you specified.

NameConditionalRouter
Folder Path/routers/
Position in a Pipelineflexible
Inputs“**kwargs”: input variables to evaluate in order to choose a specific route
OutputsA Dictionary containing the output name and value of the chosen route

Overview

To use ConditionalRouter you need to define a list of routes.
Each route is a dictionary with the following 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]).
  • 'output_name': The name under which the output value of the route is published. This name is used to connect the router to other components in the pipeline.

Usage

On its own

This component is primarily meant to be used in Pipelines.

In this example, we configure two routes. The first route sends the 'streams' value to 'enough_streams' if the stream count exceeds two. Conversely, the second route directs 'streams' to 'insufficient_streams' when there are two or fewer streams.

from haystack.components.routers import ConditionalRouter

routes = [
    {
        "condition": "{{streams|length > 2}}",
        "output": "{{streams}}",
        "output_name": "enough_streams",
        "output_type": List[int],
    },
    {
        "condition": "{{streams|length <= 2}}",
        "output": "{{streams}}",
        "output_name": "insufficient_streams",
        "output_type": List[int],
    },
]

router = ConditionalRouter(routes)

kwargs = {"streams": [1, 2, 3], "query": "Haystack"}
result = router.run(**kwargs)

print(result)
# {"enough_streams": [1, 2, 3]}

In a Pipeline

Below is an example of a simple Pipeline that routes a query based on its length.

If the query is too short, you will see a warning message, and the process will end.
If the query is long enough, it is first forwarded to the PromptBuilder, then to the Generator, which generates the final answer.

from haystack import Pipeline
from haystack.components.routers import ConditionalRouter
from haystack.components.builders.prompt_builder import PromptBuilder
from haystack.components.generators import OpenAIGenerator

routes = [
        {
            "condition": "{{query|length > 10}}",
            "output": "{{query}}",
            "output_name": "ok_query",
            "output_type": str,
        },
        {
            "condition": "{{query|length <= 10}}",
            "output": "query is too short: {{query}}",
            "output_name": "too_short_query",
            "output_type": str,
        },
    ]
router = ConditionalRouter(routes=routes)

pipe = Pipeline()
pipe.add_component("router", router)
pipe.add_component("prompt_builder", PromptBuilder("Answer the following query. {{query}}"))
pipe.add_component("generator", OpenAIGenerator()
pipe.connect("router.ok_query", "prompt_builder.query")
pipe.connect("prompt_builder", "generator")

pipe.run(data={"router": {"query": "Berlin"}})
# {'router': {'too_short_query': 'query is too short: Berlin'}}

pipe.run(data={"router": {"query": "What is the capital of Italy?"}})
# {'generator': {'replies': ['The capital of Italy is Rome.'], ...}

Related Links

See the parameters details in our API reference: