deepset-ai/haystack · error · PipelineError

Component has already been added to this Pipeline under the

Error message

Component has already been added to this Pipeline under the name '{existing_name}'. A component instance can only be added once.

What it means

A component instance can only belong to one pipeline (or appear once per pipeline). _validate_component checks the instance's __haystack_added_to_pipeline__ attribute: if it is already owned by this pipeline under a different name, or by another pipeline, a PipelineError is raised, because sharing instances would corrupt graph state.

Source

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

        # Component instances must be components
        if not isinstance(instance, Component):
            raise PipelineValidationError(
                f"'{type(instance)}' doesn't seem to be a component. Is this class decorated with @component?"
            )

        if owning_pipeline := getattr(instance, "__haystack_added_to_pipeline__", None):
            if owning_pipeline is self:
                existing_name = self.get_component_name(instance)
                msg = (
                    f"Component has already been added to this Pipeline under the name '{existing_name}'. "
                    "A component instance can only be added once."
                )
            else:
                msg = (
                    "Component has already been added in another Pipeline. "
                    "Components can't be shared between Pipelines. Create a new instance instead."
                )
            raise PipelineError(msg)

        return True

    def _add_component_to_graph(self, name: str, instance: Component) -> None:
        """Add an already validated component to the graph."""
        setattr(instance, "__haystack_added_to_pipeline__", self)  # noqa: B010
        setattr(instance, "__component_name__", name)  # noqa: B010

        # Add component to the graph, disconnected
        logger.debug("Adding component '{component_name}' ({component})", component_name=name, component=instance)
        # We're completely sure the fields exist so we ignore the type error
        self.graph.add_node(
            name,
            instance=instance,
            input_sockets=instance.__haystack_input__._sockets_dict,  # type: ignore[attr-defined]
            output_sockets=instance.__haystack_output__._sockets_dict,  # type: ignore[attr-defined]
            visits=0,
        )

View on GitHub (pinned to e318778c9b)

Solutions

  1. Create a fresh instance for each pipeline: Pipeline().add_component("c", MyComp())
  2. If it is already in this pipeline, reuse its existing name (get_component_name) instead of re-adding
  3. Refactor shared logic into separate instances or a factory function that returns new instances

Example fix

// before
comp = MyComp()
pipe1.add_component("a", comp)
pipe2.add_component("a", comp)  # shared instance
// after
pipe1.add_component("a", MyComp())
pipe2.add_component("a", MyComp())
Defensive patterns

Strategy: validation

Validate before calling

def instance_is_free(instance, pipelines) -> bool:
    return not getattr(instance, "__haystack_added_to_pipeline__", None) \
        or getattr(instance, "__haystack_added_to_pipeline__") not in pipelines

Type guard

def is_unowned_component(instance) -> bool:
    return getattr(instance, "__haystack_added_to_pipeline__", None) is None

Try / catch

from haystack.core.errors import PipelineError
try:
    pipe.add_component(name, comp)
except PipelineError as e:
    logger.error("Component reuse detected: %s", e)
    raise

Prevention

When it happens

Trigger: Adding the same instance twice under different names in one pipeline; sharing a component instance between two Pipeline objects; reusing a component that was already wired into a deployed pipeline.

Common situations: Global/cached component instances reused across pipelines in an app; moving components between pipelines without creating new instances; loops accumulating components into multiple pipelines.

Related errors


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