langchain-ai/langchain · error · ValueError

Mustache templates cannot be validated.

Error message

Mustache templates cannot be validated.

What it means

In `PromptTemplate`'s pydantic validator, `validate_template=True` triggers template validation against the declared `input_variables`. Mustache has no static checker in langchain-core (its variable syntax `{{var}}` with sections/tags makes offline validation unreliable), so `validate_template=True` combined with `template_format='mustache'` raises this `ValueError` at construction time.

Source

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

    """Whether or not to try validating the template."""

    @model_validator(mode="before")
    @classmethod
    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"]
            ]

View on GitHub (pinned to e32fa9a52e)

Solutions

  1. Set `validate_template=False` (or omit it) for mustache templates: `PromptTemplate(..., template_format='mustache')`.
  2. Switch the template to `f-string` format if you want validation guarantees.
  3. For mustache, validate inputs manually before `format(**vars)`.

Example fix

# before
PromptTemplate(
    template='Hello {{name}}',
    input_variables=['name'],
    template_format='mustache',
    validate_template=True,  # ValueError
)

# after
PromptTemplate(
    template='Hello {{name}}',
    input_variables=['name'],
    template_format='mustache',
)
Defensive patterns

Strategy: validation

Validate before calling

if template_format == 'mustache':
    validate_template = False  # mustache cannot be validated
PromptTemplate(template=t, input_variables=vars_,
               template_format=template_format, validate_template=validate_template)

Type guard

def can_validate_template(fmt: str) -> bool:
    return fmt != 'mustache'

Prevention

When it happens

Trigger: `PromptTemplate(template=..., template_format='mustache', validate_template=True)` — explicitly or via a subclass/config that sets `validate_template`. Note `validate_template` defaults to `False`, so this only fires when opted in.

Common situations: Porting code that set `validate_template=True` for f-string safety onto a mustache template; copy-pasting constructor flags across templates of different formats; older LangChain versions where this combination behaved differently.

Related errors


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