{"record":{"id":"f0da0822c479a3f0","repo":"deepset-ai/haystack","slug":"component-has-already-been-added-to-this-pipeline","errorCode":null,"errorMessage":"Component has already been added to this Pipeline under the name '{existing_name}'. A component instance can only be added once.","messagePattern":"Component has already been added to this Pipeline under the name '(.+?)'\\. A component instance can only be added once\\.","errorType":"exception","errorClass":"PipelineError","httpStatus":null,"severity":"error","filePath":"haystack/core/pipeline/base.py","lineNumber":506,"sourceCode":"        # Component instances must be components\n        if not isinstance(instance, Component):\n            raise PipelineValidationError(\n                f\"'{type(instance)}' doesn't seem to be a component. Is this class decorated with @component?\"\n            )\n\n        if owning_pipeline := getattr(instance, \"__haystack_added_to_pipeline__\", None):\n            if owning_pipeline is self:\n                existing_name = self.get_component_name(instance)\n                msg = (\n                    f\"Component has already been added to this Pipeline under the name '{existing_name}'. \"\n                    \"A component instance can only be added once.\"\n                )\n            else:\n                msg = (\n                    \"Component has already been added in another Pipeline. \"\n                    \"Components can't be shared between Pipelines. Create a new instance instead.\"\n                )\n            raise PipelineError(msg)\n\n        return True\n\n    def _add_component_to_graph(self, name: str, instance: Component) -> None:\n        \"\"\"Add an already validated component to the graph.\"\"\"\n        setattr(instance, \"__haystack_added_to_pipeline__\", self)  # noqa: B010\n        setattr(instance, \"__component_name__\", name)  # noqa: B010\n\n        # Add component to the graph, disconnected\n        logger.debug(\"Adding component '{component_name}' ({component})\", component_name=name, component=instance)\n        # We're completely sure the fields exist so we ignore the type error\n        self.graph.add_node(\n            name,\n            instance=instance,\n            input_sockets=instance.__haystack_input__._sockets_dict,  # type: ignore[attr-defined]\n            output_sockets=instance.__haystack_output__._sockets_dict,  # type: ignore[attr-defined]\n            visits=0,\n        )","sourceCodeStart":488,"sourceCodeEnd":524,"githubUrl":"https://github.com/deepset-ai/haystack/blob/e318778c9bf60a1963e3b5f451359655dd696c30/haystack/core/pipeline/base.py#L488-L524","documentation":"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.","triggerScenarios":"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.","commonSituations":"Global/cached component instances reused across pipelines in an app; moving components between pipelines without creating new instances; loops accumulating components into multiple pipelines.","solutions":["Create a fresh instance for each pipeline: Pipeline().add_component(\"c\", MyComp())","If it is already in this pipeline, reuse its existing name (get_component_name) instead of re-adding","Refactor shared logic into separate instances or a factory function that returns new instances"],"exampleFix":"// before\ncomp = MyComp()\npipe1.add_component(\"a\", comp)\npipe2.add_component(\"a\", comp)  # shared instance\n// after\npipe1.add_component(\"a\", MyComp())\npipe2.add_component(\"a\", MyComp())","handlingStrategy":"validation","validationCode":"def instance_is_free(instance, pipelines) -> bool:\n    return not getattr(instance, \"__haystack_added_to_pipeline__\", None) \\\n        or getattr(instance, \"__haystack_added_to_pipeline__\") not in pipelines","typeGuard":"def is_unowned_component(instance) -> bool:\n    return getattr(instance, \"__haystack_added_to_pipeline__\", None) is None","tryCatchPattern":"from haystack.core.errors import PipelineError\ntry:\n    pipe.add_component(name, comp)\nexcept PipelineError as e:\n    logger.error(\"Component reuse detected: %s\", e)\n    raise","preventionTips":["Never share component instances across pipelines; use factories returning new instances","Use pipe.get_component_name(instance) to reuse an already-registered component by its name","Avoid module-level singletons for components used in pipelines"],"tags":["python","pipeline","api-misuse"],"backgroundTag":"component-reuse-in-pipeline","analyzedSha":"e318778c9bf60a1963e3b5f451359655dd696c30","analyzedAt":"2026-08-30T11:45:20.711Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}