Skip to content
For the complete documentation index, see llms.txt. This page is also available as Markdown.

Test data quality in a Dagster pipeline

Verify data contracts as Dagster asset checks, so data that fails a check is never loaded or transformed.

This guide shows how to verify Soda data contracts as Dagster asset checks at two points in a pipeline: on incoming data before it is loaded, and on a dbt model after it is built. The checks are blocking, so when a check fails, Dagster doesn't materialize the assets downstream of it.

Use the example as a starting point and adapt the data source, assets, and contracts to your own pipeline.

Example pipeline

A retail company receives a file of new sales every day. The pipeline loads the file into PostgreSQL, and dbt builds a report of sales by product category on top of it.

Asset or check
What it does

incoming_sales

Reads the file of new sales into a pandas DataFrame

incoming_sales_contract

Verifies the DataFrame against a contract before anything is loaded

fact_internet_sales

Appends the new sales to the fact_internet_sales table

dbt models

Build fact_product_category and report_category_sales

report_category_sales_contract

Verifies the report against its contract

The project has this layout:

sales-pipeline/
├── data/
│   └── new_sales.csv
├── dbt/
│   ├── dbt_project.yml
│   ├── profiles.yml
│   └── models/
│       ├── sources.yml
│       ├── fact_product_category.sql
│       └── report_category_sales.sql
├── pyproject.toml
├── sales_pipeline/
│   ├── __init__.py
│   └── definitions.py
└── soda/
    ├── ds_config.yml
    └── contracts/
        ├── incoming_sales.yml
        └── report_category_sales.yml

Prerequisites

  • Python 3.10 or later

  • A PostgreSQL data source with the source tables, and a dbt project that builds models in it

Install Dagster, dbt, and Soda

Install Dagster with its dg command line and dbt integration, dbt, and the Soda packages for PostgreSQL and DuckDB. The DuckDB package lets Soda verify a pandas DataFrame in memory.

For other data sources and the full installation options, see Soda Core installation.

Connect Soda and dbt to the data source

Create a data source configuration file in the soda folder. The values in ${env.…} are read from environment variables.

dbt reads the same environment variables:

Set the environment variables before you run the pipeline:

For the configuration keys of each data source, see the Data source reference for Soda Core.

Write the contracts

Incoming data contract

This contract verifies the file of new sales while it is still a DataFrame. The pipeline registers the DataFrame as a DuckDB view named incoming_sales in a data source named incoming. A view registered this way belongs to DuckDB's temp catalog, so the dataset name is incoming/temp/main/incoming_sales.

To learn more about verifying DataFrames, see DuckDB advanced usage.

Report contract

The report contract fails if uncategorized sales reach 0.9% of the total, or if the report total doesn't match the total in fact_internet_sales. Both are metric checks on the same dataset, so each one needs a qualifier to give it a distinct identity.

For every check type and threshold option, see the Contract Language reference.

Define the pipeline

The pyproject.toml file at the root of the project tells the dg command line where the definitions are:

The definitions file creates the assets, the dbt assets, and one asset check per contract. Each check calls verify_contract_locally and returns an AssetCheckResult. result.is_ok is False when a check in the contract fails or the verification errors, and True when checks only warn.

A few details in the definitions:

  • blocking=True makes Dagster stop the run when the check fails, so fact_internet_sales and the dbt models aren't materialized after a failed incoming_sales_contract.

  • key_prefix="adventureworks" gives fact_internet_sales the same asset key as the dbt source adventureworks.fact_internet_sales, so Dagster runs the dbt models after the load.

  • verify_contract_locally accepts DuckDB data sources created from an existing cursor. See Python API.

Run the pipeline

Create the dbt manifest that dagster-dbt reads, then run the job:

With clean data, both checks pass, the new sales load, and dbt builds the report.

When a check fails, the run fails at that check. For example, a file with a sale of amount 0 fails the check All sales have a non-zero order amount. incoming_sales_contract doesn't pass, Dagster doesn't load the file, and dbt doesn't run.

To see the results in the Dagster UI, start it from the project folder:

Open the incoming_sales asset and its Checks tab to see the latest result of the check and the metadata it reports.

The Checks tab of the incoming_sales asset in the Dagster UI, with the blocking check incoming_sales_contract failed and one failed check in its metadata
The failed blocking check on incoming_sales

Next steps

Last updated

Was this helpful?