DocumentationAPI Reference📓 Tutorials🧑‍🍳 Cookbook🤝 Integrations💜 Discord🎨 Studio
Documentation

StringJoiner

Component to join strings from different components into a list of strings.

Most common position in a pipelineAfter at least two other components to join their strings
Mandatory run variables“strings”: Multiple strings from connected components.
Output variables“strings”: A list of merged strings
API referenceJoiners
GitHub linkhttps://github.com/deepset-ai/haystack/blob/main/haystack/components/joiners/string_joiner.py

Overview

The StringJoiner component collects multiple string outputs from various pipeline components and combines them into a single list. This is useful when you need to merge several strings from different parts of a pipeline into a unified output.

Usage

from haystack import Pipeline
from haystack.utils import Secret
from haystack.components.joiners import StringJoiner
from haystack.components.builders.chat_prompt_builder import ChatPromptBuilder
from haystack.components.generators.chat import OpenAIChatGenerator
from haystack.dataclasses import ChatMessage

string_1 = "What's Natural Language Processing?"
string_2 = "What is life?"

pipeline = Pipeline()
pipeline.add_component("prompt_builder_1", ChatPromptBuilder(
    template=[ChatMessage.from_user("Builder 1: {{query}}")], required_variables={"query"}
))
pipeline.add_component("prompt_builder_2", ChatPromptBuilder(
    template=[ChatMessage.from_user("Builder 2: {{query}}")], required_variables={"query"}
))
pipeline.add_component("string_joiner", StringJoiner())

pipeline.connect("prompt_builder_1.messages", "string_joiner.strings")
pipeline.connect("prompt_builder_2.messages", "string_joiner.strings")

result = pipeline.run(data={
    "prompt_builder_1": {"query": string_1}, 
    "prompt_builder_2": {"query": string_2}
})

print(result)