{"record":{"id":"5847754d93f77ff8","repo":"langchain-ai/langchain","slug":"invalid-prompt-schema-check-for-mismatched-or-mis","errorCode":null,"errorMessage":"Invalid prompt schema; check for mismatched or missing input parameters from {input_variables}.","messagePattern":"Invalid prompt schema; check for mismatched or missing input parameters from (.+?)\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/prompts/string.py","lineNumber":295,"sourceCode":"    \"\"\"\n    try:\n        validator_func = DEFAULT_VALIDATOR_MAPPING[template_format]\n    except KeyError as exc:\n        msg = (\n            f\"Invalid template format {template_format!r}, should be one of\"\n            f\" {list(DEFAULT_FORMATTER_MAPPING)}.\"\n        )\n        raise ValueError(msg) from exc\n    if template_format == \"f-string\":\n        validate_f_string_template(template)\n    try:\n        validator_func(template, input_variables)\n    except (KeyError, IndexError) as exc:\n        msg = (\n            \"Invalid prompt schema; check for mismatched or missing input parameters\"\n            f\" from {input_variables}.\"\n        )\n        raise ValueError(msg) from exc\n\n\ndef get_template_variables(template: str, template_format: str) -> list[str]:\n    \"\"\"Get the variables from the template.\n\n    Args:\n        template: The template string.\n        template_format: The template format.\n\n            Should be one of `'f-string'`, `'mustache'` or `'jinja2'`.\n\n    Returns:\n        The variables from the template.\n\n    Raises:\n        ValueError: If the template format is not supported.\n    \"\"\"\n    input_variables: list[str] | set[str]","sourceCodeStart":277,"sourceCodeEnd":313,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/prompts/string.py#L277-L313","documentation":"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.","triggerScenarios":"`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.","commonSituations":"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.","solutions":["Prefer `PromptTemplate.from_template(template)` which infers `input_variables` automatically instead of declaring them by hand","Run `get_template_variables(template, template_format)` and pass its output as `input_variables`","Diff the template placeholders against the declared list: the message shows the declared list; extract `{...}` fields from the template and reconcile names"],"exampleFix":"# before\nPromptTemplate(template=\"Hi {name}\", input_variables=[\"user\"])\n\n# after\nPromptTemplate.from_template(\"Hi {name}\")  # input_variables inferred: ['name']","handlingStrategy":"validation","validationCode":"from langchain_core.prompts.string import get_template_variables\n\ndef build_prompt(template: str, fmt: str = \"f-string\") -> PromptTemplate:\n    return PromptTemplate(\n        template=template,\n        input_variables=get_template_variables(template, fmt),\n        template_format=fmt,\n    )","typeGuard":null,"tryCatchPattern":"try:\n    p = PromptTemplate(template=t, input_variables=declared)\nexcept ValueError as e:\n    if \"Invalid prompt schema\" in str(e):\n        actual = set(get_template_variables(t, \"f-string\"))\n        missing = actual - set(declared)\n        raise ValueError(f\"input_variables missing {sorted(missing)}\") from e\n    raise","preventionTips":["Prefer PromptTemplate.from_template() so variables are inferred automatically","Add a unit test per prompt asserting get_template_variables() matches declared inputs","Re-run variable checks after any edit to template text"],"tags":["prompts","input-variables","template-validation","valueerror"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}