{"record":{"id":"95010caeb8224f1a","repo":"huggingface/smolagents","slug":"error-during-jinja-template-rendering-type-e","errorCode":null,"errorMessage":"Error during jinja template rendering: {type(e).__name__}: {e}","messagePattern":"Error during jinja template rendering: (.+?): (.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"src/smolagents/agents.py","lineNumber":107,"sourceCode":"    AgentToolExecutionError,\n    create_agent_gradio_app_template,\n    extract_code_from_text,\n    is_valid_name,\n    make_init_file,\n    parse_code_blobs,\n    truncate_content,\n)\n\n\nlogger = getLogger(__name__)\n\n\ndef populate_template(template: str, variables: dict[str, Any]) -> str:\n    compiled_template = Template(template, undefined=StrictUndefined)\n    try:\n        return compiled_template.render(**variables)\n    except Exception as e:\n        raise Exception(f\"Error during jinja template rendering: {type(e).__name__}: {e}\")\n\n\n@dataclass\nclass ActionOutput:\n    output: Any\n    is_final_answer: bool\n\n\n@dataclass\nclass ToolOutput:\n    id: str\n    output: Any\n    is_final_answer: bool\n    observation: str\n    tool_call: ToolCall\n\n\nclass PlanningPromptTemplate(TypedDict):","sourceCodeStart":89,"sourceCodeEnd":125,"githubUrl":"https://github.com/huggingface/smolagents/blob/30bb1161095dbae2271e6bc3cc4c219cc3897a57/src/smolagents/agents.py#L89-L125","documentation":"This error is raised when a Jinja2 prompt template fails to render. populate_template compiles the template with StrictUndefined, so any variable referenced in the template that is not supplied in the variables dict (or any malformed Jinja syntax) makes render() raise, and the exception is re-wrapped with its type name and message.","triggerScenarios":"Calling initialize_system_prompt(), run(), provide_final_answer(), or _generate_planning_step() on an agent whose prompt_templates reference a variable (e.g. {{ authorized_imports }}, {{ tool_descriptions }}) that isn't provided; or passing a custom prompt_templates dict containing invalid Jinja2 syntax or a typo'd variable name.","commonSituations":"Customizing ManagedAgent or CodeAgent system prompts with a custom template that uses an undefined variable; upgrading smolagents versions where template variables were renamed/added; passing a tool whose description contains raw Jinja delimiters like {{ }}.","solutions":["Inspect the exception message: it embeds the original Jinja2 error (e.g. UndefinedError: 'authorized_imports' is undefined), which names the missing variable","Ensure every {{ var }} in your custom prompt_templates is populated; either add the variable to the template's variables or remove it from the template","If your template should tolerate missing values, replace StrictUndefined behavior by using {{ var | default('') }} in the template","Check that dynamic strings (tool descriptions, names) injected into templates are escaped or free of stray {{ }} tokens"],"exampleFix":"# before\nprompt_templates={\"system_prompt\": \"Tools: {{ tools_list }}\"}  # 'tools_list' undefined -> StrictUndefined error\n\n# after\nprompt_templates={\"system_prompt\": \"Tools: {{ tool_descriptions | default('') }}\"}","handlingStrategy":"validation","validationCode":"from jinja2 import Environment, StrictUndefined\nenv = Environment(undefined=StrictUndefined)\n# dry-run your custom template with the variables smolagents provides\ntry:\n    env.from_string(my_template).render(tool_descriptions=\"...\", managed_agents=\"...\", authorized_imports=\"...\")\nexcept Exception as e:\n    print(\"template will fail:\", e)","typeGuard":null,"tryCatchPattern":"try:\n    agent.run(task)\nexcept Exception as e:\n    if \"jinja template rendering\" in str(e):\n        # inspect inner message for the undefined variable / syntax error\n        log.error(e)","preventionTips":["Never reference template variables not documented in the default prompt templates","Prefer copying the bundled prompt template and editing minimally","Escape user/tool-provided text that may contain {{ }} before it reaches templates"],"tags":["jinja2","prompt-template","smolagents","undefined-variable"],"backgroundTag":"jinja2-undefined-variable","analyzedSha":"30bb1161095dbae2271e6bc3cc4c219cc3897a57","analyzedAt":"2026-08-28T18:52:54.169Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}