deepset-ai/haystack · error · ValueError

Invalid template for output: {output}

Error message

Invalid template for output: {output}

What it means

This is the fallback ValueError raised in ConditionalRouter._validate_routes when the route 'output' is a string but still fails Jinja2 template validation (invalid Jinja syntax), while output_passthrough is False.

Source

Thrown at haystack/components/routers/conditional_router.py:532

                    raise ValueError(
                        f"Invalid template for condition: {condition_value!r} (type: {type(condition_value).__name__})."
                        f"Condition must be a string representing a valid Jinja2 template. "
                        f"For example, use {str(condition_value)!r} instead of {condition_value!r}."
                    )
                raise ValueError(f"Invalid template for condition: {condition_value}")

            # Only validate output as Jinja2 template when output_passthrough is False (default)
            output_passthrough = route.get("output_passthrough", False)
            if not output_passthrough:
                for output in outputs:
                    if not self._validate_template(self._env, output):
                        if not isinstance(output, str):
                            raise ValueError(
                                f"Invalid template for output: {output!r} (type: {type(output).__name__}). "
                                f"Output must be a string representing a valid Jinja2 template. "
                                f"For example, use {str(output)!r} instead of {output!r}."
                            )
                        raise ValueError(f"Invalid template for output: {output}")

    @staticmethod
    def _extract_variables(env: Environment, templates: list[str]) -> set[str]:
        """
        Extracts all variables from a list of Jinja template strings.

        :param env: A Jinja environment.
        :param templates: A list of Jinja template strings.
        :returns: A set of variable names.
        """
        variables = set()
        for template in templates:
            assigned_variables, template_variables = _extract_template_variables_and_assignments(
                env=env, template=template
            )
            variables.update(template_variables - assigned_variables)
        return variables

View on GitHub (pinned to e318778c9b)

Solutions

  1. Fix the Jinja2 syntax in the output template (close all {{ }} and {% %} blocks)
  2. Validate the template with jinja2.Environment().parse(output) locally to see the syntax error
  3. Ensure variables used are allowed; variable existence is not validated, only syntax

Example fix

# before
{"condition": "{{ ok }}", "output": "{{ result ", "output_name": "out", "output_type": str}
# after
{"condition": "{{ ok }}", "output": "{{ result }}", "output_name": "out", "output_type": str}
Defensive patterns

Strategy: validation

Validate before calling

import jinja2
env = jinja2.Environment()
for r in routes:
    if not r.get("output_passthrough", False):
        env.parse(r["output"])  # raises TemplateSyntaxError on bad syntax

Try / catch

try:
    router = ConditionalRouter(routes=routes)
except ValueError as e:
    if "Invalid template for output" in str(e):
        logging.error("Fix the Jinja2 syntax of the failing route output: %s", e)
        raise

Prevention

When it happens

Trigger: A string 'output' containing malformed Jinja2 syntax, e.g. '{{ x ' (unclosed braces) or '{% if %}' without end tag.

Common situations: Hand-written templates with typos; templates built by concatenation that produce unbalanced {{ }} or {% %} blocks.

Related errors


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