langchain-ai/langchain · error · ValueError

Invalid prompt schema; check for mismatched or missing input

Error message

Invalid prompt schema; check for mismatched or missing input parameters from {input_variables}.

What it means

Raised by `check_valid_template` when the format-specific validator (e.g. a dry-run of the template with the declared `input_variables`) fails with `KeyError` or `IndexError`. Concretely: the template references a placeholder that is not in `input_variables` (KeyError), or uses positional/index-style fields not supplied (IndexError). The `ValueError` chains the original exception, and the message echoes the `input_variables` list that was checked.

Source

Thrown at libs/core/langchain_core/prompts/string.py:295

    """
    try:
        validator_func = DEFAULT_VALIDATOR_MAPPING[template_format]
    except KeyError as exc:
        msg = (
            f"Invalid template format {template_format!r}, should be one of"
            f" {list(DEFAULT_FORMATTER_MAPPING)}."
        )
        raise ValueError(msg) from exc
    if template_format == "f-string":
        validate_f_string_template(template)
    try:
        validator_func(template, input_variables)
    except (KeyError, IndexError) as exc:
        msg = (
            "Invalid prompt schema; check for mismatched or missing input parameters"
            f" from {input_variables}."
        )
        raise ValueError(msg) from exc


def get_template_variables(template: str, template_format: str) -> list[str]:
    """Get the variables from the template.

    Args:
        template: The template string.
        template_format: The template format.

            Should be one of `'f-string'`, `'mustache'` or `'jinja2'`.

    Returns:
        The variables from the template.

    Raises:
        ValueError: If the template format is not supported.
    """
    input_variables: list[str] | set[str]

View on GitHub (pinned to e32fa9a52e)

Solutions

  1. Prefer `PromptTemplate.from_template(template)` which infers `input_variables` automatically instead of declaring them by hand
  2. Run `get_template_variables(template, template_format)` and pass its output as `input_variables`
  3. Diff the template placeholders against the declared list: the message shows the declared list; extract `{...}` fields from the template and reconcile names

Example fix

# before
PromptTemplate(template="Hi {name}", input_variables=["user"])

# after
PromptTemplate.from_template("Hi {name}")  # input_variables inferred: ['name']
Defensive patterns

Strategy: validation

Validate before calling

from langchain_core.prompts.string import get_template_variables

def build_prompt(template: str, fmt: str = "f-string") -> PromptTemplate:
    return PromptTemplate(
        template=template,
        input_variables=get_template_variables(template, fmt),
        template_format=fmt,
    )

Try / catch

try:
    p = PromptTemplate(template=t, input_variables=declared)
except ValueError as e:
    if "Invalid prompt schema" in str(e):
        actual = set(get_template_variables(t, "f-string"))
        missing = actual - set(declared)
        raise ValueError(f"input_variables missing {sorted(missing)}") from e
    raise

Prevention

When it happens

Trigger: `PromptTemplate(template="Hi {name}", input_variables=["user"])` — template needs `name` but `user` was declared. Or `input_variables=["0"]`-style mismatches. Fires when `validate_template` is enabled (or schemas are checked) at construction, and again from `check_valid_template` callers.

Common situations: Renaming a placeholder in the template text but forgetting to update `input_variables`; building `PromptTemplate(...)` directly (bypassing `from_template`, which infers variables automatically); declaring extra variables the template never uses while omitting ones it does.

Related errors


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