langchain-ai/langchain · error · ValueError
Invalid template format {template_format!r}, should be one o
Error message
Invalid template format {template_format!r}, should be one of {list(DEFAULT_FORMATTER_MAPPING)}. What it means
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.
Source
Thrown at libs/core/langchain_core/prompts/string.py:285
Args:
template: The template string.
template_format: The template format.
Should be one of `'f-string'` or `'jinja2'`.
input_variables: The input variables.
Raises:
ValueError: If the template format is not supported.
ValueError: If the prompt schema is invalid.
"""
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.View on GitHub (pinned to e32fa9a52e)
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
Example fix
# before PromptTemplate.from_template(t, template_format="f_string") # ValueError # after PromptTemplate.from_template(t, template_format="f-string")
Defensive patterns
Strategy: validation
Validate before calling
SUPPORTED_FORMATS = {"f-string", "jinja2", "mustache"}
def normalize_format(fmt: str) -> str:
fmt = fmt.strip().lower()
if fmt not in SUPPORTED_FORMATS:
msg = f"template_format must be one of {sorted(SUPPORTED_FORMATS)}, got {fmt!r}"
raise ValueError(msg)
return fmt Type guard
def is_valid_template_format(fmt: str) -> bool:
return fmt in {"f-string", "jinja2", "mustache"} Try / catch
try:
p = PromptTemplate.from_template(t, template_format=fmt)
except ValueError as e:
if "Invalid template format" in str(e):
raise ValueError(f"config error: {fmt!r} is not a template_format") from e
raise Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Unsupported template format: {template_format}
- Saving an example selector is not currently supported
- Loading {config_type} prompt not supported
- Mustache templates cannot be validated.
- Input variables must be provided to validate the template.
AI-assisted analysis of langchain-ai/langchain@e32fa9a52e (2026-08-14).
Data as JSON: /api/errors/a150e5772deab35c.
Report an issue: GitHub.