Skip to main content

AnswerJoiner

Merge multiple lists of Answer objects into a single list.

Basic Information

  • Type: haystack.components.joiners.answer_joiner.AnswerJoiner
  • Components it can connect with:
    • AnswerBuilder: Receives answers from multiple answer builders to combine their outputs.
    • Output: Sends the merged answers to the Output component.

Inputs

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

Outputs

ParameterTypeDefaultDescription
answersList[AnswerType]A dictionary with the following keys: - answers: Merged list of Answers

Overview

AnswerJoiner merges multiple lists of Answer objects into a single list. Use this component to combine answers from different generators into a unified output.

Currently, the component supports the CONCATENATE join mode, which concatenates multiple lists of answers into a single list.

Usage Example

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

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 documents by score in descending order. If a document 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 be merged.
top_kOptional[int]NoneThe maximum number of Answers to return. Overrides the instance's top_k if provided.