{"record":{"id":"a150e5772deab35c","repo":"langchain-ai/langchain","slug":"invalid-template-format-template-format-r-shoul","errorCode":null,"errorMessage":"Invalid template format {template_format!r}, should be one of {list(DEFAULT_FORMATTER_MAPPING)}.","messagePattern":"Invalid template format (.+?), should be one of (.+?)\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/prompts/string.py","lineNumber":285,"sourceCode":"    Args:\n        template: The template string.\n        template_format: The template format.\n\n            Should be one of `'f-string'` or `'jinja2'`.\n        input_variables: The input variables.\n\n    Raises:\n        ValueError: If the template format is not supported.\n        ValueError: If the prompt schema is invalid.\n    \"\"\"\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.","sourceCodeStart":267,"sourceCodeEnd":303,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/prompts/string.py#L267-L303","documentation":"Raised by `check_valid_template` in `langchain_core.prompts.string` when `template_format` is not a key of `DEFAULT_FORMATTER_MAPPING` — i.e. not one of 'f-string', 'jinja2', 'mustache'. The lookup `DEFAULT_VALIDATOR_MAPPING[template_format]` raises `KeyError`, which is converted to a `ValueError` (chained via `from exc`) listing the accepted formats.","triggerScenarios":"Constructing `PromptTemplate(template=..., input_variables=..., template_format=\"f_string\")` (underscore typo), `\"fstring\"`, `\"Jinja2\"` (capitalized), or `\"template\"`. Any string outside the mapping triggers it during validation at construction time.","commonSituations":"Typos in configuration files or constructor kwargs; code written against older/newer LangChain versions where the supported format set differed (e.g. 'mustache' being a newer addition); case-sensitive values passed from user input or environment variables.","solutions":["Use exactly one of the supported literals: `template_format=\"f-string\"` (default), `\"jinja2\"`, or `\"mustache\"`","Normalize external config values before use: `fmt.strip().lower()` and validate against the allowed set early","Check the error message itself — it prints the accepted list from `DEFAULT_FORMATTER_MAPPING` for the installed version"],"exampleFix":"# before\nPromptTemplate.from_template(t, template_format=\"f_string\")  # ValueError\n\n# after\nPromptTemplate.from_template(t, template_format=\"f-string\")","handlingStrategy":"validation","validationCode":"SUPPORTED_FORMATS = {\"f-string\", \"jinja2\", \"mustache\"}\n\ndef normalize_format(fmt: str) -> str:\n    fmt = fmt.strip().lower()\n    if fmt not in SUPPORTED_FORMATS:\n        msg = f\"template_format must be one of {sorted(SUPPORTED_FORMATS)}, got {fmt!r}\"\n        raise ValueError(msg)\n    return fmt","typeGuard":"def is_valid_template_format(fmt: str) -> bool:\n    return fmt in {\"f-string\", \"jinja2\", \"mustache\"}","tryCatchPattern":"try:\n    p = PromptTemplate.from_template(t, template_format=fmt)\nexcept ValueError as e:\n    if \"Invalid template format\" in str(e):\n        raise ValueError(f\"config error: {fmt!r} is not a template_format\") from e\n    raise","preventionTips":["Centralize the template_format string as a constant/enumeration","Validate config-file values at load time, not at prompt construction","Remember the exact literal is 'f-string' with a hyphen"],"tags":["prompts","template-format","valueerror","configuration","typo"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}