AnswerJoiner
Use AnswerJoiner to merge multiple lists of Answer objects from different pipeline branches into a single list.
Key Features
- Merges multiple
Answerlists from different pipeline branches into one. - Supports concatenation as the join mode.
- Optionally limits the number of answers returned with
top_k. - Optionally sorts answers by score in descending order.
Configuration
- Drag the
AnswerJoinercomponent onto the canvas from the Component Library. - Click on the component to open the configuration panel.
- On the General tab:
- Set the Join Mode. The available mode is
concatenate, which merges all input answer lists into a single list. - Optionally, set Top K to limit the number of answers returned.
- Set the Join Mode. The available mode is
- Go to the Advanced tab to enable Sort by Score if you want answers sorted in descending order by their score.
Connections
AnswerJoiner accepts lists of Answer objects from multiple components simultaneously. You typically connect it to AnswerBuilder components that produce answers from different generators. Its output is a merged List[AnswerType] that you connect to the Output component.
Source Code
To check this component's source code, open answer_joiner.py in the Haystack repository.
Usage Examples
Basic Configuration
AnswerJoiner:
type: haystack.components.joiners.answer_joiner.AnswerJoiner
init_parameters:
join_mode: concatenate
sort_by_score: false
This example shows a pipeline that generates answers from multiple sources and joins them.
components:
PromptBuilder1:
type: haystack.components.builders.prompt_builder.PromptBuilder
init_parameters:
template: "Answer this question briefly: {{ query }}"
PromptBuilder2:
type: haystack.components.builders.prompt_builder.PromptBuilder
init_parameters:
template: "Provide a detailed answer to: {{ query }}"
Generator1:
type: haystack.components.generators.openai.OpenAIGenerator
init_parameters:
api_key:
type: env_var
env_vars:
- OPENAI_API_KEY
strict: false
model: gpt-4o-mini
Generator2:
type: haystack.components.generators.openai.OpenAIGenerator
init_parameters:
api_key:
type: env_var
env_vars:
- OPENAI_API_KEY
strict: false
model: gpt-4o-mini
AnswerBuilder1:
type: haystack.components.builders.answer_builder.AnswerBuilder
init_parameters:
pattern:
reference_pattern:
last_message_only: false
return_only_referenced_documents: true
AnswerBuilder2:
type: haystack.components.builders.answer_builder.AnswerBuilder
init_parameters:
pattern:
reference_pattern:
last_message_only: false
return_only_referenced_documents: true
AnswerJoiner:
type: haystack.components.joiners.answer_joiner.AnswerJoiner
init_parameters:
join_mode: concatenate
sort_by_score: false
connections:
- sender: PromptBuilder1.prompt
receiver: Generator1.prompt
- sender: PromptBuilder2.prompt
receiver: Generator2.prompt
- sender: Generator1.replies
receiver: AnswerBuilder1.replies
- sender: Generator2.replies
receiver: AnswerBuilder2.replies
- sender: AnswerBuilder1.answers
receiver: AnswerJoiner.answers
- sender: AnswerBuilder2.answers
receiver: AnswerJoiner.answers
max_runs_per_component: 100
metadata: {}
inputs:
query:
- PromptBuilder1.query
- PromptBuilder2.query
- AnswerBuilder1.query
- AnswerBuilder2.query
outputs:
answers: AnswerJoiner.answers
Parameters
Inputs
| Parameter | Type | Description |
|---|---|---|
answers | Variadic[List[AnswerType]] | Nested list of answers to merge. |
top_k | Optional[int] | The maximum number of answers to return. Overrides the instance's top_k if provided. |
Outputs
| Parameter | Type | Description |
|---|---|---|
answers | List[AnswerType] | Merged list of answers. |
Init Parameters
These are the parameters you can configure in Pipeline Builder:
| Parameter | Type | Default | Description |
|---|---|---|---|
join_mode | Union[str, JoinMode] | JoinMode.CONCATENATE | Specifies the join mode to use. Available modes: concatenate concatenates multiple lists of answers into a single list. |
top_k | Optional[int] | None | The maximum number of answers to return. |
sort_by_score | bool | False | If True, sorts the answers by score in descending order. If an answer has no score, it is handled as if its score is -infinity. |
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 |
|---|---|---|---|
answers | Variadic[List[AnswerType]] | Nested list of answers to merge. | |
top_k | Optional[int] | None | The maximum number of answers to return. Overrides the instance's top_k if provided. |
Was this page helpful?