outputadapter.mdx
  1  ---
  2  title: "OutputAdapter"
  3  id: outputadapter
  4  slug: "/outputadapter"
  5  description: "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."
  6  ---
  7  
  8  # OutputAdapter
  9  
 10  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.
 11  
 12  <div className="key-value-table">
 13  
 14  |  |  |
 15  | --- | --- |
 16  | **Most common position in a pipeline** | Flexible |
 17  | **Mandatory init variables** | `template`: A Jinja template string that defines how to adapt the data  <br /> <br />`output_type`: Type alias that this instance will return |
 18  | **Mandatory run variables** | `**kwargs`: Input variables to be used in Jinja expression. See [Variables](#variables)  section for more details. |
 19  | **Output variables** | The output is specified under the `output` key dictionary |
 20  | **API reference** | [Converters](/reference/converters-api) |
 21  | **GitHub link** | https://github.com/deepset-ai/haystack/blob/main/haystack/components/converters/output_adapter.py |
 22  
 23  </div>
 24  
 25  ## Overview
 26  
 27  To use `OutputAdapter`, you need to specify the adaptation rule that includes:
 28  
 29  - `template`: A Jinja template string that defines how to adapt the input data.
 30  - `output_type`: The type of the output data (such as `str`, `List[int]`..). This doesn't change the actual output type and is only needed to validate connection with other components.
 31  - `custom_filters`: An optional dictionary of custom Jinja filters to be used in the template.
 32  
 33  ### Variables
 34  
 35  The `OutputAdapter` requires all template variables to be present before running and raises an error if any template variable is missing at pipeline connect time.
 36  
 37  ```python
 38  from haystack.components.converters import OutputAdapter
 39  
 40  adapter = OutputAdapter(template="Hello {{name}}!", output_type=str)
 41  ```
 42  
 43  ### Unsafe behavior
 44  
 45  The `OutputAdapter` internally renders the `template` using Jinja, and by default, this is safe behavior. However, it limits the output types to strings, bytes, numbers, tuples, lists, dicts, sets, booleans, `None`, and `Ellipsis` (`...`), as well as any combination of these structures.
 46  
 47  If you want to use other types such as `ChatMessage`, `Document`, or `Answer`, you must enable unsafe template rendering by setting the `unsafe` init argument to `True`.
 48  
 49  Be cautious, as enabling this can be unsafe and may lead to remote code execution if the `template` is a string customizable by the end user.
 50  
 51  ## Usage
 52  
 53  ### On its own
 54  
 55  This component is primarily meant to be used in pipelines.
 56  
 57  In this example, `OutputAdapter` simply outputs the content field of the first document in the arrays of documents:
 58  
 59  ```python
 60  from haystack import Document
 61  from haystack.components.converters import OutputAdapter
 62  
 63  adapter = OutputAdapter(template="{{ documents[0].content }}", output_type=str)
 64  input_data = {"documents": [Document(content="Test content")]}
 65  expected_output = {"output": "Test content"}
 66  assert adapter.run(**input_data) == expected_output
 67  ```
 68  
 69  ### In a pipeline
 70  
 71  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](https://jinja.palletsprojects.com/en/3.1.x/templates/#builtin-filters).
 72  
 73  ```python
 74  from haystack import Pipeline, component, Document
 75  from haystack.components.converters import OutputAdapter
 76  
 77  
 78  @component
 79  class DocumentProducer:
 80      @component.output_types(documents=dict)
 81      def run(self):
 82          return {"documents": [Document(content="haystack")]}
 83  
 84  
 85  pipe = Pipeline()
 86  pipe.add_component(
 87      name="output_adapter",
 88      instance=OutputAdapter(
 89          template="{{ documents[0].content | capitalize}}",
 90          output_type=str,
 91      ),
 92  )
 93  pipe.add_component(name="document_producer", instance=DocumentProducer())
 94  pipe.connect("document_producer", "output_adapter")
 95  result = pipe.run(data={})
 96  
 97  assert result["output_adapter"]["output"] == "Haystack"
 98  ```
 99  
100  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:
101  
102  ```python
103  
104  from haystack import Pipeline, component, Document
105  from haystack.components.converters import OutputAdapter
106  
107  
108  def reverse_string(s):
109      return s[::-1]
110  
111  
112  @component
113  class DocumentProducer:
114      @component.output_types(documents=dict)
115      def run(self):
116          return {"documents": [Document(content="haystack")]}
117  
118  
119  pipe = Pipeline()
120  pipe.add_component(
121      name="output_adapter",
122      instance=OutputAdapter(
123          template="{{ documents[0].content | reverse_string}}",
124          output_type=str,
125          custom_filters={"reverse_string": reverse_string},
126      ),
127  )
128  
129  pipe.add_component(name="document_producer", instance=DocumentProducer())
130  pipe.connect("document_producer", "output_adapter")
131  result = pipe.run(data={})
132  
133  assert result["output_adapter"]["output"] == "kcatsyah"
134  ```