{"record":{"id":"baf1c6d55c3357b5","repo":"invoke-ai/InvokeAI","slug":"model-key-key-already-exists-provide-a-differ","errorCode":null,"errorMessage":"Model key '{key}' already exists. Provide a different key to install this external model.","messagePattern":"Model key '(.+?)' already exists\\. Provide a different key to install this external model\\.","errorType":"exception","errorClass":"DuplicateModelException","httpStatus":409,"severity":"error","filePath":"invokeai/app/services/model_install/model_install_default.py","lineNumber":1036,"sourceCode":"        existing_external = next(\n            (\n                model\n                for model in self.record_store.search_by_attr(\n                    base_model=BaseModelType.External, model_type=ModelType.ExternalImageGenerator\n                )\n                if isinstance(model, ExternalApiModelConfig)\n                and model.provider_id == provider_id\n                and model.provider_model_id == provider_model_id\n            ),\n            None,\n        )\n\n        if existing_external is not None:\n            key = existing_external.key\n        else:\n            try:\n                self.record_store.get_model(key)\n                raise DuplicateModelException(\n                    f\"Model key '{key}' already exists. Provide a different key to install this external model.\"\n                )\n            except UnknownModelException:\n                pass\n\n        config = ExternalApiModelConfig(\n            key=key,\n            name=name,\n            description=job.config_in.description,\n            provider_id=provider_id,\n            provider_model_id=provider_model_id,\n            capabilities=capabilities,\n            default_settings=default_settings,\n            source=str(job.source),\n            source_type=MODEL_SOURCE_TO_TYPE_MAP[job.source.__class__],\n            path=\"\",\n            hash=\"\",\n            file_size=0,","sourceCodeStart":1018,"sourceCodeEnd":1054,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/app/services/model_install/model_install_default.py#L1018-L1054","documentation":"_register_external_model() checks the record store before registering an external (provider-API) model under a caller-supplied key. If a model with that key already exists, it raises DuplicateModelException asking for a different key. This guard exists because external model keys are caller-chosen, so collisions must be caught explicitly.","triggerScenarios":"Calling _register_or_install/install with ModelRecordChanges containing a 'key' that already exists in the record store; installing the same external model twice with the same hard-coded key; restoring from a store that already contains the key.","commonSituations":"Scripts that install external models idempotently but reuse a fixed key without checking first; two concurrent installs with the same key; copying install commands between environments sharing a database.","solutions":["Omit the 'key' field in ModelRecordChanges and let the service generate a unique key.","Generate a fresh unique key (e.g. from the source id + hash) before installing.","If the model is intentionally the same, look it up first and skip re-installation.","Use install_or_update-style flows that update an existing model instead of registering a duplicate."],"exampleFix":"// before\nconfig = ModelRecordChanges(source=source, key='my-sdxl')  # collides\n// after\nconfig = ModelRecordChanges(source=source)  # let service assign a unique key\nservice.install(config)","handlingStrategy":"try-catch","validationCode":"from invokeai.app.services.shared.sqlite.sqlite_common import UnknownModelException\nkey_collision = False\ntry:\n    service.record_store.get_model(key)\n    key_collision = True\nexcept UnknownModelException:\n    pass","typeGuard":null,"tryCatchPattern":"try:\n    service.install(ModelRecordChanges(source=src, key=key))\nexcept DuplicateModelException:\n    existing = service.record_store.get_model(key)  # already installed; update or skip","preventionTips":["Omit the 'key' field and let the service generate unique keys.","Treat installs as idempotent: look up before inserting.","Avoid hard-coded keys shared across scripts/environments.","Serialize external-model installs to avoid concurrent key races."],"tags":["duplicate","idempotency","api"],"backgroundTag":"duplicate-resource-key","analyzedSha":"0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06","analyzedAt":"2026-08-29T04:46:49.967Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}