Test data quality in an ADF pipeline
Verify a data contract with reconciliation checks in a Synapse notebook, and stop an Azure Data Factory pipeline when copied data doesn't match its source.
This guide shows how to verify a Soda data contract from an Azure Data Factory (ADF) pipeline. A Notebook activity runs a Synapse notebook that verifies the contract after a Copy activity: reconciliation checks compare the copied data with its source, and column checks test its completeness. When a check fails, the notebook raises an error, the Notebook activity fails, and the activities that depend on its success don't run.
Use the example as a starting point and adapt the data sources, datasets, and checks to your own pipeline.
Example pipeline
A data engineer copies the retail_customers table from a PostgreSQL database to a dedicated SQL pool in Azure Synapse Analytics. Before the rest of the pipeline uses the copy, they want to know that it matches the source and that customer IDs and country codes are complete.
The pipeline has these activities:
Copy data
Copies retail_customers from PostgreSQL to the dedicated SQL pool
Notebook
Runs the verify_contract notebook, which verifies the contract for retail_customers
Later activities
Run only when the Notebook activity succeeds
The Soda files live in a folder in the workspace's Azure Data Lake Storage Gen2 account:
soda/
├── ds_synapse.yml
├── ds_postgres.yml
├── sc_config.yml
└── contracts/
└── retail_customers.ymlPrerequisites
An Azure Data Factory pipeline that copies data from PostgreSQL to a dedicated SQL pool in an Azure Synapse Analytics workspace
An Apache Spark pool in the same workspace, on Azure Synapse Runtime for Apache Spark 3.5, that can reach the dedicated SQL pool and PostgreSQL. Runtime 3.4 reached end of support on March 31, 2026.
An Azure Data Lake Storage Gen2 account that the Spark pool can read
An Azure Key Vault with a linked service in the Synapse workspace
A login on the dedicated SQL pool and on PostgreSQL with read access to the tables. See Synapse and PostgreSQL for the permissions Soda needs.
A Soda Cloud account and an API key. See Generate API keys.
Add the Soda packages to the Spark pool
When a pipeline runs a notebook, Synapse disables the %pip and %conda commands, so install Soda on the Spark pool itself. The Spark 3.5 runtime includes Python 3.11 and ODBC Driver 18 for SQL Server, which soda-synapse uses by default.
Create a requirements.txt file with the Soda packages for the two data sources. Both are on PyPI.
Upload the file to the Spark pool: in Synapse Studio, go to Manage > Apache Spark pools, open the Packages section of the pool, and upload requirements.txt. See Manage libraries for Apache Spark pools.
On a computer with Python and pip, download the soda-reconciliation wheel from the private PyPI, with your Soda Cloud API key:
This command uses the US repository. For the EU region, use enterprise.pypi.cloud.soda.io.
Upload soda-wheels/soda_reconciliation-4.27.0-py3-none-any.whl as a workspace package in Manage > Workspace packages, then add it to the Spark pool's packages. See Workspace packages.
After you save the pool's packages, Synapse runs a job that installs them. New notebook sessions on the pool use the installed packages.
Connect Soda to the data sources and Soda Cloud
Create one data source configuration file for the dedicated SQL pool and one for PostgreSQL. The values in ${env.…} are read from environment variables, which the notebook sets from Key Vault.
Set SYNAPSE_HOST to the endpoint of the dedicated SQL pool, <workspace-name>.sql.azuresynapse.net, and database to the name of the pool. The connection has no schema: each contract names the database and schema in its dataset name.
To connect with a Microsoft Entra service principal instead of a SQL login, replace the last three lines of the connection:
Create a Soda Cloud configuration file next to them. Use cloud.us.soda.io or cloud.soda.io, depending on your Soda region.
For all connection options, including encryption and other authentication methods, see Synapse.
Write the contract
The contract defines the checks for the copied table in the dedicated SQL pool, the target dataset. The columns section tests completeness and uniqueness, and the reconciliation section compares the table with its source in PostgreSQL.
A few details in the contract:
Each source has a
name, which the checks refer to withsource:, and adatasetin the source data source.Without a
threshold, a reconciliation check passes only when the difference between source and target is0.metric_diffcompares the result of a SQL expression on each side. Here it compares the number of missing values in a column.Row-level
rows_diffchecks are not supported in Synapse.Both
metric_diffchecks are the same check type, so the second one needs aqualifierto give it a distinct identity. Without it, the verification fails with aDuplicate identityerror.
If you don't have the soda-reconciliation package, remove the reconciliation section. The column checks don't need it.
For every reconciliation check type, see Reconciliation checks.
Test the contract from the command line
Before you add the contract to the pipeline, test it from a computer that can reach both databases. The computer needs Python 3.10, 3.11, or 3.12, and the Microsoft ODBC driver for SQL Server.
Install the same packages as the Spark pool in a virtual environment, from the folder that holds requirements.txt and soda-wheels:
Set the environment variables that the configuration files reference, then run these commands from the soda folder:
soda contract test checks the contract syntax without connecting to the data sources. soda contract verify takes both data source files, because the reconciliation checks read from both. It prints a table of results and exits with 0 when all checks pass, or 1 when a check fails. For the other exit codes, see Exit codes.
Upload the Soda files
Upload the soda folder to a container in the storage account, for example a container named soda. The notebook copies the files from there to the local disk of the Spark pool, because Soda reads its configuration and contracts from local files.
Create the notebook
The notebook reads the connection values from Key Vault, copies the Soda files to the local disk, and verifies one contract. It raises an error if the contract fails, which fails the Notebook activity.
In Synapse Studio, create a notebook named verify_contract and attach it to the Spark pool. Add two cells.
The first cell holds the notebook's parameters. Select ... > Toggle parameter cell on the cell to make it the parameters cell. Replace <storage-account> with the name of your storage account.
The second cell verifies the contract. Replace <key-vault-name> and <key-vault-linked-service> with the name of your Key Vault and of its linked service, and the secret names with the names of your secrets.
A few details in the notebook:
mssparkutils.credentials.getSecretwith a linked service reads the secret with the workspace identity. In a pipeline run, Synapse uses the workspace identity to access Key Vault, so grant it access to the secrets. To also run the notebook yourself, grant your own Microsoft Entra account access too. See Microsoft Spark Utilities.verify_contract_locallygets both data source files indata_source_file_paths, because the reconciliation checks read from both data sources.result.is_okisFalsewhen a check fails or the verification errors. See Python API.With
publish = False, the notebook verifies the contract without sending results to Soda Cloud. It still needs the Soda Cloud API key, because Soda reads the dataset's configuration from Soda Cloud whenever it gets a Soda Cloud configuration file.
Add the notebook to the pipeline
Add a Notebook activity that runs the notebook after the Copy activity:
In the pipeline, drag a Notebook activity from the Synapse group of the Activities pane to the canvas, and connect the Copy activity's success output to it.
On the Azure Synapse Analytics (Artifacts) tab, select or create the linked service for the Synapse workspace.
On the Settings tab, select the
verify_contractnotebook and the Spark pool. Add a base parameter namedcontractwith the valueretail_customers.yml. The name must match the variable in the parameters cell.Connect the Notebook activity's success output to the activities that should run only when the data passes its checks.
When the pipeline runs, Data Factory adds a cell below the parameters cell that sets the base parameters, so the notebook verifies the contract you pass. To verify another dataset, add its contract to the contracts folder and add a Notebook activity that passes its file name.
For every setting of the activity, see Transform data by running a Synapse notebook.
Run the pipeline
When all checks pass, the notebook ends normally, the Notebook activity succeeds, and the pipeline continues.
When a check fails, the notebook raises an error and the Notebook activity fails. For example, if the copy loses a row, the check Row count matches the source fails, and the notebook stops with:
The activities connected to the Notebook activity's success output don't run. To see the error, open the pipeline run in the Monitor tab and select the output of the Notebook activity.
Results in Soda Cloud
With publish = True, each run publishes its results to the retail_customers dataset in Soda Cloud. Open Datasets, select the dataset, and open the Checks tab to see the latest result of each check and its result history across runs.
To get notified when a check fails, see Notifications.
Next steps
Learn when to compare metrics and when to compare rows in Data reconciliation.
Run verifications on a Soda Runner instead of the Spark pool.
Last updated
Was this helpful?
