BranchJoiner
Merge multiple branches of a pipeline into a single output.
Basic Information
- Type:
haystack.components.joiners.branch.BranchJoiner - Components it can connect with:
- Routers: Receives outputs from conditional branches (for example,
ConditionalRouter,TextLanguageRouter).
- Routers: Receives outputs from conditional branches (for example,
Inputs
| Parameter | Type | Default | Description |
|---|---|---|---|
| **kwargs | Any | The input data. Must be of the type declared by type_ during initialization. |
Outputs
| Parameter | Type | Default | Description |
|---|---|---|---|
| -- | -- | -- | The output is the first value BranchJoiner receives from connected components. |
Overview
BranchJoiner merges multiple input branches of a pipeline into a single branch. It receives inputs of the same data type and forwards the first received value to its output. It handles one input of one data type at a time. You can declare the data type when configuring the component.
If the component receives more than one value for the input, it raises the following error: ValueError: BranchJoiner expects only one input, but <number of inputs> were received..
Common Use Cases
-
Loop Handling: BranchJoiner helps close loops in pipelines. For example, if a pipeline component validates or modifies incoming data and produces an error-handling branch,
BranchJoinercan merge both branches and send (or resend in the case of a loop) the data to the component that evaluates errors. -
Decision-Based Merging:
BranchJoinerreconciles branches coming from Router components (such asConditionalRouter,TextLanguageRouter). Suppose aTextLanguageRouterdirects user queries to different retrievers based on the detected language. Each retriever processes its assigned query and passes the results toBranchJoiner, which consolidates them into a single output before passing them to the next component.
BranchJoiner can manage only one data type at a time. When creating BranchJoiner, you must specify the type of data it will receive and pass (for example, List[ChatMessage], str, List[Document]).
Usage Example
This example shows a pipeline where ConditionalRouter routes queries based on whether they contain a question mark (indicating a question vs. a statement), and BranchJoiner consolidates the outputs from different processing approaches.
components:
ConditionalRouter:
type: haystack.components.routers.conditional_router.ConditionalRouter
init_parameters:
routes:
- condition: "{{ '?' in query }}"
output: "{{ query }}"
output_name: question
output_type: str
- condition: "{{ '?' not in query }}"
output: "{{ query }}"
output_name: statement
output_type: str
QuestionPromptBuilder:
type: haystack.components.builders.prompt_builder.PromptBuilder
init_parameters:
template: "Provide a detailed answer to this question: {{ query }}"
StatementPromptBuilder:
type: haystack.components.builders.prompt_builder.PromptBuilder
init_parameters:
template: "Acknowledge and expand on this statement: {{ query }}"
QuestionGenerator:
type: haystack.components.generators.openai.OpenAIGenerator
init_parameters:
api_key:
type: env_var
env_vars:
- OPENAI_API_KEY
strict: false
model: gpt-4o-mini
generation_kwargs:
temperature: 0.3
max_tokens: 500
StatementGenerator:
type: haystack.components.generators.openai.OpenAIGenerator
init_parameters:
api_key:
type: env_var
env_vars:
- OPENAI_API_KEY
strict: false
model: gpt-4o-mini
generation_kwargs:
temperature: 0.7
max_tokens: 200
BranchJoiner:
type: haystack.components.joiners.branch.BranchJoiner
init_parameters:
type_: list[str]
connections:
- sender: ConditionalRouter.question
receiver: QuestionPromptBuilder.query
- sender: ConditionalRouter.statement
receiver: StatementPromptBuilder.query
- sender: QuestionPromptBuilder.prompt
receiver: QuestionGenerator.prompt
- sender: StatementPromptBuilder.prompt
receiver: StatementGenerator.prompt
- sender: QuestionGenerator.replies
receiver: BranchJoiner.value
- sender: StatementGenerator.replies
receiver: BranchJoiner.value
max_runs_per_component: 100
metadata: {}
inputs:
query:
- ConditionalRouter.query
outputs:
replies: BranchJoiner.value
Parameters
Init Parameters
These are the parameters you can configure in Pipeline Builder:
| Parameter | Type | Default | Description |
|---|---|---|---|
| type_ | Type | The expected data type of inputs and outputs. |
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 | The input data. Must be of the type declared by type_ during initialization. |
Was this page helpful?