{"record":{"id":"9ff22fb66fa0d4ee","repo":"langchain-ai/langchain","slug":"got-input-variables-input-variables-but-based-o","errorCode":null,"errorMessage":"Got input_variables={input_variables}, but based on prefix/suffix expected {expected_input_variables}","messagePattern":"Got input_variables=(.+?), but based on prefix/suffix expected (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/prompts/few_shot_with_templates.py","lineNumber":95,"sourceCode":"\n        return values\n\n    @model_validator(mode=\"after\")\n    def template_is_valid(self) -> Self:\n        \"\"\"Check that prefix, suffix, and input variables are consistent.\"\"\"\n        if self.validate_template:\n            input_variables = self.input_variables\n            expected_input_variables = set(self.suffix.input_variables)\n            expected_input_variables |= set(self.partial_variables)\n            if self.prefix is not None:\n                expected_input_variables |= set(self.prefix.input_variables)\n            missing_vars = expected_input_variables.difference(input_variables)\n            if missing_vars:\n                msg = (\n                    f\"Got input_variables={input_variables}, but based on \"\n                    f\"prefix/suffix expected {expected_input_variables}\"\n                )\n                raise ValueError(msg)\n        else:\n            self.input_variables = sorted(\n                set(self.suffix.input_variables)\n                | set(self.prefix.input_variables if self.prefix else [])\n                - set(self.partial_variables)\n            )\n        return self\n\n    model_config = ConfigDict(\n        arbitrary_types_allowed=True,\n        extra=\"forbid\",\n    )\n\n    def _get_examples(self, **kwargs: Any) -> list[dict[str, Any]]:\n        if self.examples is not None:\n            return self.examples\n        if self.example_selector is not None:\n            return self.example_selector.select_examples(kwargs)","sourceCodeStart":77,"sourceCodeEnd":113,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/prompts/few_shot_with_templates.py#L77-L113","documentation":"Pydantic after-validator on FewShotPromptWithTemplates (only when validate_template=True) raised when the declared `input_variables` do not cover all variables referenced by the suffix (and prefix if set) after accounting for `partial_variables`. missing_vars are the variables the sub-templates need that input_variables lacks.","triggerScenarios":"Constructing with validate_template=True, suffix=PromptTemplate(template=\"{question} {style}\"), but input_variables=[\"question\"] (missing 'style') and no partial_variables covering 'style'. The prefix's variables are unioned in too.","commonSituations":"Hand-maintained input_variables lists drifting from edited template strings; adding a variable to the suffix text during iteration without updating the list; setting partial_variables after copy() so the validator's accounting no longer matches.","solutions":["Add the missing variable(s) to input_variables.","Or add the variable to partial_variables if you intend to fill it once: partial_variables={\"style\": \"concise\"}.","With validate_template=False the class auto-computes input_variables from suffix/prefix, so dropping the manual list also avoids drift."],"exampleFix":"# before\nFewShotPromptWithTemplates(\n    ...,\n    input_variables=[\"question\"],\n    suffix=PromptTemplate(template=\"{question} {style}\", input_variables=[\"question\"]),\n    validate_template=True,\n)\n\n# after\nFewShotPromptWithTemplates(\n    ...,\n    input_variables=[\"question\", \"style\"],\n    suffix=PromptTemplate(template=\"{question} {style}\", input_variables=[\"question\", \"style\"]),\n    validate_template=True,\n)","handlingStrategy":"validation","validationCode":"def check_input_vars(suffix, prefix, input_variables, partial_variables):\n    expected = set(suffix.input_variables) | set(partial_variables)\n    if prefix is not None:\n        expected |= set(prefix.input_variables)\n    return expected <= set(input_variables)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Prefer validate_template=False and let input_variables be auto-derived from the templates.","Regenerate input_variables from template.get_variables() after every template text edit.","Cover template construction with a unit test that formats with all declared inputs."],"tags":["prompts","few-shot","input-variables","pydantic-validation"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}