# Model Registry
URL: https://docs.datarobot.com/en/docs/api/dev-learning/python/mlops/model_registry.html

> Create and manage registered models and versions with the Python API client.

# Model Registry

Registered models are generic containers that group multiple versions of models which can be deployed, used as a challenger model, or replace a deployed model.
Each registered model can have multiple versions.
Each version can be created from a DataRobot model, custom model, or external model.
Registered models can have versions of different types (Leaderboard, custom, or external) simultaneously as long as they have same target properties and time series settings, where applicable.

## Create a registered model

To create a registered model or add a version to an existing model:

```
LEADERBOARD_MODEL_ID = "650c2372c538ffa4480567d1"
# Passing registered_model_name creates new version
first_version = dr.RegisteredModelVersion.create_for_leaderboard_item(
    model_id=LEADERBOARD_MODEL_ID,
    name="Name of the version(aka model package)",
    registered_model_name='DEMO 3: Name of the registered model unique across the org '
)
# Add custom model as a version
# passing registered_model_id adds version to existing registered model
CUSTOM_MODEL_VERSION_ID = "619679c86c1abbc2bd628ed1"
second_version_from_custom = dr.RegisteredModelVersion.create_for_custom_model_version(
    custom_model_version_id=CUSTOM_MODEL_VERSION_ID,
    registered_model_id=first_version.registered_model_id,
    name="Another Name of the version(aka model package)",
)

# Add external model as a version
second_version_from_external = dr.RegisteredModelVersion.create_for_external(
    name='Another name',
    target={'name': 'Target', 'type': 'Regression'},
    registered_model_id=first_version.registered_model_id,
)
```

## List and filter registered models

Use the following command to list registered models.

You can filter the registered models that are returned by passing an instance of the [RegisteredModelListFilters](https://docs.datarobot.com/en/docs/api/reference/sdk/datarobot-models.html#datarobot.models.model_registry.RegisteredModelListFilters) class to the `filters` keyword argument.

You can also filter the registered model versions that are returned by passing an instance of the [RegisteredModelVersionsListFilters](https://docs.datarobot.com/en/docs/api/reference/sdk/datarobot-models.html#datarobot.models.model_registry.RegisteredModelVersionsListFilters) class to the `filters` keyword argument.

```
demo_registered_models = dr.RegisteredModel.list(search="DEMO")
registered_model_filters = dr.models.model_registry.RegisteredModelListFilters(
    created_at_start=datetime.datetime(2020, 1, 1),
    created_at_end=datetime.datetime(2024, 1, 2),
    modified_at_start=datetime.datetime(2020, 1, 1),
    modified_at_end=datetime.datetime(2024, 1, 2),
    target_name='readmitted',
    target_type='Binary',
    created_by='john.doe@example.com',
    compatible_with_model_package_id='650a9f57d3f427ce1cc64747',
    prediction_threshold=0.5,
    imported=False,
    for_challenger=False,
)
registered_models = dr.RegisteredModel.list(filters=registered_model_filters, search="10k")
registered_model = registered_models[0]
versions = registered_model.list_versions()
# Similarly to registered models, versions also support fine-grain filtering and search
filters = dr.models.model_registry.RegisteredModelVersionsListFilters(
    target_name='readmitted',
)
versions_with_search = registered_model.list_versions(search="Elastic", filters=filters)
```

## Manage registered models

Use the following command to archive registered models.
Archiving registered models archives all the versions of the registered model.

```
REGISTERED_MODEL_ID = "651bd2317aed25ed7d4bca7f"
dr.RegisteredModel.archive(REGISTERED_MODEL_ID)
```

Use the following command to update registered models.

```
REGISTERED_MODEL_ID = "651bd2317aed25ed7d4bca7f"
dr.RegisteredModel.update(REGISTERED_MODEL_ID, name="New name")
```

To share registered models with other users or groups, or retrieve existing roles on the deployment:

```
registered_model = dr.RegisteredModel.get('645b62d5373ed49b485d73e9')
# EXISTING ROLES
roles = registered_model.get_shared_roles()

role = dr.SharingRole(
    share_recipient_type="user",
    id='5ca19879a950d002c61ea3e7',
    role="USER",
)
registered_model.share([role])
```

## List deployments associated with a registered model

Use the following command to list deployments associated with registered model.
The deployment is considered associated if one of the versions of the registered model is either a champion or a challenger model.

```
model_with_deployments = dr.RegisteredModel.get('65035d911e9ff5b07f00f2ea')
# we can list deployments associated with this registered model. Method is searchable and paginated.
model_associated_deployments = model_with_deployments.list_associated_deployments()
# we can also list deployments associated with specific version of the registered model
version = model_with_deployments.list_versions()[1]
version.list_associated_deployments()
```

---
