Skip to main content
The Lightdash Python SDK lets you query your semantic layer directly from Python. Use it in Jupyter notebooks, Python scripts, or anywhere you use Python to ensure everyone pulls from a single source of truth.

See it in action

Try the getting started Jupyter notebook for a hands-on walkthrough.

Installation

Before you start

To use the SDK, you need three things from your Lightdash instance:
  1. instance_url — the URL where you log into Lightdash (for example, https://app.lightdash.cloud for Lightdash Cloud, or your self-hosted URL like https://lightdash.mycompany.com).
  2. project_uuid — the unique ID of the project you want to query.
  3. access_token — a personal access token (PAT) used to authenticate as you.
You don’t need to be an admin to do any of this. You just need access to the project you want to query.

How to get your project UUID

  1. Log in to Lightdash and open the project you want to query.
  2. Look at the URL in your browser’s address bar. It looks like this:
  3. Copy the long string between /projects/ and the next /. In the example above, the project UUID is 3675b69e-8324-4110-bdca-059031aa8da3.

How to get a personal access token

  1. Click your avatar (top-right corner) and go to Settings.
  2. In the left-hand menu, under your name, click Personal access tokens.
  3. Click Generate new token.
  4. Give it a description (for example, “Python SDK”) and pick an expiration date.
  5. Click Generate token.
  6. Copy the token immediately and save it somewhere safe — you won’t be able to see it again after closing the dialog. If you lose it, just generate a new one.
Treat your access token like a password. Don’t commit it to git or share it. For scripts, load it from an environment variable (for example, os.environ["LIGHTDASH_TOKEN"]) instead of pasting it directly into your code.
For more details, see Personal access tokens.

Quick start

Query builder

The SDK provides two patterns for building queries: single-call and chainable builder.

Single-call pattern

Pass all parameters at once for simple queries:

Chainable builder pattern

Build queries incrementally with method chaining. Each method returns a new immutable Query object:
Key characteristics:
  • Immutable — each method returns a new Query object, safe for reuse
  • Lazy evaluation — API calls only happen when .execute() is called
  • Order-independent — methods can be called in any order
  • Composable — create base queries and extend them

Dimensions and metrics

Access dimensions and metrics as attributes on the model:
Features:
  • Lazy loading — fetched from API on first access, then cached
  • Fuzzy matching — typos suggest closest matches
  • Tab completion — works in Jupyter/IPython for discovery
  • Rich display — HTML rendering in notebooks

Filters

Use Python comparison operators on dimensions to create filters:

Supported operators by data type

Combining filters

Use & (AND) and | (OR) to combine filters:
Multiple .filter() calls on a query are combined with AND logic:

Sorting

Sort results using the .sort() method:

Results

Query results implement a unified ResultSet interface.

Converting results

Iterating over results

Pagination

For large result sets, results are paginated automatically:
Available properties on the result:
  • result.query_uuid — unique identifier for the query
  • result.total_results — total number of rows
  • result.total_pages — number of pages
  • result.fields — field metadata

SQL runner

Execute raw SQL queries directly against your data warehouse:

Exception handling

The SDK provides specific exceptions for different error conditions:
Exception hierarchy:
  • LightdashError — base exception for all SDK errors
    • QueryError — query execution failed (HTTP 400)
    • QueryTimeout — query exceeded timeout (HTTP 408)
    • QueryCancelled — query was cancelled (HTTP 499)

Complete example


For the full SDK source and more examples, see the Python SDK on GitHub.