Validate before deploying to Model Serving

Validate a model offline with MLflow before you deploy it to Model Serving, so you catch dependency and input problems first. This page covers predictions, input payloads, dependency updates, and debugging before you create an endpoint.

Requirements

Test predictions before deployment

Azure Databricks recommends starting with this check. It runs your model in a virtual environment that simulates serving. To run the check, set run_id to your run ID and run the following:

import mlflow

run_id = "..."
model_uri = f"runs:/{run_id}/model"

mlflow.models.predict(
  model_uri=model_uri,
  input_data={"col1": 34.2, "col2": 11.2, "col3": "green"},
  content_type="json",
  env_manager="virtualenv",
  install_mlflow=False,
  pip_requirements_override=["pillow==10.3.0", "scipy==1.13.0"],
)

The predictions print to stdout, or to the file that output_path names.

With the default env_manager="virtualenv", MLflow rebuilds your model's logged dependencies in an isolated environment that simulates the serving runtime, so a dependency that is missing, conflicts with another package, or fails to install surfaces here instead of during endpoint deployment.

The MLflow CLI runs the same check. For its syntax, see MLflow CLI for predictions.

You can specify the following parameters:

  • The model_uri of the model that is deployed to Model Serving.
  • One of the following:
    • The input_data in the expected format for the mlflow.pyfunc.PyFuncModel.predict() call of the model.
    • The input_path that defines a file containing input data that is loaded and used for the call to predict.
  • The content_type in csv or json format.
  • An optional output_path to write the predictions to a file. If you omit this parameter, the predictions are printed to stdout.
  • An environment manager, env_manager, that is used to build the environment for serving:
    • The default is virtualenv. Recommended for serving validation.
    • local is available, but potentially error prone for serving validation. Generally used only for rapid debugging.
  • Whether to install the current version of MLflow that is in your environment with the virtual environment using install_mlflow. This setting defaults to False.
  • Whether to update and test different versions of package dependencies for troubleshooting or debugging. You can specify this as a list of string dependency overrides or additions using the override argument, pip_requirements_override.

Update model dependencies

If a logged model has a dependency problem, update its requirements in place instead of logging another model. To update them with Python, set run_id to your run ID and run the following:

from mlflow.models import update_model_requirements

run_id = "..."
model_uri = f"runs:/{run_id}/model"

update_model_requirements(
  model_uri=model_uri,
  operation="add",
  requirement_list=["pillow==10.2.0", "scipy==1.12.0"],
)

You can update existing definitions with specified package versions or add non-existent requirements. The requirements.txt file is in the MLflow model artifact at model_uri, and it lists pillow==10.2.0 and scipy==1.12.0 after the update.

To update the requirements with the MLflow CLI instead, see MLflow CLI for updating pip requirements.

Validate the model input before deployment

Model Serving endpoints expect a special format of JSON input. You can validate that your model input works on a serving endpoint before deployment using validate_serving_input in MLflow.

If your model was logged with a valid input example, the run's artifacts tab shows auto-generated code like the following. To validate a serving payload, replace the model_uri placeholder and run the following:

from mlflow.models import validate_serving_input

model_uri = 'runs:/<run_id>/<artifact_path>'

serving_payload = """{
 "messages": [
   {
     "content": "How many product categories are there?",
     "role": "user"
   }
 ]
}
"""

# Validate the serving payload works on the model
validate_serving_input(model_uri, serving_payload)

When the payload is valid, the call returns the prediction result from the model.

To test an input example, replace the model_uri placeholder and the INPUT_EXAMPLE value, then run the following:

from mlflow.models import validate_serving_input
from mlflow.models import convert_input_example_to_serving_input

model_uri = 'runs:/<run_id>/<artifact_path>'

# Replace this value with the input example that you logged with the model.
# A valid input example is a data instance suitable for pyfunc prediction
INPUT_EXAMPLE = {"messages": [{"role": "user", "content": "How many product categories are there?"}]}

serving_payload = convert_input_example_to_serving_input(INPUT_EXAMPLE)

# Validate the serving payload works on the model
validate_serving_input(model_uri, serving_payload)

convert_input_example_to_serving_input returns the serving input as a JSON string, and validate_serving_input checks it against the model.

Debug the model outside the endpoint

To load and debug the model before it serves, start in a notebook on Databricks Runtime. You can also load the model on your computer.

Attach a notebook to a cluster

Open a notebook and attach to an All-Purpose cluster that uses a Databricks Runtime version, not Databricks Runtime for Machine Learning.

The cells in the notebook run on the attached cluster.

Load the model with MLflow

To load the model, set run_id to your run ID and run the following:

import mlflow

run_id = "..."
model_uri = f"runs:/{run_id}/model"

model = mlflow.pyfunc.load_model(model_uri)

The call returns a PyFuncModel object, and you can call model.predict() on it.

Load the model on your computer

This path needs conda and an Azure Databricks configuration profile on your computer. To load the model on your computer, replace PROFILE with the name of an Azure Databricks configuration profile, and set run_id to your run ID. Then run the following:

import os
import mlflow

os.environ["MLFLOW_TRACKING_URI"] = "databricks://PROFILE"

run_id = "..."
model_uri = f"runs:/{run_id}/model"
ARTIFACT_URI = model_uri
if '.' in ARTIFACT_URI:
    mlflow.set_registry_uri('databricks-uc')
local_path = mlflow.artifacts.download_artifacts(ARTIFACT_URI)
print(local_path)

The print call shows the path to a local directory that holds the model files.

If you logged the model with your own conda_env, use the environment name that its conda.yaml sets in place of mlflow-env in the steps below.

Replace <local-path> with the path that the print call showed, and then run the following in a shell:

conda env create -f <local-path>/conda.yaml
conda activate mlflow-env

In Python, in the environment that you activated, load the model from that path:

import mlflow

local_path = "<local-path>"
model = mlflow.pyfunc.load_model(local_path)

The call returns a PyFuncModel object.

Additional resources

Resource Description
Debugging guide for Model Serving Find the cause and fix for failed builds and request errors.
Create an endpoint for custom models in Model Serving Create an endpoint that serves a custom model.