langchain-ai/langchain · error · ValueError
Got mismatched input_variables. Expected: {input_vars}. Got:
Error message
Got mismatched input_variables. Expected: {input_vars}. Got: {values['input_variables']} What it means
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.
Source
Thrown at libs/core/langchain_core/prompts/chat.py:1097
message.optional
and message.variable_name not in values["partial_variables"]
):
values["partial_variables"][message.variable_name] = []
optional_variables.add(message.variable_name)
if message.variable_name not in input_types:
input_types[message.variable_name] = list[AnyMessage]
if "partial_variables" in values:
input_vars -= set(values["partial_variables"])
if optional_variables:
input_vars -= optional_variables
if "input_variables" in values and values.get("validate_template"):
if input_vars != set(values["input_variables"]):
msg = (
"Got mismatched input_variables. "
f"Expected: {input_vars}. "
f"Got: {values['input_variables']}"
)
raise ValueError(msg)
else:
values["input_variables"] = sorted(input_vars)
if optional_variables:
values["optional_variables"] = sorted(optional_variables)
values["input_types"] = input_types
return values
@classmethod
def from_template(cls, template: str, **kwargs: Any) -> ChatPromptTemplate:
"""Create a chat prompt template from a template string.
Creates a chat template consisting of a single message assumed to be from the
human.
Args:
template: Template string
**kwargs: Keyword arguments to pass to the constructor.
View on GitHub (pinned to e32fa9a52e)
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
Example fix
# before
ChatPromptTemplate(
input_variables=["topic", "style"],
messages=["human: Tell me about {topic}"],
validate_template=True,
) # ValueError: expected {'topic'}
# after
ChatPromptTemplate(
input_variables=["topic"],
messages=["human: Tell me about {topic}"],
validate_template=True,
) Defensive patterns
Strategy: validation
Validate before calling
def derive_input_vars(messages) -> set[str]:
vars_ = set()
for m in messages:
vars_ |= set(getattr(m, "input_variables", []))
return vars_
# before constructing with validate_template=True:
assert set(declared) == derive_input_vars(messages) Prevention
- Omit input_variables and let the validator derive them
- When editing template strings, update (or drop) explicit input_variables lists in the same change
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- variable {self.variable_name} should be a list of base messa
- Invalid template: {tmpl}
- Invalid template: {template}
- Unexpected input: {message_template}
- INVALID_PROMPT_INPUT
AI-assisted analysis of langchain-ai/langchain@e32fa9a52e (2026-08-14).
Data as JSON: /api/errors/335a95cc74318206.
Report an issue: GitHub.