---
title: "Data quality lifecycle"
description: Stages of a pipeline run at which data is validated, from source data to the destination
keywords: ["dlthub", "data quality", "lifecycle", "contracts", "pydantic", "check", "metrics"]
---
# Data quality lifecycle

:::warning
This feature is in public preview
:::

Data can be validated at several stages of a pipeline run. The stage determines three things that are hard to change later:

- the **input data** that a check can see: a single record, the rows of the current load, or the full dataset with its history
- the **compute resources** that are used: the machine that runs the pipeline or the destination
- the **actions** that are possible after a failed check: reject or repair a record before it is stored, keep a whole load out of the dataset, or report on data that is already there

dlt distinguishes two kinds of data. **Source data** is the data as it arrives from the source, before it is normalized. **Destination data** is the normalized data, before or after it is loaded. Source data is validated record by record while the pipeline extracts and normalizes it. Destination data is validated with [checks and metrics](./index.md), which describe whole tables.

The table lists the stages in the order in which a pipeline passes through them:

| Stage                                             | Data that is validated                | Mechanism                                       | Status           |
| ------------------------------------------------- | ------------------------------------- | ----------------------------------------------- | ---------------- |
| [Source data validation](#source-data-validation) | Records, during extract and normalize | Schema contracts, Pydantic models, schema hints | Available in dlt |
| [Pre-load (in-memory)](#pre-load-in-memory)       | The load packages of the current load | Checks and metrics                              | Work in progress |
| [Pre-load (staging)](#pre-load-staging)           | The current load in a staging dataset | Checks and metrics                              | Work in progress |
| [Post-load](#post-load)                           | The full dataset on the destination   | Checks and metrics with `dq.run()`              | Available        |

## Source data validation

The first place to catch bad data is the pipeline itself. While dlt extracts and normalizes source data, it controls the shape of the data and validates each record, before anything is written to the destination:

- **[Schema contracts](../../general-usage/schema-contracts.md)** control how the schema is allowed to evolve. For tables, columns and data types you decide whether a change is accepted (`evolve`), raises an error (`freeze`), or whether the offending row or value is dropped (`discard_row`, `discard_value`).
- **[Pydantic models](../../general-usage/schema-contracts.md#use-pydantic-models-for-data-validation)** validate the content of each record: required fields, formats, value ranges and any rule that you can write as a validator. A model also [defines the schema](../../general-usage/resource.md#define-a-schema-with-pydantic) of the table that the resource produces.
- **Schema hints** on a resource, such as data types and `nullable=False`, make dlt coerce values to the declared types and keep nulls out of columns that must not have them.

This validation is stateless and looks at one record at a time. It fits semi-structured data from APIs well, and it is the only stage that can reject or repair a single record before it is stored. By design it cannot answer questions about a table as a whole: whether a key is unique, whether each order has a customer, whether the data is fresh. These questions need aggregates over the destination data and are the domain of checks and metrics in the stages below.

The [data quality lifecycle](../../general-usage/data-quality-lifecycle.md) page of the dlt documentation gives an overview of these features, grouped by the dimension of data quality that they address.

## Pre-load (in-memory)

:::warning
Work in progress. Currently unavailable.
:::

The pre-load execution in memory runs checks with `duckdb` against the load packages, that is the temporary files stored on the machine that runs dlt. Checks can then trigger actions before the data is loaded into the destination.

:::note
This is equivalent to using a staging destination that is the local filesystem. This section highlights the trade-offs of this choice.
:::

Properties:

- Failed records can be dropped or quarantined before load. This works with all write dispositions.
- Checks only have access to the current load. Checks against the full dataset require communication between the machine that runs the pipeline and the destination.
- Checks are computed on the machine that runs the pipeline, so its resources must match the size of the load.
- Data, check results and outcomes can be lost if the runtime is ephemeral (for example, an AWS Lambda timeout). In this case, the pipeline must process the data again.

```mermaid
sequenceDiagram
    participant Resource
    participant Pipeline
    participant Dataset

    Resource->>Pipeline: Extract
    Pipeline->>Pipeline: Normalize
    Pipeline->>Pipeline: Run checks
    Pipeline->>Dataset: Load
```

## Pre-load (staging)

:::warning
Work in progress. Currently unavailable.
:::

The pre-load execution with a staging dataset runs checks on the destination and triggers actions before the data is loaded into the final dataset. In effect, these are **post-load** checks on a staging dataset, followed by a second load phase.

:::info
dlt uses staging datasets for other features such as the `merge` and `replace` write dispositions.
:::

Properties:

- Failed records can be dropped or quarantined before load. This works with all write dispositions.
- The destination must support staging datasets.
- Checks have access to the current load. If the staging dataset is on the same destination as the final dataset, checks can also access the full dataset. If it is on a different destination, they cannot without moving data between the two.
- Checks are computed on the staging destination. This scales well with the size of the data and the complexity of the checks.
- Data, check results and outcomes can be kept safely in the staging dataset until they are reviewed. This helps human-in-the-loop workflows, because the pipeline does not have to process the data again.

```mermaid
sequenceDiagram
    participant Resource
    participant Pipeline
    participant Staging as Staging Dataset
    participant Dataset

    Resource->>Pipeline: Extract
    Pipeline->>Pipeline: Normalize
    Pipeline->>Staging: Load
    Staging->>Staging: Run Checks
    Staging->>Dataset: Load
```

## Post-load

The post-load execution is the simplest option, and the one that [`dq.run()`](./index.md#run-checks-on-your-data) implements today. The pipeline goes through `Extract -> Normalize -> Load` as usual. Then the checks are executed on the destination.

Properties:

- Failed records can't be dropped or quarantined before load. All records are written first, then checked, then handled. Removing bad records afterwards is practical only with `write_disposition="append"` or on destinations that support snapshots (for example `iceberg`, `ducklake`).
- Checks have access to the full dataset. This includes current and past loads and the internal dlt tables.
- Checks are computed directly on the destination. This scales well with the size of the data and the complexity of the checks.
- Results and outcomes are stored in the same dataset. No data movement is required.

```mermaid
sequenceDiagram
    participant Resource
    participant Pipeline
    participant Dataset

    Resource->>Pipeline: Extract
    Pipeline->>Pipeline: Normalize
    Pipeline->>Dataset: Load
    Dataset->>Dataset: Run Checks
```
