{"record":{"id":"3208110f01786c8d","repo":"langchain-ai/langchain","slug":"mustache-templates-cannot-be-validated","errorCode":null,"errorMessage":"Mustache templates cannot be validated.","messagePattern":"Mustache templates cannot be validated\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/prompts/prompt.py","lineNumber":105,"sourceCode":"    \"\"\"Whether or not to try validating the template.\"\"\"\n\n    @model_validator(mode=\"before\")\n    @classmethod\n    def pre_init_validation(cls, values: dict[str, Any]) -> Any:\n        \"\"\"Check that template and input variables are consistent.\"\"\"\n        if values.get(\"template\") is None:\n            # Will let pydantic fail with a ValidationError if template\n            # is not provided.\n            return values\n\n        # Set some default values based on the field defaults\n        values.setdefault(\"template_format\", \"f-string\")\n        values.setdefault(\"partial_variables\", {})\n\n        if values.get(\"validate_template\"):\n            if values[\"template_format\"] == \"mustache\":\n                msg = \"Mustache templates cannot be validated.\"\n                raise ValueError(msg)\n\n            if \"input_variables\" not in values:\n                msg = \"Input variables must be provided to validate the template.\"\n                raise ValueError(msg)\n\n            all_inputs = values[\"input_variables\"] + list(values[\"partial_variables\"])\n            check_valid_template(\n                values[\"template\"], values[\"template_format\"], all_inputs\n            )\n\n        if values[\"template_format\"]:\n            values[\"input_variables\"] = [\n                var\n                for var in get_template_variables(\n                    values[\"template\"], values[\"template_format\"]\n                )\n                if var not in values[\"partial_variables\"]\n            ]","sourceCodeStart":87,"sourceCodeEnd":123,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/prompts/prompt.py#L87-L123","documentation":"In `PromptTemplate`'s pydantic validator, `validate_template=True` triggers template validation against the declared `input_variables`. Mustache has no static checker in langchain-core (its variable syntax `{{var}}` with sections/tags makes offline validation unreliable), so `validate_template=True` combined with `template_format='mustache'` raises this `ValueError` at construction time.","triggerScenarios":"`PromptTemplate(template=..., template_format='mustache', validate_template=True)` — explicitly or via a subclass/config that sets `validate_template`. Note `validate_template` defaults to `False`, so this only fires when opted in.","commonSituations":"Porting code that set `validate_template=True` for f-string safety onto a mustache template; copy-pasting constructor flags across templates of different formats; older LangChain versions where this combination behaved differently.","solutions":["Set `validate_template=False` (or omit it) for mustache templates: `PromptTemplate(..., template_format='mustache')`.","Switch the template to `f-string` format if you want validation guarantees.","For mustache, validate inputs manually before `format(**vars)`."],"exampleFix":"# before\nPromptTemplate(\n    template='Hello {{name}}',\n    input_variables=['name'],\n    template_format='mustache',\n    validate_template=True,  # ValueError\n)\n\n# after\nPromptTemplate(\n    template='Hello {{name}}',\n    input_variables=['name'],\n    template_format='mustache',\n)","handlingStrategy":"validation","validationCode":"if template_format == 'mustache':\n    validate_template = False  # mustache cannot be validated\nPromptTemplate(template=t, input_variables=vars_,\n               template_format=template_format, validate_template=validate_template)","typeGuard":"def can_validate_template(fmt: str) -> bool:\n    return fmt != 'mustache'","tryCatchPattern":null,"preventionTips":["Only enable validate_template for f-string (and jinja2) formats.","For mustache, write your own pre-format check comparing get_template_variables output against supplied keys."],"tags":["prompts","mustache","validation","valueerror"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}