{"record":{"id":"30459c1c63c404de","repo":"deepset-ai/haystack","slug":"invalid-jinja-template-template-e","errorCode":null,"errorMessage":"Invalid Jinja template '{template}': {e}","messagePattern":"Invalid Jinja template '(.+?)': (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"haystack/components/converters/output_adapter.py","lineNumber":94,"sourceCode":"        self._unsafe = unsafe\n\n        if self._unsafe:\n            msg = (\n                \"Unsafe mode is enabled. This allows execution of arbitrary code in the Jinja template. \"\n                \"Use this only if you trust the source of the template.\"\n            )\n            logger.warning(msg)\n        self._env = (\n            NativeEnvironment()\n            if self._unsafe\n            else HaystackSandboxedEnvironment(undefined=jinja2.runtime.StrictUndefined)\n        )\n\n        try:\n            self._env.parse(template)  # Validate template syntax\n            self.template = template\n        except TemplateSyntaxError as e:\n            raise ValueError(f\"Invalid Jinja template '{template}': {e}\") from e\n\n        for name, filter_func in self.custom_filters.items():\n            self._env.filters[name] = filter_func\n\n        # b) extract variables in the template\n        assigned_variables, template_variables = _extract_template_variables_and_assignments(\n            env=self._env, template=self.template\n        )\n        route_input_names = template_variables - assigned_variables\n        input_types.update(route_input_names)\n\n        # the env is not needed, discarded automatically\n        component.set_input_types(self, **dict.fromkeys(input_types, Any))\n        component.set_output_types(self, output=output_type)\n        self.output_type = output_type\n\n    def run(self, **kwargs: Any) -> dict[str, Any]:\n        \"\"\"","sourceCodeStart":76,"sourceCodeEnd":112,"githubUrl":"https://github.com/deepset-ai/haystack/blob/e318778c9bf60a1963e3b5f451359655dd696c30/haystack/components/converters/output_adapter.py#L76-L112","documentation":"OutputAdapter validates its Jinja template at construction time by parsing it with the Jinja environment. A TemplateSyntaxError (e.g. unclosed {% if %}, bad filter syntax) is re-raised as a ValueError including the template and the underlying error, so misconfiguration fails fast before the pipeline runs.","triggerScenarios":"OutputAdapter(template=\"...\") with syntactically invalid Jinja: unbalanced {% %}/{{ }}, unknown block endings like {% endfor %} without {% for %}, malformed expressions.","commonSituations":"Hand-written templates with typos; f-string style braces pasted into Jinja; dynamically built templates where string interpolation broke the syntax.","solutions":["Fix the Jinja syntax reported in the message (check unclosed tags/brackets)","Validate the template locally by parsing it with jinja2.Environment().parse(template)","If the template is built dynamically, escape literal { as {{ and } as }}"],"exampleFix":"// before\nOutputAdapter(template=\"{% if x %}{{ x }\")  # unclosed tags\n// after\nOutputAdapter(template=\"{% if x %}{{ x }}{% endif %}\")","handlingStrategy":"validation","validationCode":"import jinja2\ndef validate_template(template: str) -> bool:\n    try:\n        jinja2.Environment().parse(template)\n        return True\n    except jinja2.TemplateSyntaxError:\n        return False","typeGuard":null,"tryCatchPattern":"try:\n    adapter = OutputAdapter(template=t)\nexcept ValueError as e:\n    if \"Invalid Jinja template\" in str(e):\n        report_template_syntax_error(t)\n    else:\n        raise","preventionTips":["Lint templates with jinja2 before wiring them into pipelines","Escape literal braces {{ '{{' }} when generating templates dynamically","Keep templates in versioned files reviewed like code"],"tags":["python","haystack","jinja","template-syntax","output-adapter"],"backgroundTag":"template-syntax-error","analyzedSha":"e318778c9bf60a1963e3b5f451359655dd696c30","analyzedAt":"2026-08-30T11:45:20.711Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}