{"record":{"id":"76130a1694ca391c","repo":"zylon-ai/private-gpt","slug":"names-already-registered-existing-names","errorCode":null,"errorMessage":"Names already registered: {existing_names}","messagePattern":"Names already registered: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"private_gpt/components/llm/registry.py","lineNumber":49,"sourceCode":"        \"\"\"Default name for the main LLM.\"\"\"\n        return \"default\"\n\n    def register(\n        self, name: str, component: LLMInstance, aliases: list[str] | None = None\n    ) -> None:\n        \"\"\"Register a new LLM component with a given name and optional aliases.\n\n        :param name: The primary name of the LLM component.\n        :param component: The LLM component to register.\n        :param aliases: Optional list of aliases for this component.\n        \"\"\"\n        aliases = [alias.strip() for alias in (aliases or []) if alias.strip()]\n        all_names = [name, *aliases]\n\n        # Check if any name is already registered\n        existing_names = [n for n in all_names if n in self._registry]\n        if existing_names:\n            raise ValueError(f\"Names already registered: {existing_names}\")\n\n        # Register component under all names\n        for alias in all_names:\n            self._registry[alias] = component\n\n    def unregister(self, name: str) -> None:\n        \"\"\"Unregister an LLM component by its name or alias.\n\n        :param name: The name or alias of the LLM component to unregister.\n        \"\"\"\n        if name not in self._registry:\n            raise KeyError(f\"LLM component '{name}' is not registered.\")\n        del self._registry[name]\n\n    def get(self, name: str) -> LLMInstance | None:\n        \"\"\"Retrieve an LLM component by its name or alias.\n\n        :param name: The name or alias of the LLM component to retrieve.","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/zylon-ai/private-gpt/blob/4a030776a31a901ad80b1bf4d7faa2c1a367efbb/private_gpt/components/llm/registry.py#L31-L67","documentation":"LLMRegistry.register stores a component under a primary name plus aliases, and refuses any name collision. Before inserting, it collects every requested name already present in the internal dict and raises ValueError listing them, keeping registrations unambiguous.","triggerScenarios":"Calling registry.register('default', instance) twice, or registering a component whose alias (e.g. 'openai') already maps to another LLMInstance. Also happens in tests that re-register the default LLM without resetting the singleton registry.","commonSituations":"Reload/re-import loops or DI containers re-running wiring code against the @singleton LLMRegistry; adding a new LLM component whose alias clashes with an existing one; test suites that don't call unregister between cases.","solutions":["Check registry.get_all_aliases() (or registry.get(name)) before registering and unregister the existing entry first.","Use unique names/aliases for each new LLM component.","In tests, call registry.unregister(name) in teardown or use a fresh LLMRegistry instance instead of the singleton."],"exampleFix":"// before\nregistry.register('default', llm_instance, aliases=['openai'])  # raises if taken\n\n// after\nfor n in ['default', 'openai']:\n    if registry.get(n) is not None:\n        registry.unregister(n)\nregistry.register('default', llm_instance, aliases=['openai'])","handlingStrategy":"validation","validationCode":"def safe_register(registry, name, component, aliases=None):\n    wanted = [name, *(aliases or [])]\n    taken = [n for n in wanted if registry.get(n) is not None]\n    for n in taken:\n        registry.unregister(n)\n    registry.register(name, component, aliases=aliases)","typeGuard":null,"tryCatchPattern":"try:\n    registry.register(name, inst, aliases=aliases)\nexcept ValueError as e:\n    if 'already registered' in str(e):\n        registry.unregister(name)  # or pick new names\n        registry.register(name, inst, aliases=aliases)\n    else:\n        raise","preventionTips":["Register each LLM exactly once in the DI wiring path.","Derive names/aliases from a single constants module to avoid collisions.","Reset or fresh-instantiate the registry in test fixtures."],"tags":["registry","dependency-injection","lifecycle"],"backgroundTag":null,"analyzedSha":"4a030776a31a901ad80b1bf4d7faa2c1a367efbb","analyzedAt":"2026-08-15T03:51:26.951Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}