{"record":{"id":"9d351fed7bb4a2b0","repo":"langchain-ai/langchain","slug":"cannot-add-templates-of-different-formats","errorCode":null,"errorMessage":"Cannot add templates of different formats","messagePattern":"Cannot add templates of different formats","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/prompts/prompt.py","lineNumber":157,"sourceCode":"\n        return mustache_schema(self.template)\n\n    def __add__(self, other: Any) -> PromptTemplate:\n        \"\"\"Override the `+` operator to allow for combining prompt templates.\n\n        Raises:\n            ValueError: If the template formats are not f-string or if there are\n                conflicting partial variables.\n            NotImplementedError: If the other object is not a `PromptTemplate` or str.\n\n        Returns:\n            A new `PromptTemplate` that is the combination of the two.\n        \"\"\"\n        # Allow for easy combining\n        if isinstance(other, PromptTemplate):\n            if self.template_format != other.template_format:\n                msg = \"Cannot add templates of different formats\"\n                raise ValueError(msg)\n            input_variables = list(\n                set(self.input_variables) | set(other.input_variables)\n            )\n            template = self.template + other.template\n            # If any do not want to validate, then don't\n            validate_template = self.validate_template and other.validate_template\n            partial_variables = dict(self.partial_variables.items())\n            for k, v in other.partial_variables.items():\n                if k in partial_variables:\n                    msg = \"Cannot have same variable partialed twice.\"\n                    raise ValueError(msg)\n                partial_variables[k] = v\n            return PromptTemplate(\n                template=template,\n                input_variables=input_variables,\n                partial_variables=partial_variables,\n                template_format=self.template_format,\n                validate_template=validate_template,","sourceCodeStart":139,"sourceCodeEnd":175,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/prompts/prompt.py#L139-L175","documentation":"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`.","triggerScenarios":"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.","commonSituations":"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.","solutions":["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)`","Rewrite one template's syntax to match the other (convert `{{ var }}` Jinja2 syntax to `{var}` f-string syntax, or vice versa)","Instead of `+`, format each template separately and concatenate the resulting strings: `prompt_a.format(**a) + prompt_b.format(**b)`"],"exampleFix":"// before\np1 = PromptTemplate.from_template(\"Summarize: {text}\")\np2 = PromptTemplate.from_template(\"{% for d in docs %}{{ d }}{% endfor %}\", template_format=\"jinja2\")\ncombined = p1 + p2  # ValueError: Cannot add templates of different formats\n\n// after\np2 = PromptTemplate.from_template(\"{docs}\", template_format=\"f-string\")\ncombined = p1 + p2  # works, both f-string","handlingStrategy":"validation","validationCode":"def safe_add_prompts(a: PromptTemplate, b: PromptTemplate) -> PromptTemplate:\n    if a.template_format != b.template_format:\n        msg = f\"template_format mismatch: {a.template_format!r} vs {b.template_format!r}\"\n        raise ValueError(msg)\n    return a + b","typeGuard":"def same_format(a: PromptTemplate, b: PromptTemplate) -> bool:\n    return a.template_format == b.template_format","tryCatchPattern":"try:\n    combined = p1 + p2\nexcept ValueError as e:\n    if \"different formats\" in str(e):\n        p2 = PromptTemplate.from_template(p2.template, template_format=p1.template_format)\n        combined = p1 + p2\n    else:\n        raise","preventionTips":["Standardize one template_format across all prompts in a project (constant/enum)","Centralize prompt construction in a factory so formats never diverge","Assert format equality before concatenating prompt libraries from different sources"],"tags":["prompts","prompt-template","valueerror","operator-overload"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}