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

OutputAdapter

This component helps the output of one component fit smoothly into the input of another. It uses Jinja expressions to define how this adaptation occurs.

NameOutputAdapter
Folder Path/converters/
Position in a Pipelineflexible
Inputs“**kwargs”: input variables to be used in Jinja expression
Outputsthe output is specified under “output” key dictionary

Overview

To use OutputAdapter, you need to specify the adaptation rule that includes:

  • template: A Jinja template string that defines how to adapt the input data.
  • output_type: The type of the output data (such as str, List[int]..).
  • custom_filters: An optional dictionary of custom Jinja filters to be used in the template.

Usage

On its own

This component is primarily meant to be used in Pipelines.

In this example, OutputAdapter simply outputs the content field of the first Document in the arrays of Documents:

from haystack import Document
from haystack.components.converters import OutputAdapter

adapter = OutputAdapter(template="{{ documents[0].content }}", output_type=str)
input_data = {"documents": [Document(content="Test content")]}
expected_output = {"output": "Test content"}
assert adapter.run(**input_data) == expected_output

In a Pipeline

The example below demonstrates a straightforward Pipeline that uses the OutputAdapter to capitalize the first Document in the list. If needed, you can also utilize the predefined Jinja filters.

from haystack import Pipeline, component, Document
from haystack.components.converters import OutputAdapter

@component
class DocumentProducer:
    @component.output_types(documents=dict)
    def run(self):
        return {"documents": [Document(content="haystack")]}

pipe = Pipeline()
pipe.add_component(
    name="output_adapter",
    instance=OutputAdapter(template="{{ documents[0].content | capitalize}}", output_type=str),
)
pipe.add_component(name="document_producer", instance=DocumentProducer())
pipe.connect("document_producer", "output_adapter")
result = pipe.run(data={})

assert result["output_adapter"]["output"] == "Haystack"

You can also define your own custom filters, which can then be added to an OutputAdapter instance through its init method and used in templates. Here’s an example of this approach:


from haystack import Pipeline, component, Document
from haystack.components.converters import OutputAdapter

def reverse_string(s):
    return s[::-1]

@component
class DocumentProducer:
    @component.output_types(documents=dict)
    def run(self):
        return {"documents": [Document(content="haystack")]}

pipe = Pipeline()
pipe.add_component(
    name="output_adapter",
    instance=OutputAdapter(template="{{ documents[0].content | reverse_string}}",
                           output_type=str,
                           custom_filters={"reverse_string": reverse_string}))

pipe.add_component(name="document_producer", instance=DocumentProducer())
pipe.connect("document_producer", "output_adapter")
result = pipe.run(data={})

assert result["output_adapter"]["output"] == "kcatsyah"

Related Links

See the parameters details in our API reference: