deepset-ai/haystack · error · PipelineError

Component instance cannot be added to the pipeline more than

Error message

Component instance cannot be added to the pipeline more than once. It is mapped to both '{previous_name}' and '{name}'.

What it means

add_components() tracks component instances by their Python id(). If the same instance object appears twice in the input mapping, it would be registered under two different names, which the pipeline forbids. This guard prevents one shared object from being wired under multiple node names.

Source

Thrown at haystack/core/pipeline/base.py:459

        :raises ValueError:
            If a component name is invalid or already belongs to a different component in this pipeline.
        :raises PipelineValidationError:
            If one of the given instances is not a component.
        :raises PipelineError:
            If a component instance is already in this pipeline under another name, is in another pipeline,
            or occurs more than once in the mapping.
        """
        components_to_add: list[tuple[str, Component]] = []
        component_names_by_id: dict[int, str] = {}

        for name, instance in components.items():
            if not self._validate_component(name, instance):
                continue

            instance_id = id(instance)
            previous_name = component_names_by_id.get(instance_id)
            if previous_name is not None:
                raise PipelineError(
                    f"Component instance cannot be added to the pipeline more than once. "
                    f"It is mapped to both '{previous_name}' and '{name}'."
                )

            component_names_by_id[instance_id] = name
            components_to_add.append((name, instance))

        for name, instance in components_to_add:
            self._add_component_to_graph(name, instance)

        return self

    def _validate_component(self, name: str, instance: Component) -> bool:
        """Validate a component before adding it, returning whether it needs to be added."""
        # Component names are unique
        if name in self.graph.nodes:
            if self.graph.nodes[name]["instance"] is instance:
                return False

View on GitHub (pinned to e318778c9b)

Solutions

  1. Create a separate instance for each name, e.g. {"a": MyComp(), "b": MyComp()}
  2. Check for accidental reuse of a variable instead of calling the constructor twice
  3. If intentional reuse is desired, wrap in a distinct component or add via separate Pipeline

Example fix

// before
comp = MyComp()
pipe.add_components({"a": comp, "b": comp})
// after
pipe.add_components({"a": MyComp(), "b": MyComp()})
Defensive patterns

Strategy: validation

Validate before calling

def has_unique_instances(components: dict) -> bool:
    ids = [id(c) for c in components.values()]
    return len(ids) == len(set(ids))

Type guard

from haystack.core.component import Component

def are_distinct_components(mapping: dict[str, Component]) -> bool:
    return len({id(v) for v in mapping.values()}) == len(mapping)

Try / catch

from haystack.core.errors import PipelineError
try:
    pipe.add_components(components)
except PipelineError as e:
    logger.error("Duplicate component instance: %s", e)
    raise

Prevention

When it happens

Trigger: Calling pipe.add_components({"a": comp, "b": comp}) with the identical component instance for two keys; building the components dict programmatically or via a loop that reuses one instance; deserializing pipelines that share instances.

Common situations: Constructing pipelines dynamically where a variable holding a component is reused; copy-paste of add_component calls changed only in the name string; factory functions returning a cached singleton component.

Related errors


AI-assisted analysis of deepset-ai/haystack@e318778c9b (2026-08-30). Data as JSON: /api/errors/d9e8dcace2c3c563. Report an issue: GitHub.