{"record":{"id":"d877543bc6e737c8","repo":"deepset-ai/haystack","slug":"undefined-variable-in-the-template-self-template","errorCode":null,"errorMessage":"Undefined variable in the template {self.template}; kwargs: {kwargs}","messagePattern":"Undefined variable in the template (.+?); kwargs: (.+?)","errorType":"exception","errorClass":"OutputAdaptationException","httpStatus":null,"severity":"error","filePath":"haystack/components/converters/output_adapter.py","lineNumber":133,"sourceCode":"        :param kwargs:\n            Must contain all variables used in the `template` string.\n        :returns:\n            A dictionary with the following keys:\n            - `output`: Rendered Jinja template.\n\n        :raises OutputAdaptationException: If template rendering fails.\n        \"\"\"\n        # check if kwargs are empty\n        if not kwargs:\n            raise ValueError(\"No input data provided for output adaptation\")\n        for name, filter_func in self.custom_filters.items():\n            self._env.filters[name] = filter_func\n        adapted_outputs = {}\n        try:\n            adapted_output_template = self._env.from_string(self.template)\n            output_result = adapted_output_template.render(**kwargs)\n            if isinstance(output_result, jinja2.runtime.Undefined):\n                raise OutputAdaptationException(f\"Undefined variable in the template {self.template}; kwargs: {kwargs}\")  # noqa: TRY301\n\n            # We suppress the exception in case the output is already a string, otherwise\n            # we try to evaluate it and would fail.\n            # This must be done cause the output could be different literal structures.\n            # This doesn't support any user types.\n            with contextlib.suppress(Exception):\n                if not self._unsafe:\n                    output_result = ast.literal_eval(output_result)\n\n            adapted_outputs[\"output\"] = output_result\n        except Exception as e:\n            raise OutputAdaptationException(f\"Error adapting {self.template} with {kwargs}: {e}\") from e\n        return adapted_outputs\n\n    def to_dict(self) -> dict[str, Any]:\n        \"\"\"\n        Serializes the component to a dictionary.\n","sourceCodeStart":115,"sourceCodeEnd":151,"githubUrl":"https://github.com/deepset-ai/haystack/blob/e318778c9bf60a1963e3b5f451359655dd696c30/haystack/components/converters/output_adapter.py#L115-L151","documentation":"After rendering, if Jinja returns an jinja2.runtime.Undefined object (a template variable was never provided), run() raises OutputAdaptationException naming the template and the kwargs that were actually passed, making the missing-variable mismatch explicit.","triggerScenarios":"Template references a variable not supplied in run() kwargs, and the StrictUndefined/undefined result survives rendering — e.g. run(template uses {{ summary }} but kwargs only contain docs).","commonSituations":"Renaming an output key upstream without updating the template; typos in variable names; templates copied from another pipeline expecting different inputs.","solutions":["Pass the missing variable in run() kwargs","Correct the variable name in the template to match an available kwarg","Add a default in the template: {{ summary | default('') }}"],"exampleFix":"// before\nadapter.run(docs=docs)  # template uses {{ summary }}\n// after\nadapter.run(docs=docs, summary=summary)","handlingStrategy":"validation","validationCode":"import re\nVAR_RE = re.compile(r\"{{\\s*(\\w+)\\s*}\")\ndef missing_vars(template: str, kwargs: dict) -> set:\n    return {v for v in VAR_RE.findall(template) if v not in kwargs}","typeGuard":null,"tryCatchPattern":"try:\n    out = adapter.run(**kwargs)\nexcept OutputAdaptationException as e:\n    if \"Undefined variable\" in str(e):\n        provide_defaults_and_retry(kwargs)\n    else:\n        raise","preventionTips":["Keep template variable names in sync with upstream output keys","Use {{ var | default(...) }} for optional values","Add a pre-run check comparing template variables against kwargs"],"tags":["python","haystack","jinja","undefined-variable","output-adapter"],"backgroundTag":"undefined-template-variable","analyzedSha":"e318778c9bf60a1963e3b5f451359655dd696c30","analyzedAt":"2026-08-30T11:45:20.711Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}