langchain-ai/langchain · error · ValueError

Cannot add templates of different formats

Error message

Cannot add templates of different formats

What it means

Raised by `PromptTemplate.__add__` (the `+` operator) when the left and right operands are both `PromptTemplate` instances but their `template_format` fields differ (e.g. 'f-string' vs 'jinja2' vs 'mustache'). The combined template would be ambiguous because each half needs a different renderer, so LangChain refuses the merge with a `ValueError`.

Source

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

        return mustache_schema(self.template)

    def __add__(self, other: Any) -> PromptTemplate:
        """Override the `+` operator to allow for combining prompt templates.

        Raises:
            ValueError: If the template formats are not f-string or if there are
                conflicting partial variables.
            NotImplementedError: If the other object is not a `PromptTemplate` or str.

        Returns:
            A new `PromptTemplate` that is the combination of the two.
        """
        # Allow for easy combining
        if isinstance(other, PromptTemplate):
            if self.template_format != other.template_format:
                msg = "Cannot add templates of different formats"
                raise ValueError(msg)
            input_variables = list(
                set(self.input_variables) | set(other.input_variables)
            )
            template = self.template + other.template
            # If any do not want to validate, then don't
            validate_template = self.validate_template and other.validate_template
            partial_variables = dict(self.partial_variables.items())
            for k, v in other.partial_variables.items():
                if k in partial_variables:
                    msg = "Cannot have same variable partialed twice."
                    raise ValueError(msg)
                partial_variables[k] = v
            return PromptTemplate(
                template=template,
                input_variables=input_variables,
                partial_variables=partial_variables,
                template_format=self.template_format,
                validate_template=validate_template,

View on GitHub (pinned to e32fa9a52e)

Solutions

  1. Make both templates use the same `template_format` by re-creating one of them, e.g. `PromptTemplate.from_template(other.template, template_format=prompt.template_format)`
  2. Rewrite one template's syntax to match the other (convert `{{ var }}` Jinja2 syntax to `{var}` f-string syntax, or vice versa)
  3. Instead of `+`, format each template separately and concatenate the resulting strings: `prompt_a.format(**a) + prompt_b.format(**b)`

Example fix

// before
p1 = PromptTemplate.from_template("Summarize: {text}")
p2 = PromptTemplate.from_template("{% for d in docs %}{{ d }}{% endfor %}", template_format="jinja2")
combined = p1 + p2  # ValueError: Cannot add templates of different formats

// after
p2 = PromptTemplate.from_template("{docs}", template_format="f-string")
combined = p1 + p2  # works, both f-string
Defensive patterns

Strategy: validation

Validate before calling

def safe_add_prompts(a: PromptTemplate, b: PromptTemplate) -> PromptTemplate:
    if a.template_format != b.template_format:
        msg = f"template_format mismatch: {a.template_format!r} vs {b.template_format!r}"
        raise ValueError(msg)
    return a + b

Type guard

def same_format(a: PromptTemplate, b: PromptTemplate) -> bool:
    return a.template_format == b.template_format

Try / catch

try:
    combined = p1 + p2
except ValueError as e:
    if "different formats" in str(e):
        p2 = PromptTemplate.from_template(p2.template, template_format=p1.template_format)
        combined = p1 + p2
    else:
        raise

Prevention

When it happens

Trigger: Calling `prompt_a + prompt_b` where `prompt_a` was created with `template_format='f-string'` (the default) and `prompt_b` with `template_format='jinja2'` or `'mustache'` (e.g. via `PromptTemplate.from_template(t, template_format='jinja2')`). The check fires before any template concatenation happens.

Common situations: Mixing prompts copied from different examples/docs where one uses Jinja2 syntax (`{% for %}`, `{{ var }}`) and another uses `{var}` placeholders; composing a system prompt and a user prompt that were authored with different templating engines; partialing pipelines that combine prompts from third-party packages.

Related errors


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