{"record":{"id":"d9e8dcace2c3c563","repo":"deepset-ai/haystack","slug":"component-instance-cannot-be-added-to-the-pipeline","errorCode":null,"errorMessage":"Component instance cannot be added to the pipeline more than once. It is mapped to both '{previous_name}' and '{name}'.","messagePattern":"Component instance cannot be added to the pipeline more than once\\. It is mapped to both '(.+?)' and '(.+?)'\\.","errorType":"exception","errorClass":"PipelineError","httpStatus":null,"severity":"error","filePath":"haystack/core/pipeline/base.py","lineNumber":459,"sourceCode":"        :raises ValueError:\n            If a component name is invalid or already belongs to a different component in this pipeline.\n        :raises PipelineValidationError:\n            If one of the given instances is not a component.\n        :raises PipelineError:\n            If a component instance is already in this pipeline under another name, is in another pipeline,\n            or occurs more than once in the mapping.\n        \"\"\"\n        components_to_add: list[tuple[str, Component]] = []\n        component_names_by_id: dict[int, str] = {}\n\n        for name, instance in components.items():\n            if not self._validate_component(name, instance):\n                continue\n\n            instance_id = id(instance)\n            previous_name = component_names_by_id.get(instance_id)\n            if previous_name is not None:\n                raise PipelineError(\n                    f\"Component instance cannot be added to the pipeline more than once. \"\n                    f\"It is mapped to both '{previous_name}' and '{name}'.\"\n                )\n\n            component_names_by_id[instance_id] = name\n            components_to_add.append((name, instance))\n\n        for name, instance in components_to_add:\n            self._add_component_to_graph(name, instance)\n\n        return self\n\n    def _validate_component(self, name: str, instance: Component) -> bool:\n        \"\"\"Validate a component before adding it, returning whether it needs to be added.\"\"\"\n        # Component names are unique\n        if name in self.graph.nodes:\n            if self.graph.nodes[name][\"instance\"] is instance:\n                return False","sourceCodeStart":441,"sourceCodeEnd":477,"githubUrl":"https://github.com/deepset-ai/haystack/blob/e318778c9bf60a1963e3b5f451359655dd696c30/haystack/core/pipeline/base.py#L441-L477","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Create a separate instance for each name, e.g. {\"a\": MyComp(), \"b\": MyComp()}","Check for accidental reuse of a variable instead of calling the constructor twice","If intentional reuse is desired, wrap in a distinct component or add via separate Pipeline"],"exampleFix":"// before\ncomp = MyComp()\npipe.add_components({\"a\": comp, \"b\": comp})\n// after\npipe.add_components({\"a\": MyComp(), \"b\": MyComp()})","handlingStrategy":"validation","validationCode":"def has_unique_instances(components: dict) -> bool:\n    ids = [id(c) for c in components.values()]\n    return len(ids) == len(set(ids))","typeGuard":"from haystack.core.component import Component\n\ndef are_distinct_components(mapping: dict[str, Component]) -> bool:\n    return len({id(v) for v in mapping.values()}) == len(mapping)","tryCatchPattern":"from haystack.core.errors import PipelineError\ntry:\n    pipe.add_components(components)\nexcept PipelineError as e:\n    logger.error(\"Duplicate component instance: %s\", e)\n    raise","preventionTips":["Construct a new instance per name instead of reusing variables","Assert uniqueness of id() values when building the dict dynamically","Avoid caching component instances in module-level singletons for pipeline use"],"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"}