Skip to main content
For the complete documentation index for agents and LLMs, see llms.txt.

AnswerJoiner

Use AnswerJoiner to merge multiple lists of Answer objects from different pipeline branches into a single list.

Key Features​

  • Merges multiple Answer lists 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​

  1. Drag the AnswerJoiner component onto the canvas from the Component Library.
  2. Click on the component to open the configuration panel.
  3. 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.
  4. 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.

# haystack-pipeline
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.chat.openai.OpenAIChatGenerator
init_parameters:
api_key:
type: env_var
env_vars:
- OPENAI_API_KEY
strict: false
model: gpt-4o-mini
Generator2:
type: haystack.components.generators.chat.openai.OpenAIChatGenerator
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.messages
- sender: PromptBuilder2.prompt
receiver: Generator2.messages
- 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​

ParameterTypeDescription
answersVariadic[List[AnswerType]]Nested list of answers to merge.
top_kOptional[int]The maximum number of answers to return. Overrides the instance's top_k if provided.

Outputs​

ParameterTypeDescription
answersList[AnswerType]Merged list of answers.

Init Parameters​

These are the parameters you can configure in Pipeline Builder:

ParameterTypeDefaultDescription
join_modeUnion[str, JoinMode]JoinMode.CONCATENATESpecifies the join mode to use. Available modes: concatenate concatenates multiple lists of answers into a single list.
top_kOptional[int]NoneThe maximum number of answers to return.
sort_by_scoreboolFalseIf 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.

ParameterTypeDefaultDescription
answersVariadic[List[AnswerType]]Nested list of answers to merge.
top_kOptional[int]NoneThe maximum number of answers to return. Overrides the instance's top_k if provided.