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

Test data quality in an Airflow pipeline

Verify data contracts as tasks in an Airflow DAG, and stop the pipeline when data fails a check.

This guide shows how to verify Soda data contracts at each stage of an Apache Airflow pipeline: after ingestion, after a dbt transformation, and before the data reaches a report. When a check fails, the Airflow task fails and the tasks downstream of it don't run, so data that fails a check never reaches the report. Warnings are logged and published to Soda Cloud without stopping the pipeline.

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

Example pipeline

A data engineer builds a report of sales by product category from AdventureWorks data in PostgreSQL. dbt builds two models: fact_product_category, which joins products to their subcategories and categories, and report_category_sales, which sums sales per category.

The DAG verifies a set of contracts after each stage:

Task
What it does

verify_ingest

Verifies the contracts for the source tables dim_product and fact_internet_sales

dbt_transform

Builds fact_product_category

verify_transform

Verifies the contract for fact_product_category

dbt_reports

Builds report_category_sales

verify_reports

Verifies the contract for report_category_sales

The project has this layout:

sales-pipeline/
├── dags/
│   └── sales_report.py
├── dbt/
│   ├── dbt_project.yml
│   ├── profiles.yml
│   └── models/
│       ├── transform/fact_product_category.sql
│       └── reports/report_category_sales.sql
└── soda/
    ├── ds_config.yml
    ├── sc_config.yml
    └── contracts/
        ├── ingest/
        │   ├── dim_product.yml
        │   └── fact_internet_sales.yml
        ├── transform/
        │   └── fact_product_category.yml
        └── reports/
            └── report_category_sales.yml

Prerequisites

  • An Airflow 3 environment, and access to its workers to install Python packages

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

  • A Soda Cloud account and an API key, to publish results. See Generate API keys.

Install Soda in its own environment

Install Soda in a separate virtual environment on the Airflow workers, not in the Airflow environment itself. Soda Core pins versions of dependencies that Airflow also pins in its constraints file, such as requests, so the two can't always be installed together. The DAG calls Soda through Airflow's @task.external_python decorator, which runs a task in another Python environment.

dbt has the same kind of conflict with Airflow, so this example installs it in its own environment as well and calls it from a BashOperator:

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

Connect Soda to the data source and Soda Cloud

Create a data source configuration file in the soda folder. The values in ${env.…} are read from environment variables, which must be set on the Airflow workers.

Create a Soda Cloud configuration file next to it. Use cloud.us.soda.io or cloud.soda.io, depending on your Soda region.

Test both connections from the soda folder:

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

Write the contracts

Each contract defines the checks for one dataset. The DAG verifies every contract in a stage folder, so to add a dataset to a stage, add its contract to that folder.

Ingest contracts

The source tables come from upstream systems that the data engineer can't fix directly. These contracts catch problems as soon as the data lands, so the engineer can report them to the data owners before any model is built on top.

A product without a subcategory isn't an error on its own, but it ends up in the report as uncategorized sales. That check warns instead of failing.

Transform contract

The transformation joins every product to a subcategory and a category. A product that has no match keeps its row with empty category keys, so these checks warn when that happens.

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.

Coming from Soda v3? Checks that compare a value with previous scans, such as change for row_count and anomaly detection, have no contract equivalent. Use Metric Monitoring to detect anomalies on the same datasets. For the rest of the syntax changes, see Migrate from v3 to v4.

Verify a contract from the command line

Before you add the contracts to the DAG, validate and verify them from the soda folder:

soda contract test checks the contract syntax without connecting to the data source. soda contract verify runs the checks and prints a table of results. Its exit code reports the outcome:

Exit code
Meaning

0

All checks passed

1

One or more checks failed

2

One or more checks warned, and none failed

3

An error occurred during execution, such as a parse or connection error

4

Results could not be sent to Soda Cloud

In this example, dim_product has one product without a subcategory, so the command exits with 2.

Create the DAG

The DAG runs each verification stage with the same task. The task verifies every contract in the stage folder with verify_contract_locally, publishes the results to Soda Cloud, and raises an error if any contract fails.

result.is_ok is False when a check fails or the verification errors, and True when checks only warn. A warning therefore doesn't stop the pipeline.

Replace the paths at the top of the file with the locations of your project and virtual environments.

A few details in the task:

  • expect_airflow=False tells Airflow that the Soda environment doesn't include Airflow. Without it, Airflow logs a warning for every run.

  • The imports are inside the function, because the function runs in the Soda environment.

  • configure_logging is called before each verification in the loop. See Python API.

Run the DAG

Run the DAG once from the command line to test it:

In this example the run succeeds. verify_ingest and verify_transform log warnings for the product without a subcategory, and all five tasks complete.

When a check fails, the task that verified it fails, and the tasks after it don't run. For example, a sale with an amount of 0 fails the check All sales have a non-zero order amount. verify_ingest then fails with an error like this, and dbt never builds the models:

The Checks tab of the fact_internet_sales dataset in Soda Cloud, with the check All sales have a non-zero order amount failed
The failed check on fact_internet_sales after the ingest task stopped the pipeline

Results in Soda Cloud

Each verification publishes its results to the dataset in Soda Cloud. Open Datasets, select a dataset, and open the Checks tab to see the latest result of each check and its result history across runs.

The Checks tab of the fact_product_category dataset in Soda Cloud, with one passing check and two warnings
The transform contract's results: the category and subcategory checks warn for the product without a subcategory

To get notified when a check fails, see Notifications.

Next steps

Last updated

Was this helpful?