Why Do dbt Tests Pass While Data Values Change, and Do You Need Data Diff in CI?
Assertion tests check rules you wrote in advance, so a value change that breaks no rule gets through
dbt tests pass while values change because they are assertions: each one checks a rule you wrote in advance, such as the four built-in generic tests (unique, not_null, accepted_values and relationships) listed in the dbt data tests docs. A value that shifts but stays inside those rules never fails, so it ships. Add a data diff to CI when a model feeds money, metrics or downstream contracts and you cannot enumerate every way its values could be wrong.
Key Takeaways
- •dbt tests are assertions about rules you already know; they cannot fail on a change nobody thought to write a rule for.
- •A data diff compares the rows your pull request builds against the rows production holds, so it surfaces unexpected value changes without a pre-written rule.
- •Add a diff step to CI for models that feed revenue, finance, customer-facing metrics or contracts; skip it for scratch and low-traffic models.
- •A diff needs a baseline, and dbt's defer feature gives CI a way to read production relations for unchanged parents instead of rebuilding them.
- •The diff is evidence, not a verdict: someone, or an agent with rules, still has to decide whether a change is intended.
Why dbt tests pass when the data is wrong
A dbt test is a query that returns failing rows. If it returns none, the test passes. That design is deliberate and useful, but it means a test can only catch the failure modes you thought of when you wrote it. A not_null test cannot tell you that a column which used to hold net revenue now holds gross revenue. Both are non-null, both are numbers, and the test stays green.
The failure shows up most after refactors. You rewrite a join, change a filter, or swap a source, and the model still builds, still returns unique keys, and still satisfies every accepted_values list. What changed is the distribution: a few thousand rows now carry different amounts, or a category absorbed another one. Nothing in the test suite describes that shift, so nothing fails.
A published comparison of assertion tests and data diffs makes the same argument: assertion tests miss unexpected value changes, and teams respond by building custom defer-plus-diff checks into CI because subtle errors still got through. We read that as a practitioner signal rather than a vendor claim. If your team has already written one-off queries to compare old and new output before merging, you are doing a manual data diff.
What a data diff checks that assertions do not
A data diff runs the changed model in a CI schema and compares the result to the same model in production. It reports which rows were added, removed or modified, and which columns changed value. You do not write a rule first. You read the difference and decide whether it matches what the pull request was supposed to do.
That reverses the burden. With assertions, the author predicts failures. With a diff, the author explains changes. A reviewer who sees that 12,000 rows changed in the amount column, when the pull request description says it only renamed a field, has a concrete reason to stop the merge. The same reviewer reading only a green test run has nothing to go on.
A diff is not a replacement for tests. Tests keep running on every build, including scheduled production runs, where there is no pull request to diff against. A diff only exists at change time. We treat them as two layers: assertions guard the invariants you know, and a diff reports the deltas you did not predict. For the contract side of that pair, see our walkthrough of a data contract template in YAML.
When you actually need data diff in CI
Not every model deserves a diff. A diff costs warehouse compute, adds minutes to CI, and generates output a human has to read. Spend that budget where an unnoticed value change is expensive, and nowhere else.
- •The model feeds finance, revenue recognition, billing or board-level metrics.
- •The model backs a data contract or an external consumer who cannot easily roll back.
- •The change is a refactor, where output is supposed to stay identical and any delta is a bug.
- •The model has many downstream dependents, so one wrong value fans out.
- •Your team has shipped a silent value regression from a green test run before.
The refactor case is the strongest. When a pull request claims no behavior change, the expected diff is empty, and an empty diff is easy to automate: any changed row fails the check. For feature work, where changes are intended, the diff becomes a review aid instead of a gate.
Skip it for exploratory models, staging layers that are thin renames of sources, and anything with no downstream readers. If your project is small and one analyst reviews every change, a manual comparison query may be enough for now. Revisit when review load grows faster than reviewers can read.
How to wire a defer-based diff into CI
A diff needs a baseline and a candidate. The baseline is the production relation. The candidate is the model built from your branch. The awkward part is building the candidate without rebuilding your whole project, and dbt's defer documentation describes the mechanism: with a production manifest as state, dbt resolves references to unchanged upstream models against production instead of building them in the CI schema.
In practice the flow has four moves. Save the production manifest from the last deploy. Run dbt build with a state-modified selector so only changed models and their dependents build, with defer pointing at that manifest. Run the diff for each changed model against its production twin, joined on the primary key. Publish the summary back to the pull request.
dbt's own continuous integration docs cover the state-based selection side of this. The diff itself can be a SQL query you maintain or an open-source package; for example, the data validation package published on PyPI at version 0.38.3 targets exactly this pull-request workflow. Whichever you pick, test it against a branch where you deliberately change a value before you trust it.
Keeping the diff readable and cheap
A raw row-level diff on a billion-row table is neither cheap nor readable. Start with summaries: row count delta, primary-key adds and removes, and per-column counts of changed values. Drill into sample rows only for columns that moved. A reviewer can scan five numbers; no one scans five million rows.
Limit scope in two ways. First, diff only the models the pull request changed, plus dependents that are tagged critical. Second, filter to a recent partition where the table allows it, such as the last seven days of an events table. Both choices trade completeness for speed, and you should write that trade into the check's documentation so no one reads a partial diff as a full one.
Decide ahead of time what counts as expected. A pull request that adds a column should show an added column and no changed values elsewhere. A pull request that fixes a bug should show changes in the affected rows and nowhere else. Encoding that expectation in the pull request description gives the reviewer something to compare the diff against.
Where the Data Guardrail agent fits
The diff produces evidence, and someone has to turn it into a merge decision. That is the step where teams stall: the output sits in a comment, the author is busy, the reviewer skims. The Data Guardrail agent is built for this slot, issuing a pre-merge verdict that reads the diff alongside your tests and contracts instead of leaving the interpretation to whoever opens the pull request first.
Because every Data Workers agent is an MCP server, an engineer can ask the question from Claude Code or Cursor before pushing: "Is the customer_id join safe after the migration?" or "What's the blast radius if I drop this column?" The answer draws on lineage and the diff together, so the reviewer sees what changed and which downstream consumers it touches.
We keep the OSS edition read-only by default, so an agent can read your warehouse and report without writing anything. Writes, approval workflows and audit trails belong to Pro. Either way, we would not let any agent auto-merge a value change it cannot explain; a verdict should say what changed, who is affected, and what a human needs to confirm.
A five-step rollout
- •Pick three to five critical models and list who consumes each one.
- •Store the production manifest after every deploy so CI has a baseline for defer.
- •Add a diff step that runs only for changed critical models and prints summary counts.
- •Make the check blocking for refactor pull requests and advisory for feature pull requests.
- •Review the false alarms after two weeks and tighten scope, such as excluding audit timestamp columns that always change.
Start advisory. A blocking check that cries wolf teaches people to bypass it, and then you have the cost of CI with none of the protection. After a few weeks of reading real output, you will know which columns are noisy and which models deserve a hard gate.
Track one number: how many value regressions reached production after the check went live compared with the quarter before. Use your own incident log for this. If the count does not move, the diff is not covering the models that hurt you, and the scope needs another look.
Frequently Asked Questions
Do dbt unit tests solve this problem?
They help with logic you can express as fixed inputs and expected outputs, which makes them good for transformations with tricky branches. They still test synthetic rows you chose, not the shape of real production data. A diff against production catches changes that only appear on real data, such as an unexpected null pattern or a late-arriving category, so we use both.
Is a data diff the same as data observability?
No. Observability watches production after a change lands and flags anomalies in freshness, volume or distribution. A CI diff runs before merge and compares a specific proposed change against production. One tells you something drifted; the other tells you which pull request would cause it. Teams that run both catch problems earlier and with a clearer cause.
How much warehouse cost does a CI diff add?
It depends on table size and how much you scope. Diffing only changed models, using defer so unchanged parents are not rebuilt, and starting with summary counts keeps cost modest. We do not quote a figure because it varies by warehouse and data volume. Measure one week of CI runs with the check on, then compare your warehouse query history.
Should the diff block the merge?
For refactors that claim no behavior change, yes: any changed value should fail. For feature work, make it advisory and require the author to explain the delta in the pull request. Move a model from advisory to blocking only after you have seen its diff output behave sensibly for a few weeks.