langchain-ai/langchain · error · ValueError

Cannot have same variable partialed twice.

Error message

Cannot have same variable partialed twice.

What it means

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`.

Source

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

        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,
            )
        if isinstance(other, str):
            prompt = PromptTemplate.from_template(
                other,
                template_format=self.template_format,
            )
            return self + prompt
        msg = f"Unsupported operand type for +: {type(other)}"
        raise NotImplementedError(msg)

    @property

View on GitHub (pinned to e32fa9a52e)

Solutions

  1. Remove the duplicate partial from one of the templates before adding: rebuild one template without that partial_variables entry
  2. Give the two variables distinct names in the templates (e.g. `a1`/`a2`) so each partial applies to its own placeholder
  3. Partial the merged result once instead of partialing both inputs: add the raw templates first, then call `.partial(a=...)` on the combined template

Example fix

# before
p1 = PromptTemplate.from_template("{style}: {x}", partial_variables={"style": "formal"})
p2 = PromptTemplate.from_template("{y} ({style})", partial_variables={"style": "casual"})
combined = p1 + p2  # ValueError: Cannot have same variable partialed twice.

# after
p2 = PromptTemplate.from_template("{y} ({style})")
combined = (p1 + p2).partial(style="formal")  # single partial on merged template
Defensive patterns

Strategy: validation

Validate before calling

def merge_prompts(a: PromptTemplate, b: PromptTemplate) -> PromptTemplate:
    clash = set(a.partial_variables) & set(b.partial_variables)
    if clash:
        msg = f"duplicate partial variables: {sorted(clash)}"
        raise ValueError(msg)
    return a + b

Type guard

def has_no_partial_clash(a: PromptTemplate, b: PromptTemplate) -> bool:
    return not (set(a.partial_variables) & set(b.partial_variables))

Try / catch

try:
    combined = p1 + p2
except ValueError as e:
    if "partialed twice" in str(e):
        clash = set(p1.partial_variables) & set(p2.partial_variables)
        b2 = PromptTemplate.from_template(p2.template, template_format=p2.template_format)  # drop partials
        combined = (p1 + b2).partial(**{k: p1.partial_variables[k] for k in clash})
    else:
        raise

Prevention

When it happens

Trigger: `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`.

Common situations: 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`.

Related errors


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