{"record":{"id":"66024a9a9f895a67","repo":"langchain-ai/langchain","slug":"cannot-have-same-variable-partialed-twice","errorCode":null,"errorMessage":"Cannot have same variable partialed twice.","messagePattern":"Cannot have same variable partialed twice\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/prompts/prompt.py","lineNumber":168,"sourceCode":"        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,\n            )\n        if isinstance(other, str):\n            prompt = PromptTemplate.from_template(\n                other,\n                template_format=self.template_format,\n            )\n            return self + prompt\n        msg = f\"Unsupported operand type for +: {type(other)}\"\n        raise NotImplementedError(msg)\n\n    @property","sourceCodeStart":150,"sourceCodeEnd":186,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/prompts/prompt.py#L150-L186","documentation":"Raised by `PromptTemplate.__add__` when both operands are `PromptTemplate` instances and they each define a partial variable with the same name. A partial variable fixes a value for a placeholder at construction time; two conflicting partials for one name cannot be merged, so the addition raises `ValueError`.","triggerScenarios":"`PromptTemplate.from_template(\"{a} {b}\", partial_variables={\"a\": 1}) + PromptTemplate.from_template(\"{a} {c}\", partial_variables={\"a\": 2})` — both partial the variable `a`. The check iterates `other.partial_variables` and raises on the first key already present in `self.partial_variables`.","commonSituations":"Building reusable prompt components (e.g. a 'tone' partial and a 'language' partial) that both set a shared variable like `style`; merging prompt libraries where each partials common boilerplate such as `today` or `context`.","solutions":["Remove the duplicate partial from one of the templates before adding: rebuild one template without that partial_variables entry","Give the two variables distinct names in the templates (e.g. `a1`/`a2`) so each partial applies to its own placeholder","Partial the merged result once instead of partialing both inputs: add the raw templates first, then call `.partial(a=...)` on the combined template"],"exampleFix":"# before\np1 = PromptTemplate.from_template(\"{style}: {x}\", partial_variables={\"style\": \"formal\"})\np2 = PromptTemplate.from_template(\"{y} ({style})\", partial_variables={\"style\": \"casual\"})\ncombined = p1 + p2  # ValueError: Cannot have same variable partialed twice.\n\n# after\np2 = PromptTemplate.from_template(\"{y} ({style})\")\ncombined = (p1 + p2).partial(style=\"formal\")  # single partial on merged template","handlingStrategy":"validation","validationCode":"def merge_prompts(a: PromptTemplate, b: PromptTemplate) -> PromptTemplate:\n    clash = set(a.partial_variables) & set(b.partial_variables)\n    if clash:\n        msg = f\"duplicate partial variables: {sorted(clash)}\"\n        raise ValueError(msg)\n    return a + b","typeGuard":"def has_no_partial_clash(a: PromptTemplate, b: PromptTemplate) -> bool:\n    return not (set(a.partial_variables) & set(b.partial_variables))","tryCatchPattern":"try:\n    combined = p1 + p2\nexcept ValueError as e:\n    if \"partialed twice\" in str(e):\n        clash = set(p1.partial_variables) & set(p2.partial_variables)\n        b2 = PromptTemplate.from_template(p2.template, template_format=p2.template_format)  # drop partials\n        combined = (p1 + b2).partial(**{k: p1.partial_variables[k] for k in clash})\n    else:\n        raise","preventionTips":["Keep partial_variables in one place (apply partials after merging, not before)","Lint shared variable names when composing reusable prompt components","Document which variables each partial sets to avoid silent collisions"],"tags":["prompts","prompt-template","partial-variables","valueerror"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}