listjoiner.mdx
1 --- 2 title: "ListJoiner" 3 id: listjoiner 4 slug: "/listjoiner" 5 description: "A component that joins multiple lists into a single flat list." 6 --- 7 8 # ListJoiner 9 10 A component that joins multiple lists into a single flat list. 11 12 <div className="key-value-table"> 13 14 | | | 15 | --- | --- | 16 | **Most common position in a pipeline** | In indexing and query pipelines, after components that return lists of documents such as multiple [Retrievers](../retrievers.mdx) or multiple [Converters](../converters.mdx) | 17 | **Mandatory run variables** | `values`: The dictionary of lists to be joined | 18 | **Output variables** | `values`: A dictionary with a `values` key containing the joined list | 19 | **API reference** | [Joiners](/reference/joiners-api) | 20 | **GitHub link** | https://github.com/deepset-ai/haystack/blob/main/haystack/components/joiners/list_joiner.py | 21 22 </div> 23 24 ## Overview 25 26 The `ListJoiner` component combines multiple lists into one list. It is useful for combining multiple lists from different pipeline components, merging LLM responses, handling multi-step data processing, and gathering data from different sources into one list. 27 28 The items stay in order based on when each input list was processed in a pipeline. 29 30 You can optionally specify a `list_type_` parameter to set the expected type of the lists being joined (for example, `List[ChatMessage]`). If not set, `ListJoiner` will accept lists containing mixed data types. 31 32 ## Usage 33 34 ### On its own 35 36 ```python 37 from haystack.components.joiners import ListJoiner 38 39 list1 = ["Hello", "world"] 40 list2 = ["This", "is", "Haystack"] 41 list3 = ["ListJoiner", "Example"] 42 43 joiner = ListJoiner() 44 45 result = joiner.run(values=[list1, list2, list3]) 46 47 print(result["values"]) 48 ``` 49 50 ### In a pipeline 51 52 ```python 53 from haystack.components.builders import ChatPromptBuilder 54 from haystack.components.generators.chat import OpenAIChatGenerator 55 from haystack.dataclasses import ChatMessage 56 from haystack import Pipeline 57 from haystack.components.joiners import ListJoiner 58 from typing import List 59 60 user_message = [ 61 ChatMessage.from_user("Give a brief answer the following question: {{query}}"), 62 ] 63 64 feedback_prompt = """ 65 You are given a question and an answer. 66 Your task is to provide a score and a brief feedback on the answer. 67 Question: {{query}} 68 Answer: {{response}} 69 """ 70 feedback_message = [ChatMessage.from_system(feedback_prompt)] 71 72 prompt_builder = ChatPromptBuilder(template=user_message) 73 feedback_prompt_builder = ChatPromptBuilder(template=feedback_message) 74 llm = OpenAIChatGenerator(model="gpt-4o-mini") 75 feedback_llm = OpenAIChatGenerator(model="gpt-4o-mini") 76 77 pipe = Pipeline() 78 pipe.add_component("prompt_builder", prompt_builder) 79 pipe.add_component("llm", llm) 80 pipe.add_component("feedback_prompt_builder", feedback_prompt_builder) 81 pipe.add_component("feedback_llm", feedback_llm) 82 pipe.add_component("list_joiner", ListJoiner(List[ChatMessage])) 83 84 pipe.connect("prompt_builder.prompt", "llm.messages") 85 pipe.connect("prompt_builder.prompt", "list_joiner") 86 pipe.connect("llm.replies", "list_joiner") 87 pipe.connect("llm.replies", "feedback_prompt_builder.response") 88 pipe.connect("feedback_prompt_builder.prompt", "feedback_llm.messages") 89 pipe.connect("feedback_llm.replies", "list_joiner") 90 91 query = "What is nuclear physics?" 92 ans = pipe.run( 93 data={ 94 "prompt_builder": {"template_variables": {"query": query}}, 95 "feedback_prompt_builder": {"template_variables": {"query": query}}, 96 }, 97 ) 98 99 print(ans["list_joiner"]["values"]) 100 ```