{"record":{"id":"335a95cc74318206","repo":"langchain-ai/langchain","slug":"got-mismatched-input-variables-expected-input-v","errorCode":null,"errorMessage":"Got mismatched input_variables. Expected: {input_vars}. Got: {values['input_variables']}","messagePattern":"Got mismatched input_variables\\. Expected: (.+?)\\. Got: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/prompts/chat.py","lineNumber":1097,"sourceCode":"                    message.optional\n                    and message.variable_name not in values[\"partial_variables\"]\n                ):\n                    values[\"partial_variables\"][message.variable_name] = []\n                    optional_variables.add(message.variable_name)\n                if message.variable_name not in input_types:\n                    input_types[message.variable_name] = list[AnyMessage]\n        if \"partial_variables\" in values:\n            input_vars -= set(values[\"partial_variables\"])\n        if optional_variables:\n            input_vars -= optional_variables\n        if \"input_variables\" in values and values.get(\"validate_template\"):\n            if input_vars != set(values[\"input_variables\"]):\n                msg = (\n                    \"Got mismatched input_variables. \"\n                    f\"Expected: {input_vars}. \"\n                    f\"Got: {values['input_variables']}\"\n                )\n                raise ValueError(msg)\n        else:\n            values[\"input_variables\"] = sorted(input_vars)\n        if optional_variables:\n            values[\"optional_variables\"] = sorted(optional_variables)\n        values[\"input_types\"] = input_types\n        return values\n\n    @classmethod\n    def from_template(cls, template: str, **kwargs: Any) -> ChatPromptTemplate:\n        \"\"\"Create a chat prompt template from a template string.\n\n        Creates a chat template consisting of a single message assumed to be from the\n        human.\n\n        Args:\n            template: Template string\n            **kwargs: Keyword arguments to pass to the constructor.\n","sourceCodeStart":1079,"sourceCodeEnd":1115,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/prompts/chat.py#L1079-L1115","documentation":"ChatPromptTemplate's model validator computes input variables as the union across all message templates, minus partial and optional variables. If 'input_variables' was supplied explicitly AND values['validate_template'] is truthy, and the computed set differs from the supplied one, ValueError('Got mismatched input_variables...') is raised listing both sets. Without validate_template, the computed list silently overwrites the supplied one.","triggerScenarios":"Constructing ChatPromptTemplate(input_variables=['a','b'], messages=[...]) with validate_template=True where the templates actually reference {'a','c'}; typical when hand-maintained input_variables lists drift from template edits.","commonSituations":"Copy-pasted constructors where the template string changed but the explicit input_variables list did not; migration from legacy ChatPromptTemplate(prompt=...) kwargs that required manual variable lists.","solutions":["Update the supplied input_variables to exactly match the variables used across message templates (partial/optional excluded)","Or omit input_variables entirely and let the validator derive them","Only set validate_template=True when you genuinely want strict cross-checking"],"exampleFix":"# before\nChatPromptTemplate(\n    input_variables=[\"topic\", \"style\"],\n    messages=[\"human: Tell me about {topic}\"],\n    validate_template=True,\n)  # ValueError: expected {'topic'}\n\n# after\nChatPromptTemplate(\n    input_variables=[\"topic\"],\n    messages=[\"human: Tell me about {topic}\"],\n    validate_template=True,\n)","handlingStrategy":"validation","validationCode":"def derive_input_vars(messages) -> set[str]:\n    vars_ = set()\n    for m in messages:\n        vars_ |= set(getattr(m, \"input_variables\", []))\n    return vars_\n\n# before constructing with validate_template=True:\nassert set(declared) == derive_input_vars(messages)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Omit input_variables and let the validator derive them","When editing template strings, update (or drop) explicit input_variables lists in the same change"],"tags":["prompts","chat","validation","input-variables","core"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}