---
title: Add Python libraries to Model Serving
description: Add custom Python libraries to a Model Serving model deployment, from a private package repository or wheel files packaged with the model.
canonical_url: https://docs.databricks.com/aws/en/machine-learning/model-serving/private-libraries-model-serving
---

# Add Python libraries to Model Serving

Add custom Python libraries to a model so Model Serving can install them when it builds your endpoint. This page covers two ways to do it: a private package repository for the workspace, and wheel files packaged with the model.

Custom libraries often hold pre- or post-processing functions, custom model definitions, and shared utilities. Many enterprise security teams use private PyPI mirrors, such as Nexus or Artifactory, to reduce the risk of [supply-chain attacks](https://wikipedia.org/wiki/Supply_chain_attack). Databricks offers [native support](https://docs.databricks.com/aws/en/libraries/index.md) for installing custom libraries and libraries from a private mirror in the Databricks workspace. Complete these steps after you train the model and before you create an [endpoint](https://docs.databricks.com/aws/en/machine-learning/model-serving/create-manage-serving-endpoints.md).

## Requirements

* [MLflow](https://mlflow.org/docs/latest/python_api/mlflow.models.html) 1.29 or above.
* MLflow 3 or above, for the `name` argument of [`log_model()`](https://mlflow.org/docs/latest/python_api/mlflow.pyfunc.html#mlflow.pyfunc.log_model) in the logging example.
* A [custom model](https://docs.databricks.com/aws/en/machine-learning/model-serving/custom-models.md) ready to log with MLflow.

- Restrict outbound network access from Model Serving endpoints by configuring network policies. See [Validate with model serving](https://docs.databricks.com/aws/en/security/network/serverless-network-security/manage-network-policies.md#model-serving).

## Use a private package repository

Use this option if your organization uses a private PyPI mirror, such as Nexus or Artifactory. Workspace admins can configure it as the default package repository for the workspace. Model Serving automatically uses this workspace-level configuration when building your model environment.

To set up a private package repository, see [Configure default package repositories](https://docs.databricks.com/aws/en/admin/workspace-settings/default-package-repositories.md). After you configure it, log your model and proceed to [Serve the model version](https://docs.databricks.com/aws/en/machine-learning/model-serving/private-libraries-model-serving.md#serve-the-model-version).

## Package wheel files with the model

Use this option if a private PyPI mirror isn't accessible, or if your libraries aren't in any package repository. Package them as Python wheel files and include them when you log the model.

### Upload the wheel file and install it in your notebook

Databricks recommends that you upload your dependency file to Unity Catalog [volumes](https://docs.databricks.com/aws/en/volumes/index.md). Using `%pip` installs the library in the current notebook and downloads the dependency to the cluster. To install the wheel file, run the following:

```bash
%pip install /path/to/dependency.whl
```

The install finishes without errors.

### Log the model with the wheel file

Pass the wheel file in `code_paths`, and list it in `extra_pip_requirements` with the `code/` prefix. Don't list a volume path, such as `/Volumes/...`, in pip requirements. Volume paths aren't available while Model Serving builds the container, so the build fails. To log and register a pyfunc model, replace the registered model name with your own, and run the following:

```python
import mlflow

class MyModel(mlflow.pyfunc.PythonModel):
    def predict(self, context, model_input, params=None):
        return model_input

mlflow.pyfunc.log_model(
    name="model",
    python_model=MyModel(),
    registered_model_name="<your_catalog>.<your_schema>.<your_model>",
    code_paths=["/path/to/dependency.whl"], # This will be logged as `code/dependency.whl`
    extra_pip_requirements=["code/dependency.whl"],
)
```

The call logs the model and creates a model version under the registered model name.

If you have a custom library, you must specify all custom Python libraries associated with your model when you configure logging. You can do so with the `extra_pip_requirements` or `conda_env` parameters in [log\_model()](https://mlflow.org/docs/latest/python_api/mlflow.pyfunc.html#mlflow.pyfunc.log_model). For a scikit-learn model, call `mlflow.sklearn.log_model` with the same `code_paths` and `extra_pip_requirements` arguments.

### Optional: Package every dependency as wheel files

MLflow provides the [add\_libraries\_to\_model()](https://mlflow.org/docs/latest/python_api/mlflow.models.html#mlflow.models.add_libraries_to_model) utility to log your model with all of its dependencies pre-packaged as Python wheel files. This packages your custom libraries alongside the model in addition to *all* other libraries that are specified as dependencies of your model. As a result, your model uses the same libraries that are accessible from your training environment.

Replace `<model-uri>` with a model URI. For a model in Unity Catalog, use `models:/<uc-model>/<model-version>`. For the workspace model registry (legacy), use `models:/<model-name>/<model-version>`. To add the libraries, run the following:

```python
import mlflow.models
mlflow.models.add_libraries_to_model("<model-uri>")
```

When you use the model registry URI, this utility generates a new version under your existing registered model.

## <a id="serve-the-model-version" />Serve the model version

When a new model version with the packages included is available in the model registry, add this model version to an endpoint with [Model Serving](https://docs.databricks.com/aws/en/machine-learning/model-serving/index.md).

## Troubleshooting

### Check the build logs

If your model deployment fails during the build phase, review the build logs to identify package installation issues.

1. Open the **Serving** page in your Databricks workspace.
2. Select your endpoint name to open the endpoint details.
3. Select the **Logs** tab.
4. Select the failed version from the drop-down menu.
5. Select **Build logs**.

Review the error messages to identify the issue.

After resolving the issue, create a new deployment or update your endpoint to trigger a new build.

### Fix private package repository issues

If you use a private package repository, common issues include:

* **Missing packages**: The package isn't available in your configured repository. Add the required package to your private repository.
* **Connection issues**: Model Serving can't reach your package repository. Verify network connectivity and firewall rules.
* **Authentication failures**: The credentials configured for your repository aren't valid or have expired. Update the secrets in your workspace configuration.

### Test packages in a notebook

Serverless notebooks use the same default package repository configured for your workspace. To test connectivity, authentication, and package availability before you deploy, install the requirements from your model's `requirements.txt` file. Set the variables at the top of the sample to your model details, and run the following:

```python
import mlflow
import subprocess
import sys

# Step 1: Set your model details
catalog = "<your_catalog>"
schema = "<your_schema>"
model_name = "<your_model>"
version = 1  # Replace 1 with your model version number

# Step 2: Download the model's requirements.txt
full_model_name = f"{catalog}.{schema}.{model_name}"
requirements_uri = f"models:/{full_model_name}/{version}/requirements.txt"

print(f"Downloading artifacts from: {requirements_uri}")
local_path = mlflow.artifacts.download_artifacts(requirements_uri)

# Step 3: Print the requirements
with open(local_path, "r") as f:
    print(f.read())

# Step 4: Install the requirements using the workspace's default package repository
print(f"Installing requirements from {local_path}...")
subprocess.check_call([sys.executable, "-m", "pip", "install", "-r", local_path])
print("Installation complete!")
```

The last line of output is `Installation complete!`.

## Additional resources

| Resource                                                                                                                              | Description                                                  |
| ------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------ |
| [Configure default package repositories](https://docs.databricks.com/aws/en/admin/workspace-settings/default-package-repositories.md) | Set a default package repository for your workspace.         |
| [Troubleshoot Model Serving endpoints](https://docs.databricks.com/aws/en/machine-learning/model-serving/model-serving-debug.md)      | Find the cause and fix for failed builds and request errors. |
