Coverage for haystack/components/joiners/string_joiner.py: 100%
8 statements
« prev ^ index » next coverage.py v7.16.1, created at 2026-10-09 14:34 +0000
« prev ^ index » next coverage.py v7.16.1, created at 2026-10-09 14:34 +0000
1# SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
2#
3# SPDX-License-Identifier: Apache-2.0
6from haystack import component
7from haystack.core.component.types import Variadic
10@component
11class StringJoiner:
12 """
13 Component to join strings from different components to a list of strings.
15 ### Usage example
17 ```python
18 from haystack.components.joiners import StringJoiner
19 from haystack.components.builders import PromptBuilder
20 from haystack.core.pipeline import Pipeline
22 string_1 = "What's Natural Language Processing?"
23 string_2 = "What is life?"
25 pipeline = Pipeline()
26 pipeline.add_component("prompt_builder_1", PromptBuilder("Builder 1: {{query}}"))
27 pipeline.add_component("prompt_builder_2", PromptBuilder("Builder 2: {{query}}"))
28 pipeline.add_component("string_joiner", StringJoiner())
30 pipeline.connect("prompt_builder_1.prompt", "string_joiner.strings")
31 pipeline.connect("prompt_builder_2.prompt", "string_joiner.strings")
33 print(pipeline.run(data={"prompt_builder_1": {"query": string_1}, "prompt_builder_2": {"query": string_2}}))
35 # >> {"string_joiner": {"strings": ["Builder 1: What's Natural Language Processing?", "Builder 2: What is life?"]}}
36 ```
37 """
39 @component.output_types(strings=list[str])
40 def run(self, strings: Variadic[str]) -> dict[str, list[str]]:
41 """
42 Joins strings into a list of strings
44 :param strings:
45 strings from different components
47 :returns:
48 A dictionary with the following keys:
49 - `strings`: Merged list of strings
50 """
52 out_strings = list(strings)
53 return {"strings": out_strings}