Skip to content

Create model version

This example demonstrates how you can use the MLOps Python client to register an experiment as a new version of a given registered model using the RegisteredModelVersionService of the MLOps API.

Before you begin

  • You will need the values for the following constants in order to successfully carry out the task. Contact your administrator to obtain deployment specific values.
MLOPS_API_URL Defines the URL for the MLOps gateway component. Usually:
TOKEN_ENDPOINT_URL Defines the token endpoint URL of the identity provider. This uses Keycloak as the identity provider. Keycloak realm should be provided.
REFRESH_TOKEN Defines the user's refresh token.
CLIENT_ID Sets the client id for authentication. This is the client you will be using to connect to MLOps.
CLIENT_SECRET Sets the client secret for authentication.
EXPERIMENT_ID Sets the id of the experiment which will be registered as a new version of the specified registered model.
REGISTERED_MODEL_ID Sets the registered model id.
REGISTERED_MODEL_NAME Sets the registered model name.


Only one of the REGISTERED_MODEL_ID and REGISTERED_MODEL_NAME should be provided when creating a model version. If the REGISTERED_MODEL_ID is provided, a new version of that model will be created. If REGISTERED_MODEL_NAME is provided, a new model and version will be created with the given name. This example uses REGISTERED_MODEL_ID to create a new version of an existing model.

The following steps demonstrate how you can use the MLOps Python client to create a registered model version in MLOps.

  1. Download the file.

  2. Change the values of the following constants in your file as given in the preceding data table.
    ### Constants
    MLOPS_API_URL = ""
    CLIENT_SECRET = "<your-client-secret>"
    EXPERIMENT_ID = "<your-experiment-id>"
    REGISTERED_MODEL_ID = "<your-registered-model-id>"
  3. Run the file.

  4. This registers the given experiment as new version of the registered model you have specified.

        "registeredModelVersion" : {
            "updatedTime" : "2000-01-23T04:56:07.000+00:00",
            "updatedBy" : "updatedBy",
            "createdBy" : "createdBy",
            "registeredModelId" : "registeredModelId",
            "createdTime" : "2000-01-23T04:56:07.000+00:00",
            "experimentId" : "experimentId",
            "id" : "id",
            "version" : 0

Example walkthrough

This section provides a walkthrough of the file.

  1. Register a new model version for the given registered model id by calling the CreateModelVersion endpoint of the RegisteredModelVersionService. The request for this API call is prepared by thecreate_model_version_request function, which specifies the registered model id and experiment id.


    If the model name and description are provided instead of the registered model id, a new registered model will be created with that name and description.
    def create_model_version(
        mlops_client: mlops.Client, 
        model_version_request: mlops.StorageCreateModelVersionRequest
    def create_model_version_request(registered_model_id, experiment_id):
        return mlops.StorageCreateModelVersionRequest(
  2. In the main function, set up the token provider using an existing refresh token and client secret, and then set up the MLOps client.

  3. Call the create_model_version with the REGISTERED_MODEL_ID and EXPERIMENT_ID constants defined in the beginning of the script.
    create_model_version_response: mlops.StorageCreateModelVersionResponse = create_model_version(