langchain-ai/langchain · error · ValueError
Got input_variables={input_variables}, but based on prefix/s
Error message
Got input_variables={input_variables}, but based on prefix/suffix expected {expected_input_variables} What it means
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.
Source
Thrown at libs/core/langchain_core/prompts/few_shot_with_templates.py:95
return values
@model_validator(mode="after")
def template_is_valid(self) -> Self:
"""Check that prefix, suffix, and input variables are consistent."""
if self.validate_template:
input_variables = self.input_variables
expected_input_variables = set(self.suffix.input_variables)
expected_input_variables |= set(self.partial_variables)
if self.prefix is not None:
expected_input_variables |= set(self.prefix.input_variables)
missing_vars = expected_input_variables.difference(input_variables)
if missing_vars:
msg = (
f"Got input_variables={input_variables}, but based on "
f"prefix/suffix expected {expected_input_variables}"
)
raise ValueError(msg)
else:
self.input_variables = sorted(
set(self.suffix.input_variables)
| set(self.prefix.input_variables if self.prefix else [])
- set(self.partial_variables)
)
return self
model_config = ConfigDict(
arbitrary_types_allowed=True,
extra="forbid",
)
def _get_examples(self, **kwargs: Any) -> list[dict[str, Any]]:
if self.examples is not None:
return self.examples
if self.example_selector is not None:
return self.example_selector.select_examples(kwargs)View on GitHub (pinned to e32fa9a52e)
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.
Example fix
# before
FewShotPromptWithTemplates(
...,
input_variables=["question"],
suffix=PromptTemplate(template="{question} {style}", input_variables=["question"]),
validate_template=True,
)
# after
FewShotPromptWithTemplates(
...,
input_variables=["question", "style"],
suffix=PromptTemplate(template="{question} {style}", input_variables=["question", "style"]),
validate_template=True,
) Defensive patterns
Strategy: validation
Validate before calling
def check_input_vars(suffix, prefix, input_variables, partial_variables):
expected = set(suffix.input_variables) | set(partial_variables)
if prefix is not None:
expected |= set(prefix.input_variables)
return expected <= set(input_variables) Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Only one of 'examples' and 'example_selector' should be prov
- One of 'examples' and 'example_selector' should be provided
- Only one of 'examples' and 'example_selector' should be prov
- One of 'examples' and 'example_selector' should be provided
- Got mismatched input_variables. Expected: {input_vars}. Got:
AI-assisted analysis of langchain-ai/langchain@e32fa9a52e (2026-08-14).
Data as JSON: /api/errors/9ff22fb66fa0d4ee.
Report an issue: GitHub.