Sub topic 11
Using the Databricks SDK for Python
This chapter assumes you can run Python, have authenticated to a workspace as shown in earlier chapters, and have seen the raw Jobs API calls for running and polling a job. Here you replace that HTTP code with the Databricks SDK for Python.
Why use the SDK
Raw requests code works, but you write the same plumbing each time: building headers, following pagination tokens, retrying, and parsing errors. The SDK is a Python package that wraps the REST API and handles those details. It also reads the same credentials the CLI uses, so one login serves both tools.
Install it with pip:
pip install databricks-sdkAuthentication
WorkspaceClient finds credentials on its own. It checks, in order, arguments you pass, then environment variables such as DATABRICKS_HOST and DATABRICKS_TOKEN, then a profile in ~/.databrickscfg. This is called unified authentication, and it is the same logic the CLI uses.
from databricks.sdk import WorkspaceClient
w = WorkspaceClient() # environment variables or the DEFAULT profile
w_dev = WorkspaceClient(profile="dev") # a named profilePassing a token as a string in code is possible but you should avoid it. Use a profile or environment variables so secrets stay out of source control.
Listing resources
The raw approach needs a loop over next_page_token. In the SDK, list methods return an iterator that fetches further pages as you consume it.
from databricks.sdk import WorkspaceClient
w = WorkspaceClient()
for job in w.jobs.list():
print(job.job_id, job.settings.name)Because the iterator is lazy, a workspace with thousands of jobs does not load everything at once. Wrap it in list(...) only when you need all results in memory.
Running a job and waiting
The polling loop from the REST chapter collapses to one call. run_now returns a waiter object, and .result() blocks until the run reaches a terminal state.
from databricks.sdk import WorkspaceClient
w = WorkspaceClient()
run = w.jobs.run_now(job_id=123).result()
print(run.state.result_state, run.state.state_message)If you want the run ID immediately without blocking, call run_now_and_wait alternatives sparingly and prefer run_now(...) and read waiter.run_id, then check status later with w.jobs.get_run(run_id=...).
Handling errors
API failures raise exceptions from databricks.sdk.errors, such as NotFound or PermissionDenied. Catch the specific class instead of inspecting status codes.
from databricks.sdk import WorkspaceClient
from databricks.sdk.errors import NotFound
w = WorkspaceClient()
try:
job = w.jobs.get(job_id=999999)
except NotFound:
print("No such job")The SDK also retries some transient failures, such as rate-limit responses, automatically.
SDK or raw HTTP
| Situation | Better choice |
|---|---|
| Python script with several API calls | SDK |
| One-off call from a shell | CLI or `curl` |
| Endpoint missing from your SDK version | `w.api_client.do(...)` or `databricks api` |
| Non-Python environment | CLI or REST |
Exercise
Why does for job in w.jobs.list() not need a manual pagination loop, and when would you wrap it in list(...)?
Show answer
The SDK returns a lazy iterator that requests the next page when the current one is exhausted, so you never handle next_page_token yourself. Wrap it in list(...) only when you need all results in memory at once, for example to sort or count them.
Exercise
A script calls WorkspaceClient() with no arguments and fails to authenticate on a CI runner. Name two ways to supply credentials without putting a token in the code.
Show answer
Set the DATABRICKS_HOST and DATABRICKS_TOKEN environment variables (or OAuth client ID and secret variables) from the CI secret store, or create a profile in ~/.databrickscfg and pass profile="name".