deepset-ai/haystack · error · OutputAdaptationException

Error adapting {self.template} with {kwargs}: {e}

Error message

Error adapting {self.template} with {kwargs}: {e}

What it means

OutputAdapter.run wraps template rendering in a try/except and re-raises any failure (Jinja runtime errors, ast.literal_eval failures on non-literal outputs, type errors) as OutputAdaptationException including the template, kwargs, and original exception message.

Source

Thrown at haystack/components/converters/output_adapter.py:145

            self._env.filters[name] = filter_func
        adapted_outputs = {}
        try:
            adapted_output_template = self._env.from_string(self.template)
            output_result = adapted_output_template.render(**kwargs)
            if isinstance(output_result, jinja2.runtime.Undefined):
                raise OutputAdaptationException(f"Undefined variable in the template {self.template}; kwargs: {kwargs}")  # noqa: TRY301

            # We suppress the exception in case the output is already a string, otherwise
            # we try to evaluate it and would fail.
            # This must be done cause the output could be different literal structures.
            # This doesn't support any user types.
            with contextlib.suppress(Exception):
                if not self._unsafe:
                    output_result = ast.literal_eval(output_result)

            adapted_outputs["output"] = output_result
        except Exception as e:
            raise OutputAdaptationException(f"Error adapting {self.template} with {kwargs}: {e}") from e
        return adapted_outputs

    def to_dict(self) -> dict[str, Any]:
        """
        Serializes the component to a dictionary.

        :returns:
            Dictionary with serialized data.
        """
        se_filters = {name: serialize_callable(filter_func) for name, filter_func in self.custom_filters.items()}
        return default_to_dict(
            self,
            template=self.template,
            output_type=serialize_type(self.output_type),
            custom_filters=se_filters,
            unsafe=self._unsafe,
        )

View on GitHub (pinned to e318778c9b)

Solutions

  1. Read the chained exception message to find the underlying render/eval failure
  2. Fix the template expression or supply correctly-typed inputs
  3. Register any custom filter used by the template via custom_filters
  4. Set unsafe=True if you intentionally need non-literal evaluation (trust the template source)

Example fix

// before
{{ count / total }}  # total is a string -> render error
// after
{{ count / (total | int) }}
Defensive patterns

Strategy: try-catch

Try / catch

try:
    out = adapter.run(**kwargs)
except OutputAdaptationException as e:
    log.error("OutputAdapter failed: %s", e)
    raise  # inspect chained cause for the root render/eval error

Prevention

When it happens

Trigger: Rendering raises any Exception: calling methods/filters on undefined or wrong-typed values, division errors, filters not registered, or the rendered string failing ast.literal_eval when _unsafe=False and output is not a literal.

Common situations: Templates doing arithmetic on strings; custom filters omitted from custom_filters; output being a plain sentence that literal_eval cannot parse.

Related errors


AI-assisted analysis of deepset-ai/haystack@e318778c9b (2026-08-30). Data as JSON: /api/errors/bd9c8e6be90e4857. Report an issue: GitHub.