langchain-ai/langchain · error · ValueError

Input variables must be provided to validate the template.

Error message

Input variables must be provided to validate the template.

What it means

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`.

Source

Thrown at libs/core/langchain_core/prompts/prompt.py:109

    def pre_init_validation(cls, values: dict[str, Any]) -> Any:
        """Check that template and input variables are consistent."""
        if values.get("template") is None:
            # Will let pydantic fail with a ValidationError if template
            # is not provided.
            return values

        # Set some default values based on the field defaults
        values.setdefault("template_format", "f-string")
        values.setdefault("partial_variables", {})

        if values.get("validate_template"):
            if values["template_format"] == "mustache":
                msg = "Mustache templates cannot be validated."
                raise ValueError(msg)

            if "input_variables" not in values:
                msg = "Input variables must be provided to validate the template."
                raise ValueError(msg)

            all_inputs = values["input_variables"] + list(values["partial_variables"])
            check_valid_template(
                values["template"], values["template_format"], all_inputs
            )

        if values["template_format"]:
            values["input_variables"] = [
                var
                for var in get_template_variables(
                    values["template"], values["template_format"]
                )
                if var not in values["partial_variables"]
            ]

        return values

    @override

View on GitHub (pinned to e32fa9a52e)

Solutions

  1. Pass the variables explicitly: `PromptTemplate(template=..., input_variables=['name'], validate_template=True)`.
  2. Use `PromptTemplate.from_template('Hi {name}')`, which infers variables and skips this code path (validation defaults off).
  3. Drop `validate_template=True` and rely on `format`-time errors, or pre-validate with `get_template_variables(template, 'f-string')` yourself.

Example fix

# before
PromptTemplate(
    template='Hi {name}',
    validate_template=True,  # ValueError: no input_variables
)

# after
PromptTemplate(
    template='Hi {name}',
    input_variables=['name'],
    validate_template=True,
)
Defensive patterns

Strategy: validation

Validate before calling

from langchain_core.prompts.string import get_template_variables

inferred = get_template_variables(template, 'f-string')
if 'input_variables' not in kwargs:
    kwargs['input_variables'] = inferred
PromptTemplate(template=template, validate_template=True, **kwargs)

Type guard

def has_explicit_input_variables(kwargs: dict) -> bool:
    return 'input_variables' in kwargs

Prevention

When it happens

Trigger: `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.

Common situations: 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.

Related errors


AI-assisted analysis of langchain-ai/langchain@e32fa9a52e (2026-08-14). Data as JSON: /api/errors/81671e996ba44048. Report an issue: GitHub.