# Deployments
URL: https://docs.datarobot.com/en/docs/api/dev-learning/python/mlops/deployment.html

> Deploy, manage, and monitor models with the Python API client.

# Deployments

This page outlines how you can deploy, manage, and monitor models with the Python API client.

## Manage deployments

The following commands can be used to manage deployments.

### Create a deployment

A new deployment can be created from:

- A DataRobot model - use create_from_registered_model_version() . Refer to the Model Registry documentation to reference how to create a registered model version.

When creating a new deployment, you must provide a DataRobot `registered_model_version_id` (also known as `model_package_id`) and a `label`.
Optionally, provide a `description` to document the purpose of the deployment.

The default prediction server is used when making predictions against the deployment and is required for creating a deployment on DataRobot in managed SaaS environments.
For Self-Managed users, you cannot provide a default prediction server.
Instead use a pre-configured prediction server.
Refer to [datarobot.PredictionServer.list](https://docs.datarobot.com/en/docs/api/reference/sdk/deployment-management.html#datarobot.PredictionServer.list) for more information on retrieving available prediction servers.

```
import datarobot as dr

project = dr.Project.get('6527eb38b9e5dead5fc12491')
model = project.get_models()[0]
prediction_server = dr.PredictionServer.list()[0]

registered_model_version = dr.RegisteredModelVersion.create_for_leaderboard_item(
    model_id=model.id,
    name="Name of the version(aka model package)",
    registered_model_name='Name of the registered model unique across the org '
)

deployment = dr.Deployment.create_from_registered_model_version(
    registered_model_version.id, label='New Deployment', description='A new deployment',
    default_prediction_server_id=prediction_server.id)
>>> Deployment('New Deployment')
```

### List deployments

To list deployments a user can view:

```
import datarobot as dr

deployments = dr.Deployment.list()
deployments
>>> [Deployment('New Deployment'), Deployment('Previous Deployment')]
```

Refer to [Deployment](https://docs.datarobot.com/en/docs/api/reference/sdk/deployment-management.html#datarobot.models.Deployment) to learn about the properties of the deployment object.

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

```
import datarobot as dr

filters = dr.models.deployment.DeploymentListFilters(
    role='OWNER',
    accuracy_health=dr.enums.DEPLOYMENT_ACCURACY_HEALTH_STATUS.FAILING
)
deployments = dr.Deployment.list(filters=filters)
deployments
>>> [Deployment('Deployment Owned by Me w/ Failing Accuracy 1'), Deployment('Deployment Owned by Me w/ Failing Accuracy 2')]
```

### Retrieve a deployment

It is possible to retrieve a single deployment with its identifier, rather than list all deployments:

```
import datarobot as dr

deployment = dr.Deployment.get(deployment_id='5c939e08962d741e34f609f0')
deployment.id
>>> '5c939e08962d741e34f609f0'
deployment.label
>>> 'New Deployment'
```

Refer to [Deployment](https://docs.datarobot.com/en/docs/api/reference/sdk/deployment-management.html#datarobot.models.Deployment) to learn about the properties of the deployment object.

### Update a deployment

To update a deployment’s label and description:

```
import datarobot as dr

deployment = dr.Deployment.get(deployment_id='5c939e08962d741e34f609f0')
deployment.update(label='new label')
```

### Delete a deployment

To mark a deployment as deleted:

```
import datarobot as dr

deployment = dr.Deployment.get(deployment_id='5c939e08962d741e34f609f0')
deployment.delete()
```

### Activate or deactivate a deployment

To activate a deployment:

```
import datarobot as dr

deployment = dr.Deployment.get(deployment_id='5c939e08962d741e34f609f0')
deployment.activate()
deployment.status
>>> 'active'
```

To deactivate a deployment:

```
import datarobot as dr

deployment = dr.Deployment.get(deployment_id='5c939e08962d741e34f609f0')
deployment.deactivate()
deployment.status
>>> 'inactive'
```

### Make batch predictions with a deployment

DataRobot provides a small utility function to make batch predictions using a deployment: [Deployment.predict_batch](https://docs.datarobot.com/en/docs/api/reference/sdk/deployment-management.html#datarobot.models.Deployment.predict_batch).

```
import datarobot as dr

deployment = dr.Deployment.get(deployment_id='5c939e08962d741e34f609f0')
# To note: `source` can be a file path, a file, or a pandas DataFrame
prediction_results_as_dataframe = deployment.predict_batch(
    source="./my_local_file.csv",
)
```

## Model replacement

A deployment’s model can be replaced effortlessly with zero interruption of predictions.

Model replacement is an asynchronous process, which means some preparatory work may be performed after the initial request is completed.
Predictions made against this deployment will start using the new model as soon as the request is completed.
There will be no interruption for predictions throughout the process.
The [replace_model()](https://docs.datarobot.com/en/docs/api/reference/sdk/deployment-management.html#datarobot.models.Deployment.replace_model) function won’t return until the asynchronous process is fully finished.

Alongside the identifier of the new model, a `reason` is also required.
The reason is stored in model history of the deployment for documentation purposes.
An enum, `MODEL_REPLACEMENT_REASON`, is provided for convenience. All possible values are documented below:

- MODEL_REPLACEMENT_REASON.ACCURACY
- MODEL_REPLACEMENT_REASON.DATA_DRIFT
- MODEL_REPLACEMENT_REASON.ERRORS
- MODEL_REPLACEMENT_REASON.SCHEDULED_REFRESH
- MODEL_REPLACEMENT_REASON.SCORING_SPEED
- MODEL_REPLACEMENT_REASON.OTHER

Below is an example of model replacement:

```
import datarobot as dr
from datarobot.enums import MODEL_REPLACEMENT_REASON

project = dr.Project.get('5cc899abc191a20104ff446a')
model = project.get_models()[0]

deployment = Deployment.get(deployment_id='5c939e08962d741e34f609f0')
deployment.model['id'], deployment.model['type']
>>> ('5c0a979859b00004ba52e431', 'Decision Tree Classifier (Gini)')

deployment.replace_model('5c0a969859b00004ba52e41b', MODEL_REPLACEMENT_REASON.ACCURACY)
deployment.model['id'], deployment.model['type']
>>> ('5c0a969859b00004ba52e41b', 'Support Vector Classifier (Linear Kernel)')
```

### Validation

Before initiating the model replacement request, it is usually a good idea to use the [validate_replacement_model()](https://docs.datarobot.com/en/docs/api/reference/sdk/deployment-management.html#datarobot.models.Deployment.validate_replacement_model) function to validate if the new model can be used as a replacement.

The [validate_replacement_model()](https://docs.datarobot.com/en/docs/api/reference/sdk/deployment-management.html#datarobot.models.Deployment.validate_replacement_model) function returns the validation status, a message and a checks dictionary.
If the status is ‘passing’ or ‘warning’, use [replace_model()](https://docs.datarobot.com/en/docs/api/reference/sdk/deployment-management.html#datarobot.models.Deployment.replace_model) to perform model the replacement.
If status is ‘failing’, refer to the `checks` dict for more details on why the new model cannot be used as a replacement.

```
import datarobot as dr

project = dr.Project.get('5cc899abc191a20104ff446a')
model = project.get_models()[0]
deployment = dr.Deployment.get(deployment_id='5c939e08962d741e34f609f0')
status, message, checks = deployment.validate_replacement_model(new_model_id=model.id)
status
>>> 'passing'

# `checks` can be inspected for detail, showing two examples here:
checks['target']
>>> {'status': 'passing', 'message': 'Target is compatible.'}
checks['permission']
>>> {'status': 'passing', 'message': 'User has permission to replace model.'}
```

## Monitoring

Deployment monitoring can be categorized into several area of concerns:

- Service stats over time
- Accuracy over time

With a [Deployment](https://docs.datarobot.com/en/docs/api/reference/sdk/deployment-management.html#datarobot.models.Deployment) object, `get` functions are provided so that you can query monitoring data.
Alternatively, it is also possible to retrieve monitoring data directly using a deployment ID:

```
from datarobot.models import Deployment, ServiceStats

deployment_id = '5c939e08962d741e34f609f0'

# Call `get` functions on a `Deployment` object
deployment = Deployment.get(deployment_id)
service_stats = deployment.get_service_stats()

# Directly fetch without a `Deployment` object
service_stats = ServiceStats.get(deployment_id)
```

When querying monitoring data, you can optionally provide a start and end time (accepted as either a `datetime` object or a `string`).
Note that only top of the hour datetimes are accepted. For example: `2019-08-01T00:00:00Z`.
By default, the end time of the query will be the next top of the hour, the start time will be 7 days before the end time.

In the over time variants, an optional `bucket_size` can be provided to specify the resolution of time buckets.
For example, if the start time is `2019-08-01T00:00:00Z`, the end time is `2019-08-02T00:00:00Z`, and the `bucket_size` is `T1H`, then 24 time buckets are generated, each providing data calculated over one hour.
Use [construct_duration_string()](https://docs.datarobot.com/en/docs/api/reference/sdk/projects.html#datarobot.helpers.partitioning_methods.construct_duration_string) to help construct a bucket size string.

> NOTE¶The minimum bucket size is one hour.

### Service stats

Service stats are metrics tracking deployment utilization and how well deployments respond to prediction requests.
Use `SERVICE_STAT_METRIC.ALL` to retrieve a list of supported metrics.

[ServiceStats](https://docs.datarobot.com/en/docs/api/reference/sdk/deployment-management.html#datarobot.models.deployment.ServiceStats) retrieves values for all service stats metrics.[ServiceStatsOverTime](https://docs.datarobot.com/en/docs/api/reference/sdk/deployment-management.html#datarobot.models.deployment.ServiceStatsOverTime) can be used to fetch how one single metric changes over time.

```
from datetime import datetime
from datarobot.enums import SERVICE_STAT_METRIC
from datarobot.helpers.partitioning_methods import construct_duration_string
from datarobot.models import Deployment

deployment = Deployment.get(deployment_id='5c939e08962d741e34f609f0')
service_stats = deployment.get_service_stats(
    start_time=datetime(2019, 8, 1, hour=15),
    end_time=datetime(2019, 8, 8, hour=15)
)
service_stats[SERVICE_STAT_METRIC.TOTAL_PREDICTIONS]
>>> 12597

total_predictions = deployment.get_service_stats_over_time(
    start_time=datetime(2019, 8, 1, hour=15),
    end_time=datetime(2019, 8, 8, hour=15),
    bucket_size=construct_duration_string(days=1),
    metric=SERVICE_STAT_METRIC.TOTAL_PREDICTIONS
)
total_predictions.bucket_values
>>> OrderedDict([(datetime.datetime(2019, 8, 1, 15, 0, tzinfo=tzutc()), 1610),
                 (datetime.datetime(2019, 8, 2, 15, 0, tzinfo=tzutc()), 2249),
                 (datetime.datetime(2019, 8, 3, 15, 0, tzinfo=tzutc()), 254),
                 (datetime.datetime(2019, 8, 4, 15, 0, tzinfo=tzutc()), 943),
                 (datetime.datetime(2019, 8, 5, 15, 0, tzinfo=tzutc()), 1967),
                 (datetime.datetime(2019, 8, 6, 15, 0, tzinfo=tzutc()), 2810),
                 (datetime.datetime(2019, 8, 7, 15, 0, tzinfo=tzutc()), 2775)])
```

### Data drift

Data drift measures how much the distribution of target or a feature has changed comparing to the training data.
Deployment’s target drift and feature drift can be retrieved separately using [datarobot.models.deployment.TargetDrift](https://docs.datarobot.com/en/docs/api/reference/sdk/deployment-management.html#datarobot.models.deployment.TargetDrift) and [datarobot.models.deployment.FeatureDrift](https://docs.datarobot.com/en/docs/api/reference/sdk/deployment-management.html#datarobot.models.deployment.FeatureDrift).
Use `DATA_DRIFT_METRIC.ALL` to retrieve a list of supported metrics.

```
from datetime import datetime
from datarobot.enums import DATA_DRIFT_METRIC
from datarobot.models import Deployment, FeatureDrift

deployment = Deployment.get(deployment_id='5c939e08962d741e34f609f0')
target_drift = deployment.get_target_drift(
    start_time=datetime(2019, 8, 1, hour=15),
    end_time=datetime(2019, 8, 8, hour=15)
)
target_drift.drift_score
>>> 0.00408514

feature_drift_data = FeatureDrift.list(
    deployment_id='5c939e08962d741e34f609f0',
    start_time=datetime(2019, 8, 1, hour=15),
    end_time=datetime(2019, 8, 8, hour=15),
    metric=DATA_DRIFT_METRIC.HELLINGER
)
feature_drift = feature_drift_data[0]
feature_drift.name
>>> 'age'
feature_drift.drift_score
>>> 4.16981594
```

#### Predictions over time

Predictions over time gives insight on how deployment’s prediction response has changed over time.
Different data can be retrieved in each bucket, depending on deployment’s target type:

- row_count : The number of rows in the bucket, available for all target types.
- mean_predicted_value : The average of the predicted values for all rows in the bucket. Available for regression target type.
- mean_probabilities : The mean of the predicted probability for each class. Available for binary or multiclass classification target types.
- class_distribution : The count and percent of the predicted class labels. Available for binary or multiclass classification target types.
- percentiles : The 10th and 90th percentile of a predicted value or positive class probability. Available for regression and binary target types.

```
from datetime import datetime
from datarobot.enums import BUCKET_SIZE
from datarobot.models import Deployment

# Deployment with regression target type
deployment = Deployment.get(deployment_id='5c939e08962d741e34f609f0')
predictions_over_time = deployment.get_predictions_over_time(
    start_time=datetime(2023, 4, 1),
    end_time=datetime(2023, 4, 30),
    bucket_size=BUCKET_SIZE.P1D,
)
predicted = [bucket['mean_predicted_value'] for bucket in predictions_over_time.buckets]
predicted
>>> [0.3772, 0.6642, ...., 0.7937]

# Deployment with binary target type
deployment = Deployment.get(deployment_id='62fff28a0f5fee488587ce92')
predictions_over_time = deployment.get_predictions_over_time(
    start_time=datetime(2023, 4, 1),
    end_time=datetime(2023, 4, 22),
    bucket_size=BUCKET_SIZE.P7D,
)
predicted = [
    {item['class_name']: item['value'] for item in bucket['mean_probabilities']}.get('True')
    for bucket in predictions_over_time.buckets
]
predicted
>>> [0.3955, 0.4274, None]
```

### Accuracy

A collection of metrics are provided to measure the accuracy of a deployment’s predictions.
For deployed classification models, use `ACCURACY_METRIC.ALL_CLASSIFICATION` for all supported metrics;
for deployed regression models, use `ACCURACY_METRIC.ALL_REGRESSION` instead.

[Accuracy](https://docs.datarobot.com/en/docs/api/reference/sdk/deployment-management.html#datarobot.models.deployment.Accuracy) and [AccuracyOverTime](https://docs.datarobot.com/en/docs/api/reference/sdk/deployment-management.html#datarobot.models.deployment.AccuracyOverTime) are provided to retrieve all default accuracy metrics and measure how one single metric changes over time.

```
from datetime import datetime
from datarobot.enums import ACCURACY_METRIC
from datarobot.helpers.partitioning_methods import construct_duration_string
from datarobot.models import Deployment

deployment = Deployment.get(deployment_id='5c939e08962d741e34f609f0')
accuracy = deployment.get_accuracy(
    start_time=datetime(2019, 8, 1, hour=15),
    end_time=datetime(2019, 8, 1, 15, 0)
)
accuracy[ACCURACY_METRIC.RMSE]
>>> 943.225

rmse = deployment.get_accuracy_over_time(
    start_time=datetime(2019, 8, 1),
    end_time=datetime(2019, 8, 3),
    bucket_size=construct_duration_string(days=1),
    metric=ACCURACY_METRIC.RMSE
)
rmse.bucket_values
>>> OrderedDict([(datetime.datetime(2019, 8, 1, 15, 0, tzinfo=tzutc()), 1777.190657),
                 (datetime.datetime(2019, 8, 2, 15, 0, tzinfo=tzutc()), 1613.140772)])
```

It is also possible to retrieve how multiple metrics changed over the same period of time, enabling easier side-by-side comparison across different metrics.

```
from datarobot.enums import ACCURACY_METRIC
from datarobot.models import Deployment

accuracy_over_time = AccuracyOverTime.get_as_dataframe(
    ram_app.id, [ACCURACY_METRIC.RMSE, ACCURACY_METRIC.GAMMA_DEVIANCE, ACCURACY_METRIC.MAD])
```

#### Predictions vs. actuals over time

Predictions vs. actuals over time can be used to analyze how deployment’s predictions compare against actuals.
Different data can be retrieved in each bucket, depending on deployment’s target type:

- row_count_total : The number of rows with or without actuals in the bucket. Available for all target types.
- row_count_with_actual : The number of rows with actuals in the bucket. Available for all target types.
- mean_predicted_value : The mean of the predicted value for all rows matched with an actual in the bucket. Available for the regression target type.
- mean_actual_value : The mean of the actual value for all rows in the bucket. Available for the regression target type.
- predicted_class_distribution : The count and percent of predicted class labels. Available for binary and multiclass classification target types.
- actual_class_distribution : The count and percent of actual class labels. Available for binary or multiclass classification target types.

```
from datetime import datetime
from datarobot.enums import BUCKET_SIZE
from datarobot.models import Deployment

# Deployment with the regression target type
deployment = Deployment.get(deployment_id='5c939e08962d741e34f609f0')
predictions_over_time = deployment.get_predictions_vs_actuals_over_time(
    start_time=datetime(2023, 4, 1),
    end_time=datetime(2023, 4, 30),
    bucket_size=BUCKET_SIZE.P1D,
)
predicted = [bucket['mean_actual_value'] for bucket in predictions_over_time.buckets]
predicted
>>> [0.2806, 0.9170, ...., 0.0314]

# Deployment with the binary target type
deployment = Deployment.get(deployment_id='62fff28a0f5fee488587ce92')
predictions_over_time = deployment.get_predictions_vs_actuals_over_time(
    start_time=datetime(2023, 4, 1),
    end_time=datetime(2023, 4, 22),
    bucket_size=BUCKET_SIZE.P7D,
)
predicted = [
    {item['class_name']: item['value'] for item in bucket['mean_predicted_value']}.get('True')
    for bucket in predictions_over_time.buckets
]
predicted
>>> [0.5822, 0.6305, None]
```

### Delete data

Monitoring data accumulated on a deployment can be deleted using [delete_monitoring_data()](https://docs.datarobot.com/en/docs/api/reference/sdk/deployment-management.html#datarobot.models.Deployment.delete_monitoring_data).
A start and end timestamp could be provided to limit data deletion to certain time period.

#### WARNING

Monitoring data is not recoverable once deleted.

```
import datarobot as dr

deployment = dr.Deployment.get(deployment_id='5c939e08962d741e34f609f0')
deployment.delete_monitoring_data(model_id=deployment.model['id'])
```

### List deployment prediction data exports

Prediction data exports for a deployment can be retrieved using [list_prediction_data_exports()](https://docs.datarobot.com/en/docs/api/reference/sdk/deployment-management.html#datarobot.models.Deployment.list_prediction_data_exports).

```
from datarobot.enums import ExportStatus
from datarobot.models import Deployment

deployment = Deployment.get(deployment_id='5c939e08962d741e34f609f0')

prediction_data_exports = deployment.list_prediction_data_exports(limit=0)

prediction_data_exports
>>> [PredictionDataExport('65fbe59aaa3f847bd5acc75b'),
     PredictionDataExport('65fbe59aaa3f847bd5acc75c'),
     PredictionDataExport('65fbe59aaa3f847bd5acc75a')]
```

To list all prediction data exports, set the limit to 0.

Adjust additional parameters to filter the data as needed:

```
from datarobot.enums import ExportStatus
from datarobot.models import Deployment

deployment = Deployment.get(deployment_id='5c939e08962d741e34f609f0')

prediction_data_exports = deployment.list_prediction_data_exports(
    model_id="6444482e5583f6ee2e572265",
    batch=False,
    status=ExportStatus.SUCCEEDED,
    limit=100,
    offset=50,
)
```

### List deployment actuals data exports

Actuals data exports for a deployment can be retrieved using [list_actuals_data_exports()](https://docs.datarobot.com/en/docs/api/reference/sdk/deployment-management.html#datarobot.models.Deployment.list_actuals_data_exports).

```
from datarobot.enums import ExportStatus
from datarobot.models import Deployment

deployment = Deployment.get(deployment_id='5c939e08962d741e34f609f0')

actuals_data_exports = deployment.list_actuals_data_exports(limit=0)

actuals_data_exports
>>> [ActualsDataExport('660456a332d0081029ee5031'),
     ActualsDataExport('660456a332d0081029ee5032'),
     ActualsDataExport('660456a332d0081029ee5033')]
```

To list all actuals data exports, set the limit to `0`.

Adjust additional parameters to filter the data as needed:

```
from datarobot.enums import ExportStatus
from datarobot.models import Deployment

deployment = Deployment.get(deployment_id='5c939e08962d741e34f609f0')

 actuals_data_exports = deployment.list_actuals_data_exports(
    deployment_id='5c939e08962d741e34f609f0',
    offset=500,
    limit=50,
    status=ExportStatus.SUCCEEDED
)
```

### List deployment training data exports

To retrieve successful training data exports for a deployment, use [list_training_data_exports()](https://docs.datarobot.com/en/docs/api/reference/sdk/deployment-management.html#datarobot.models.Deployment.list_training_data_exports).

```
from datarobot.models.deployment import TrainingDataExport

training_data_exports = TrainingDataExport.list(deployment_id='5c939e08962d741e34f609f0')

training_data_exports
>>> [TrainingDataExport('6565fbf2356124f1daa3acc522')]
```

### List deployment data quality exports

To retrieve successful data quality exports for a deployment, use [list_data_quality_exports()](https://docs.datarobot.com/en/docs/api/reference/sdk/deployment-management.html#datarobot.models.Deployment.list_data_quality_exports).

```
from datarobot.models import Deployment

deployment = Deployment.get('66903c40f18e6ec90fd7c8c7')
data_quality_exports = deployment.list_data_quality_exports(start='2024-07-01', end='2024-08-01')

data_quality_exports
>>> [DataQualityExport(6447ca39c6a04df6b5b0ed19c6101e3c),
 ...
 DataQualityExport(0ff46fd3636545a9ac3e15ee1dbd8638)]
```

There are many filtering and sorting options available.

### Segment analysis

Segment analysis is a deployment utility that filters service stats, data drift, and accuracy statistics into unique segment attributes and values.

Use [get_segment_attributes()](https://docs.datarobot.com/en/docs/api/reference/sdk/deployment-management.html#datarobot.models.Deployment.get_segment_attributes) to retrieve segment analysis data.
Use [get_segment_values()](https://docs.datarobot.com/en/docs/api/reference/sdk/deployment-management.html#datarobot.models.Deployment.get_segment_values) to retrieve segment value data.

```
import datarobot as dr
deployment = dr.Deployment.get(deployment_id='5c939e08962d741e34f609f0')
segment_attributes_service_health = deployment.get_segment_attributes(DEPLOYMENT_MONITORING_TYPE.SERVICE_HEALTH)
>>>['DataRobot-Consumer', 'DataRobot-Host-IP', 'DataRobot-Remote-IP']
segment_attributes_data_drift = deployment.get_segment_attributes(DEPLOYMENT_MONITORING_TYPE.DATA_DRIFT)
>>>['DataRobot-Consumer', 'attribute_1', 'attribute_2']
segment_values = deployment.get_segment_values(segmentAttribute=ReservedSegmentAttributes.CONSUMER)
>>>['DataRobot-Consumer', 'datarobotuser@email.com']
```

## Challengers

Challenger models can be used to compare the currently deployed model (the “champion” model) to another model.

The following functions can be used to manage deployment’s challenger models:

- List: list_challengers() or list() .
- Create: create() .
- Get: get() .
- Update: update() .
- Delete: delete() .

```
import datarobot as dr

deployment = dr.Deployment.get(deployment_id='5c939e08962d741e34f609f0')
challenger = deployment.list_challengers()[-1]
challenger.update(name='New Challenger Name')
challenger.name
>>> 'New Challenger Name'
```

### Settings

Use [get_challenger_models_settings()](https://docs.datarobot.com/en/docs/api/reference/sdk/deployment-management.html#datarobot.models.Deployment.get_challenger_models_settings) and [update_challenger_models_settings()](https://docs.datarobot.com/en/docs/api/reference/sdk/deployment-management.html#datarobot.models.Deployment.update_challenger_models_settings) to retrieve and update challenger model settings.

```
import datarobot as dr

deployment = dr.Deployment.get(deployment_id='5c939e08962d741e34f609f0')
deployment.update_challenger_models_settings(challenger_models_enabled=True)
settings = deployment.get_challenger_models_settings()
settings
>>> {'enabled': True}
```

Use [get_challenger_replay_settings()](https://docs.datarobot.com/en/docs/api/reference/sdk/deployment-management.html#datarobot.models.Deployment.get_challenger_replay_settings) and [update_challenger_replay_settings()](https://docs.datarobot.com/en/docs/api/reference/sdk/deployment-management.html#datarobot.models.Deployment.update_challenger_replay_settings) to retrieve and update challenger replay settings.

```
import datarobot as dr

deployment = dr.Deployment.get(deployment_id='5c939e08962d741e34f609f0')
deployment.update_challenger_replay_settings(enabled=True)
settings = deployment.get_challenger_replay_settings()
settings['enabled']
>>> True
```

## Settings

Review the sections below to learn how to manage a deployment’s settings.

### Drift tracking settings

Drift tracking is used to help analyze and monitor the performance of a model after it is deployed.
When the model of a deployment is replaced drift tracking status will not be altered.

Use [get_drift_tracking_settings()](https://docs.datarobot.com/en/docs/api/reference/sdk/deployment-management.html#datarobot.models.Deployment.get_drift_tracking_settings) to retrieve the current tracking status for target drift and feature drift.

```
import datarobot as dr

deployment = dr.Deployment.get(deployment_id='5c939e08962d741e34f609f0')
settings = deployment.get_drift_tracking_settings()
settings
>>> {'target_drift': {'enabled': True}, 'feature_drift': {'enabled': True}}
```

Use [update_drift_tracking_settings()](https://docs.datarobot.com/en/docs/api/reference/sdk/deployment-management.html#datarobot.models.Deployment.update_drift_tracking_settings) to update target drift and feature drift tracking status.

```
import datarobot as dr

deployment = dr.Deployment.get(deployment_id='5c939e08962d741e34f609f0')
deployment.update_drift_tracking_settings(target_drift_enabled=True, feature_drift_enabled=True)
```

### Association ID settings

Association ID is used to identify predictions, so that when actuals are acquired, accuracy can be calculated.

Use [get_association_id_settings()](https://docs.datarobot.com/en/docs/api/reference/sdk/deployment-management.html#datarobot.models.Deployment.get_association_id_settings) to retrieve current association ID settings.

```
import datarobot as dr

deployment = dr.Deployment.get(deployment_id='5c939e08962d741e34f609f0')
settings = deployment.get_association_id_settings()
settings
>>> {'column_names': ['application_id'], 'required_in_prediction_requests': True}
```

Use [update_association_id_settings()](https://docs.datarobot.com/en/docs/api/reference/sdk/deployment-management.html#datarobot.models.Deployment.update_association_id_settings) to update association ID settings.

```
import datarobot as dr

deployment = dr.Deployment.get(deployment_id='5c939e08962d741e34f609f0')
deployment.update_association_id_settings(column_names=['application_id'], required_in_prediction_requests=True)
```

### Predictions by forecast date

Forecast date setting for the deployment.

Use [get_predictions_by_forecast_date_settings()](https://docs.datarobot.com/en/docs/api/reference/sdk/deployment-management.html#datarobot.models.Deployment.get_predictions_by_forecast_date_settings) to retrieve current predictions by forecast date settings.

```
import datarobot as dr

deployment = dr.Deployment.get(deployment_id='5c939e08962d741e34f609f0')
settings = deployment.get_predictions_by_forecast_date_settings()
settings
>>> {'enabled': False, 'column_name': 'date (actual)', 'datetime_format': '%Y-%m-%d'}
```

Use [update_predictions_by_forecast_date_settings()](https://docs.datarobot.com/en/docs/api/reference/sdk/deployment-management.html#datarobot.models.Deployment.update_predictions_by_forecast_date_settings) to update predictions by forecast date settings.

```
import datarobot as dr

deployment = dr.Deployment.get(deployment_id='5c939e08962d741e34f609f0')
deployment.update_predictions_by_forecast_date_settings(
    enable_predictions_by_forecast_date=True,
    forecast_date_column_name='date (actual)',
    forecast_date_format='%Y-%m-%d')
```

### Health settings

Health settings APIs can be used to customize definitions for deployment health status.

Use [get_health_settings()](https://docs.datarobot.com/en/docs/api/reference/sdk/deployment-management.html#datarobot.models.Deployment.get_health_settings) to retrieve current health settings, and [get_default_health_settings()](https://docs.datarobot.com/en/docs/api/reference/sdk/deployment-management.html#datarobot.models.Deployment.get_default_health_settings) to retrieve default health settings.
To perform updates, use [update_health_settings()](https://docs.datarobot.com/en/docs/api/reference/sdk/deployment-management.html#datarobot.models.Deployment.update_health_settings).

```
import datarobot as dr

# Get current data drift threshold
deployment = dr.Deployment.get(deployment_id='5c939e08962d741e34f609f0')
settings = deployment.get_health_settings()
settings['data_drift']['drift_threshold']
>>> 0.15

# Update accuracy health metric
settings['accuracy']['metric'] = 'AUC'
settings = deployment.update_health_settings(accuracy=settings['accuracy'])
settings['accuracy']['metric']
>>> 'AUC'

# Set accuracy health metric to default
default_settings = deployment.get_default_health_settings()
settings = deployment.update_health_settings(accuracy=default_settings['accuracy'])
settings['accuracy']['metric']
>>> 'LogLoss'
```

### Segment analysis settings

Segment analysis is a deployment utility that filters data drift and accuracy statistics into unique segment attributes and values.

Use [get_segment_analysis_settings()](https://docs.datarobot.com/en/docs/api/reference/sdk/deployment-management.html#datarobot.models.Deployment.get_segment_analysis_settings) to retrieve current segment analysis settings.

```
import datarobot as dr

deployment = dr.Deployment.get(deployment_id='5c939e08962d741e34f609f0')
settings = deployment.get_segment_analysis_settings()
settings
>>> {'enabled': False, 'attributes': []}
```

Use [update_segment_analysis_settings()](https://docs.datarobot.com/en/docs/api/reference/sdk/deployment-management.html#datarobot.models.Deployment.update_segment_analysis_settings) to update segment analysis settings. Any categorical column can be a segment attribute.

```
import datarobot as dr

deployment = dr.Deployment.get(deployment_id='5c939e08962d741e34f609f0')
deployment.update_segment_analysis_settings(
    segment_analysis_enabled=True,
    segment_analysis_attributes=["country_code", "is_customer"])
```

### Predictions data collection settings

Predictions data collection configures whether prediction requests and results should be saved to predictions data storage.

Use [get_predictions_data_collection_settings()](https://docs.datarobot.com/en/docs/api/reference/sdk/deployment-management.html#datarobot.models.Deployment.get_predictions_data_collection_settings) to retrieve current settings of predictions data collection.

```
import datarobot as dr

deployment = dr.Deployment.get(deployment_id='5c939e08962d741e34f609f0')
settings = deployment.get_predictions_data_collection_settings()
settings
>>> {'enabled': True}
```

Use [update_predictions_data_collection_settings()](https://docs.datarobot.com/en/docs/api/reference/sdk/deployment-management.html#datarobot.models.Deployment.update_predictions_data_collection_settings) to update predictions data collection settings.

```
import datarobot as dr

deployment = dr.Deployment.get(deployment_id='5c939e08962d741e34f609f0')
deployment.update_predictions_data_collection_settings(enabled=True)
```

### Prediction warning settings

Prediction warning is used to enable Humble AI for a deployment which determines if a model is misbehaving when a prediction goes outside of the calculated boundaries.

Use [get_prediction_warning_settings()](https://docs.datarobot.com/en/docs/api/reference/sdk/deployment-management.html#datarobot.models.Deployment.get_prediction_warning_settings) to retrieve the current prediction warning settings.

```
import datarobot as dr

deployment = dr.Deployment.get(deployment_id='5c939e08962d741e34f609f0')
settings = deployment.get_prediction_warning_settings()
settings
>>> { {'enabled': True}, 'custom_boundaries': {'upper': 1337, 'lower': 0} }
```

Use [update_prediction_warning_settings()](https://docs.datarobot.com/en/docs/api/reference/sdk/deployment-management.html#datarobot.models.Deployment.update_prediction_warning_settings) to update current prediction warning settings.

```
import datarobot as dr

# Set custom boundaries
deployment = dr.Deployment.get(deployment_id='5c939e08962d741e34f609f0')
deployment.update_prediction_warning_settings(
    prediction_warning_enabled=True,
    use_default_boundaries=False,
    lower_boundary=1337,
    upper_boundary=2000,
)

# Reset boundaries
deployment.update_prediction_warning_settings(
    prediction_warning_enabled=True,
    use_default_boundaries=True,
)
```

### Secondary dataset configuration settings

The secondary dataset configuration for a deployed Feature Discovery model can be replaced and retrieved.

Secondary dataset configuration is used to specify which secondary datasets to use during prediction for a given deployment.

Use [update_secondary_dataset_config()](https://docs.datarobot.com/en/docs/api/reference/sdk/deployment-management.html#datarobot.models.Deployment.update_secondary_dataset_config) to update the secondary dataset configuration.

```
import datarobot as dr

deployment = dr.Deployment.get(deployment_id='5c939e08962d741e34f609f0')
config = deployment.update_secondary_dataset_config(secondary_dataset_config_id='5f48cb94408673683eca0fab')
config
>>> '5f48cb94408673683eca0fab'
```

Use [get_secondary_dataset_config()](https://docs.datarobot.com/en/docs/api/reference/sdk/deployment-management.html#datarobot.models.Deployment.get_secondary_dataset_config) to get the secondary dataset config.

```
import datarobot as dr

deployment = dr.Deployment.get(deployment_id='5c939e08962d741e34f609f0')
config = deployment.get_secondary_dataset_config()
config
>>> '5f48cb94408673683eca0fab'
```

### Share deployments

You can grant or revoke other users’ access to a deployment.

#### Access levels

For deployments, there are 3 access levels:

`OWNER` - Allows all actions on a deployment.

`USER` - Can see the deployment in the DataRobot UI and see the prediction statistics of the deployment, but cannot edit or delete the deployment.

`CONSUMER` - Can only make predictions on the deployment. Cannot see the deployment in the DataRobot UI or retrieve prediction statistics for the deployment in the API.

#### Sharing

Use [list_shared_roles()](https://docs.datarobot.com/en/docs/api/reference/sdk/deployment-management.html#datarobot.models.Deployment.list_shared_roles) to get a list of users, groups, and organizations that currently have a role on the project. Each role will be returned as a [datarobot.models.deployment.DeploymentSharedRole](https://docs.datarobot.com/en/docs/api/reference/sdk/deployment-management.html#datarobot.models.deployment.DeploymentSharedRole).

```
import datarobot as dr

deployment = dr.Deployment.get(deployment_id='5c939e08962d741e34f609f0')
roles = deployment.list_shared_roles()
[role.to_dict() for role in roles]
>>> [{'role': 'OWNER', 'id': '5c939e08962d741e34f609f0', 'share_recipient_type': 'user', 'name': 'user@datarobot.com'},
 {'role': 'USER', 'id': '5c939e08962d741e34f609f1', 'share_recipient_type': 'group', 'name': 'Example Group'},
 {'role': 'CONSUMER', 'id': '5c939e08962d741e34f609f2', 'share_recipient_type': 'organization', 'name': 'Example Org'}]
```

Use [update_shared_roles()](https://docs.datarobot.com/en/docs/api/reference/sdk/deployment-management.html#datarobot.models.Deployment.update_shared_roles) to grant and revoke roles on the deployment.
This function takes a list of [datarobot.models.deployment.DeploymentGrantSharedRoleWithId](https://docs.datarobot.com/en/docs/api/reference/sdk/deployment-management.html#datarobot.models.deployment.DeploymentGrantSharedRoleWithId) and [datarobot.models.deployment.DeploymentGrantSharedRoleWithUsername](https://docs.datarobot.com/en/docs/api/reference/sdk/deployment-management.html#datarobot.models.deployment.DeploymentGrantSharedRoleWithUsername) objects and updates roles accordingly.

```
import datarobot as dr

deployment = dr.Deployment.get(deployment_id='5c939e08962d741e34f609f0')
roles = deployment.list_shared_roles()
[role.to_dict() for role in roles]
>>> [{'role': 'OWNER', 'id': '5c939e08962d741e34f609f0', 'share_recipient_type': 'user', 'name': 'user@datarobot.com'}]

new_role = DeploymentGrantSharedRoleWithUsername(username='user_2@datarobot.com', role='OWNER')
response = deployment.update_shared_roles([new_role])
response.status_code
>>> 204

roles = deployment.list_shared_roles()
[role.to_dict() for role in roles]
>>> [{'role': 'OWNER', 'id': '5c939e08962d741e34f609f0', 'share_recipient_type': 'user', 'name': 'user@datarobot.com'},
  {'role': 'OWNER', 'id': '5c939e08962d741e34f609f1', 'share_recipient_type': 'user', 'name': 'user_2@datarobot.com'}]

revoke_role =  DeploymentGrantSharedRoleWithUsername(username='user_2@datarobot.com', role='NO_ROLE')
response = deployment.update_shared_roles([revoke_role])
response.status_code
>>> 204

roles = deployment.list_shared_roles()
[role.to_dict() for role in roles]
>>> [{'role': 'OWNER', 'id': '5c939e08962d741e34f609f0', 'share_recipient_type': 'user', 'name': 'user@datarobot.com'}]
```

---
