Databricks SDK for Python (Beta)

This repository is a read-only mirror, published from Databricks'
internal repository with each release. Pull requests are reviewed
here but merged internally (see CONTRIBUTING.md).
Beta: This SDK is supported for production use cases,
but we do expect future releases to have some interface changes; see Interface stability.
We are keen to hear feedback from you on these SDKs. Please file issues, and we will address them.
| See also the SDK for Java
| See also the SDK for Go
| See also the Terraform Provider
| See also cloud-specific docs (AWS,
Azure,
GCP)
| See also the API reference on readthedocs
The Databricks SDK for Python includes functionality to accelerate development with Python for the Databricks Lakehouse.
It covers all public Databricks REST API operations.
The SDK's internal HTTP client is robust and handles failures on different levels by performing intelligent retries.
Contents
Getting started
- Please install Databricks SDK for Python via
pip install databricks-sdk and instantiate WorkspaceClient:
from databricks.sdk import WorkspaceClient
w = WorkspaceClient()
for c in w.clusters.list():
print(c.cluster_name)
Databricks SDK for Python is compatible with Python 3.7 (until June 2023), 3.8, 3.9, 3.10, and 3.11.
Note: Databricks Runtime starting from version 13.1 includes a bundled version of the Python SDK.
It is highly recommended to upgrade to the latest version which you can do by running the following in a notebook cell:
%pip install --upgrade databricks-sdk
followed by
dbutils.library.restartPython()
Code examples
The Databricks SDK for Python comes with a number of examples demonstrating how to use the library for various common use-cases, including
These examples and more are located in the examples/ directory of the Github repository.
Some other examples of using the SDK include:
Authentication
If you use Databricks configuration profiles
or Databricks-specific environment variables
for Databricks authentication, the only code required to start
working with a Databricks workspace is the following code snippet, which instructs the Databricks SDK for Python to use
its default authentication flow:
from databricks.sdk import WorkspaceClient
w = WorkspaceClient()
w. # press <TAB> for autocompletion
The conventional name for the variable that holds the workspace-level client of the Databricks SDK for Python is w, which is shorthand for workspace.
In this section
Default authentication flow
If you run the Databricks Terraform Provider,
the Databricks SDK for Go, the Databricks CLI,
or applications that target the Databricks SDKs for other languages, most likely they will all interoperate nicely together.
By default, the Databricks SDK for Python tries the following authentication methods,
in the following order, until it succeeds:
- Databricks native authentication
- Azure native authentication
- GCP native authentication
- If the SDK is unsuccessful at this point, it returns an authentication error and stops running.
Each authentication method requires specific configuration attributes (e.g., token for PAT auth, azure_client_id for Azure service principal auth). The SDK automatically detects the cloud provider and skips authentication methods whose required configuration attributes are not present. This means that Azure-specific methods like azure-cli are automatically skipped when connecting to an AWS or GCP workspace, and vice versa for GCP-specific methods.
To force a specific authentication method instead of relying on auto-detection, set the auth_type argument:
from databricks.sdk import WorkspaceClient
# Force Azure CLI authentication — skip all other methods
w = WorkspaceClient(host='https://mycompany.databricks.com', auth_type='azure-cli', cloud='AZURE')
This is useful when your environment has credentials for multiple authentication methods and you want to ensure a specific one is used or when auto detection is not accurate.
For each authentication method, the SDK searches for compatible authentication credentials in the following locations,
in the following order. Once the SDK finds a compatible set of credentials that it can use, it stops searching:
Credentials that are hard-coded into configuration arguments.
:warning: Caution: Databricks does not recommend hard-coding credentials into arguments, as they can be exposed in plain text in version control systems. Use environment variables or configuration profiles instead.
Credentials in Databricks-specific environment variables.
For Databricks native authentication, credentials in the .databrickscfg file's DEFAULT configuration profile from its default file location (~ for Linux or macOS, and %USERPROFILE% for Windows).
For Azure native authentication, the SDK searches for credentials through the Azure CLI as needed.
Depending on the Databricks authentication method, the SDK uses the following information. Presented are the WorkspaceClient and AccountClient arguments (which have corresponding .databrickscfg file fields), their descriptions, and any corresponding environment variables.
Unified host support
Certain Databricks host types support both account-level and workspace-level API operations from a single endpoint. When using such a unified host, a single configuration profile can be used to create both WorkspaceClient and AccountClient instances without changing the host.
For this to work, the following conditions must be met:
- The host must support unified operations.
- Both
account_id and workspace_id must be available — either set explicitly in the configuration or auto-discovered.
When both values are present, the SDK uses workspace_id to route workspace-level requests and account_id to route account-level requests, all through the same host.
# .databrickscfg
[unified]
host = https://mycompany.databricks.com
account_id = 00000000-0000-0000-0000-000000000000
workspace_id = 1234567890
from databricks.sdk import WorkspaceClient, AccountClient
# Both clients share the same host and profile
w = WorkspaceClient(profile='unified')
a = AccountClient(profile='unified')
# A WorkspaceClient for a different workspace under the same host and account
w = WorkspaceClient(profile='unified', workspace_id='2345678901')
If the host supports it, account_id and workspace_id may be auto-discovered, reducing the required explicit configuration.
Databricks native authentication
By default, the Databricks SDK for Python initially tries Databricks token authentication (auth_type='pat' argument). If the SDK is unsuccessful, it then tries Workload Identity Federation (WIF). See Supported WIF for the supported JWT token providers.
- For Databricks token authentication, you must provide
host and token; or their environment variable or .databrickscfg file field equivalents.
- For Databricks OIDC authentication, you must provide the
host, client_id and token_audience (optional) either directly, through the corresponding environment variables, or in your .databrickscfg configuration file.
- For Azure DevOps OIDC authentication, the
token_audience is irrelevant as the audience is always set to api://AzureADTokenExchange. Also, the System.AccessToken pipeline variable required for OIDC request must be exposed as the SYSTEM_ACCESSTOKEN environment variable, following Pipeline variables
During initialization, the SDK automatically resolves missing configuration fields (account_id, workspace_id, cloud, and discovery_url). Any explicitly provided values take precedence and are never overwritten. If the auto discovery fails, the SDK falls back to the explicit configuration. It is recommended to always set explicit configuration.
| Argument |
Description |
Environment variable |
host |
(String) The Databricks host URL for either the Databricks workspace endpoint or the Databricks accounts endpoint. |
DATABRICKS_HOST |
account_id |
(String) The Databricks account ID for the Databricks accounts endpoint. Auto-discovered if not provided. |
DATABRICKS_ACCOUNT_ID |