{"record":{"id":"81671e996ba44048","repo":"langchain-ai/langchain","slug":"input-variables-must-be-provided-to-validate-the-t","errorCode":null,"errorMessage":"Input variables must be provided to validate the template.","messagePattern":"Input variables must be provided to validate the template\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/prompts/prompt.py","lineNumber":109,"sourceCode":"    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            ]\n\n        return values\n\n    @override","sourceCodeStart":91,"sourceCodeEnd":127,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/prompts/prompt.py#L91-L127","documentation":"When `validate_template=True`, `PromptTemplate`'s validator needs the declared `input_variables` list to check the template against (together with `partial_variables`). If `input_variables` was not passed at all (key absent, not merely empty), validation cannot proceed and raises this `ValueError`.","triggerScenarios":"`PromptTemplate(template='Hi {name}', validate_template=True)` with no `input_variables` argument. An explicitly empty list `[]` would instead fail inside `check_valid_template` with a missing-variable error; this message is specifically for the absent key.","commonSituations":"Relying on auto-inference of input variables (which happens later, after validation) while also enabling `validate_template`; upgrading old code where omitting `input_variables` plus validation used to pass.","solutions":["Pass the variables explicitly: `PromptTemplate(template=..., input_variables=['name'], validate_template=True)`.","Use `PromptTemplate.from_template('Hi {name}')`, which infers variables and skips this code path (validation defaults off).","Drop `validate_template=True` and rely on `format`-time errors, or pre-validate with `get_template_variables(template, 'f-string')` yourself."],"exampleFix":"# before\nPromptTemplate(\n    template='Hi {name}',\n    validate_template=True,  # ValueError: no input_variables\n)\n\n# after\nPromptTemplate(\n    template='Hi {name}',\n    input_variables=['name'],\n    validate_template=True,\n)","handlingStrategy":"validation","validationCode":"from langchain_core.prompts.string import get_template_variables\n\ninferred = get_template_variables(template, 'f-string')\nif 'input_variables' not in kwargs:\n    kwargs['input_variables'] = inferred\nPromptTemplate(template=template, validate_template=True, **kwargs)","typeGuard":"def has_explicit_input_variables(kwargs: dict) -> bool:\n    return 'input_variables' in kwargs","tryCatchPattern":null,"preventionTips":["Always pass input_variables explicitly when validate_template=True.","Or skip validation entirely and use PromptTemplate.from_template, which infers variables."],"tags":["prompts","validation","input-variables","valueerror"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}