{"record":{"id":"c78a1f261472d7ed","repo":"langchain-ai/langchain","slug":"jinja2-is-unsafe-and-is-not-supported-for-template","errorCode":null,"errorMessage":"jinja2 is unsafe and is not supported for templates expressed as dicts. Please use 'f-string' or 'mustache' format.","messagePattern":"jinja2 is unsafe and is not supported for templates expressed as dicts\\. Please use 'f-string' or 'mustache' format\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/prompts/chat.py","lineNumber":523,"sourceCode":"                                    )\n                                )\n                        img_template_obj = ImagePromptTemplate(\n                            input_variables=input_variables,\n                            template=img_template,\n                            template_format=template_format,\n                        )\n                    else:\n                        msg = f\"Invalid image template: {tmpl}\"  # type: ignore[unreachable]\n                        raise ValueError(msg)\n                    prompt.append(img_template_obj)\n                elif isinstance(tmpl, dict):\n                    if template_format == \"jinja2\":\n                        msg = (\n                            \"jinja2 is unsafe and is not supported for templates \"\n                            \"expressed as dicts. Please use 'f-string' or 'mustache' \"\n                            \"format.\"\n                        )\n                        raise ValueError(msg)\n                    data_template_obj = DictPromptTemplate(\n                        template=cast(\"dict[str, Any]\", tmpl),\n                        template_format=template_format,\n                    )\n                    prompt.append(data_template_obj)\n                else:\n                    msg = f\"Invalid template: {tmpl}\"  # type: ignore[unreachable]\n                    raise ValueError(msg)\n            return cls(prompt=prompt, **kwargs)\n        msg = f\"Invalid template: {template}\"  # type: ignore[unreachable]\n        raise ValueError(msg)\n\n    @classmethod\n    def from_template_file(\n        cls: type[Self],\n        template_file: str | Path,\n        input_variables: list[str],\n        **kwargs: Any,","sourceCodeStart":505,"sourceCodeEnd":541,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/prompts/chat.py#L505-L541","documentation":"from_messages/from_template refuses dict-expressed message templates when template_format='jinja2', raising ValueError. LangChain renders dict templates (DictPromptTemplate) by f-string-style interpolation over the dict contents; doing that with jinja2 would evaluate arbitrary template code over user-influenced data, which is unsafe, so only 'f-string' and 'mustache' are allowed for dict templates.","triggerScenarios":"Calling ChatPromptTemplate.from_messages([...], template_format='jinja2') where any message element is a dict (e.g. {'type': 'text', 'text': ...}) rather than a plain string.","commonSituations":"Porting jinja2-based prompts into structured multimodal message lists; teams standardizing on jinja2 house-style then adding image/content-block dicts to the same template.","solutions":["Use template_format='f-string' or 'mustache' for prompts that include dict message templates","Keep jinja2 only for pure string templates and express structured messages via tuples/strings instead of raw dicts"],"exampleFix":"# before\nChatPromptTemplate.from_messages(\n    [(\"human\", [{\"type\": \"text\", \"text\": \"{{ q }}\"}])],\n    template_format=\"jinja2\",  # ValueError\n)\n\n# after\nChatPromptTemplate.from_messages(\n    [(\"human\", [{\"type\": \"text\", \"text\": \"{q}\"}])],\n    template_format=\"f-string\",\n)","handlingStrategy":"validation","validationCode":"def valid_combo(templates, template_format: str) -> bool:\n    if template_format == \"jinja2\" and any(isinstance(t, dict) for t in templates):\n        return False\n    return True","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Standardize multimodal (dict) templates on f-string or mustache format","Reserve jinja2 for pure string templates only"],"tags":["prompts","chat","jinja2","security","core"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}