Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Give a Model Serving endpoint access to external and private resources with environment variables and secrets. This page covers plain text environment variables and secrets-based environment variables that reference Azure Databricks secrets.
Requirements
For secrets-based environment variables:
- The endpoint creator must have READ access to the Databricks secrets being referenced in the configs.
- You must store credentials like your API key or other tokens as a Databricks secret.
The samples use the Databricks SDK for Python and the Databricks CLI, installed and authenticated to your workspace. The samples require databricks-sdk 0.41.0 or above. To install or upgrade the SDK, run pip install --upgrade databricks-sdk. Each SDK sample creates an endpoint named example-add-model that serves version 2 of a model named main.default.addmodel. Change endpoint_name if that endpoint already exists, and change model_name and entity_version to a model you registered.
Add plain text environment variables
Use plain text environment variables for values that don't need to be hidden. You can set variables in the Serving UI, the REST API, or the SDK when you create or update an endpoint.
To add plain text environment variables with the Databricks SDK for Python, run the following:
from databricks.sdk import WorkspaceClient
from databricks.sdk.service.serving import ServedEntityInput, EndpointCoreConfigInput, ServingModelWorkloadType
w = WorkspaceClient()
endpoint_name = "example-add-model"
model_name = "main.default.addmodel"
w.serving_endpoints.create_and_wait(
name=endpoint_name,
config=EndpointCoreConfigInput(
name=endpoint_name,
served_entities=[
ServedEntityInput(
entity_name = model_name,
entity_version = "2",
workload_type = ServingModelWorkloadType("CPU"),
workload_size = "Small",
scale_to_zero_enabled = False,
environment_vars = {
"MY_ENV_VAR": "value_to_be_injected",
"ADS_TOKEN": "abcdefg-1234"
}
)
]
)
)
The create_and_wait call returns the endpoint details after creation finishes. On success, state.config_update is NOT_UPDATING and state.ready is READY.
From the Serving UI, you can add an environment variable in Advanced configuration:

To use the REST API, add the environment_vars field to each served entity in the config of a create endpoint request.
To use the MLflow Deployments client, pass the same environment_vars field in the config argument of create_endpoint. See the MLflow Deployments reference.
You can set up to 50 environment variables per served model. To increase the limit, reach out to your Databricks account team. For all limits, see Model Serving limits and regions.
Variable names can contain letters, digits, periods, hyphens, and underscores, but they can't start with a digit. Keys and values can't be empty.
Log feature lookup DataFrames to inference tables
If you have inference tables enabled on your endpoint, you can log your automatic feature lookup DataFrame to that inference table as part of the endpoint's MLflow trace.
Set ENABLE_MLFLOW_TRACING to true as an environment variable in the Serving UI, REST API, or SDK when you create or update the endpoint. Feature lookup DataFrames are included in the trace by default. To leave them out, set ENABLE_FEATURE_TRACING to false.
Logging feature lookup DataFrames requires the following:
- Log the model with MLflow 3.12.0 or above.
- For Databricks Online Feature Stores, use
databricks-feature-lookup1.12.0 or above. Models logged withfe.log_modelget the latest 1.x version when the endpoint is built.
The feature values are in the model_input input of the trace's custom_model span, which is stored under databricks_output.trace in the response column of the inference table. A response and its trace are logged up to 1 MiB in total, so keep batches small enough for the feature values to fit.
Serving UI
- In Advanced configuration, select + Add environment variables.
- Type
ENABLE_MLFLOW_TRACINGas the environment name. - In the field to the right, type
true.

REST API
The following is an example for creating a serving endpoint using the POST /api/2.0/serving-endpoints REST API and the environment_vars field to configure the ENABLE_MLFLOW_TRACING environment variable.
{
"name": "endpoint-name",
"config": {
"served_entities": [
{
"entity_name": "model-name",
"entity_version": "1",
"workload_size": "Small",
"scale_to_zero_enabled": "true",
"environment_vars": {
"ENABLE_MLFLOW_TRACING": "true"
}
}
]
}
}
WorkspaceClient SDK
The following is an example for creating a serving endpoint using the WorkspaceClient SDK and the environment_vars field to configure the ENABLE_MLFLOW_TRACING environment variable.
from databricks.sdk import WorkspaceClient
from databricks.sdk.service.serving import ServedEntityInput, EndpointCoreConfigInput, ServingModelWorkloadType
w = WorkspaceClient()
endpoint_name = "example-add-model"
model_name = "main.default.addmodel"
w.serving_endpoints.create_and_wait(
name=endpoint_name,
config=EndpointCoreConfigInput(
served_entities=[
ServedEntityInput(
entity_name = model_name,
entity_version = "2",
workload_type = ServingModelWorkloadType("CPU"),
workload_size = "Small",
scale_to_zero_enabled = False,
environment_vars = {
"ENABLE_MLFLOW_TRACING": "true"
}
)
]
)
)
MLflow Deployments SDK
The following is an example for creating a serving endpoint using the MLflow Deployments SDK and the environment_vars field to configure the ENABLE_MLFLOW_TRACING environment variable.
from mlflow.deployments import get_deploy_client
client = get_deploy_client("databricks")
endpoint = client.create_endpoint(
name="unity-catalog-model-endpoint",
config={
"served_entities": [
{
"name": "ads-entity",
"entity_name": "catalog.schema.my-ads-model",
"entity_version": "3",
"workload_size": "Small",
"scale_to_zero_enabled": True,
"environment_vars": {
"ENABLE_MLFLOW_TRACING": "true"
}
}
]
}
)
Add secrets-based environment variables
You can store credentials as Databricks secrets and reference those secrets from a model serving endpoint with secrets-based environment variables. This lets the endpoint fetch the credentials when the model is deployed.
For example, you can pass credentials to call OpenAI and other external model endpoints or access external data storage locations directly from model serving.
Databricks recommends this feature for deploying OpenAI and LangChain MLflow model flavors to serving. It also applies to other software as a service (SaaS) models that need credentials. The access pattern uses environment variables and API keys and tokens.
When the model is deployed, the secrets are retrieved from Databricks secrets by the secret scope and key. These get assigned to the secret environment variable names that can be used inside the model.
Create a secret scope
For the scope options, see Manage secret scopes. Run:
databricks secrets create-scope my_secret_scope
The command finishes without errors.
Add a secret to the scope
Add a secret to the scope under a key. Run:
databricks secrets put-secret my_secret_scope my_secret_key
The command finishes without errors.
You can then pass the secret reference and the environment variable name to your endpoint configuration. Do this during endpoint creation or as an update to an existing endpoint.
Reference the secret in the endpoint configuration
You can add the secret scope to an environment variable and pass that variable to your endpoint during endpoint creation or configuration updates. See Create an endpoint for custom models in Model Serving.
From the Serving UI, you can add an environment variable in Advanced configuration. The secrets-based environment variable must be provided using the following syntax: {{secrets/scope/key}}. Otherwise, the environment variable is considered a plain text environment variable. Write the reference exactly in that form.
The following example assigns the value from the secret to the environment variable OPENAI_API_KEY. It uses the Databricks SDK for Python:
from databricks.sdk import WorkspaceClient
from databricks.sdk.service.serving import ServedEntityInput, EndpointCoreConfigInput, ServingModelWorkloadType
w = WorkspaceClient()
endpoint_name = "example-add-model"
model_name = "main.default.addmodel"
w.serving_endpoints.create_and_wait(
name=endpoint_name,
config=EndpointCoreConfigInput(
name=endpoint_name,
served_entities=[
ServedEntityInput(
entity_name = model_name,
entity_version = "2",
workload_type = ServingModelWorkloadType("CPU"),
workload_size = "Small",
scale_to_zero_enabled = False,
environment_vars = {
"OPENAI_API_KEY": "{{secrets/my_secret_scope/my_secret_key}}"
}
)
]
)
)
The create_and_wait call returns the endpoint details after creation finishes. On success, state.config_update is NOT_UPDATING and state.ready is READY.
To use the REST API, add the environment_vars field with the same secret reference to each served entity in the config of a create endpoint request. To update an existing endpoint, use the update config request with the same field.
To use the MLflow Deployments client, pass the same environment_vars field in the config argument of create_endpoint. See the MLflow Deployments reference.
After the endpoint is created or updated, model serving automatically fetches the secret key from the Databricks secrets scope and populates the environment variable for your model inference code to use.
A running endpoint keeps the secret value it read when the endpoint was created or last updated. To pick up a new secret value, update the endpoint configuration.
If a secret can't be read when the model is deployed, the endpoint starts without that variable. It doesn't report an error. If your model code can't find the variable, check that the secret still exists.
Configure an OpenAI API key in a notebook
See the following notebook for an example. It configures an OpenAI API key for a LangChain Retrieval QA Chain deployed behind model serving endpoints with secret-based environment variables.
Configure access to resources from model serving endpoints notebook
Limitations
For general model serving endpoint limitations, see Model Serving limits and regions.
Additional resources
| Resource | Description |
|---|---|
| Create an endpoint for custom models in Model Serving | Create an endpoint that serves a custom model. |
| Workspace secret scopes | Create and manage secret scopes in your workspace. |