Skip to main content

Add expectations to a pipeline

Add checks to a pipeline so that a run fails when its data breaks a rule, before anything reaches the branch. Use this page when a pipeline already runs and you want to guard its inputs or outputs. For what an expectation is and what happens to a run when one fails, see expectations.

Decide what to check

Check data at the points where a wrong value would spread:

  • Inputs from outside: check data from vendors, partner teams or APIs before anything is built on it.
  • Model outputs: check that a transformation produces the result you expect before you put it in production.

Check what the schema can't. The output schema of a model already checks each column's type and whether it can be null. An expectation checks values and relations between columns, such as a timestamp that must come after another one.

Write the expectation

An expectation is a Python function decorated with @bauplan.expectation() and @bauplan.python that reads a model or a table and returns True if the data is acceptable or False if it is not. You can keep expectations in their own top-level file, such as expectations.py, but it isn't mandatory.

Read only the columns you check. Give the input a projection_schema with the checked columns, so the expectation doesn't load the whole table.

Bauplan's standard expectations cover nulls, accepted values, uniqueness, column means and derived columns; for the full list, see the standard expectations reference. Use one of the standard expectations when one fits and no custom logic is needed:

from typing import Annotated
import bauplan
import pyarrow as pa
from bauplan.standard_expectations import expect_column_all_unique

class OrderIds(bauplan.TableSchema):
"""The column this expectation checks."""

order_id: bauplan.Int64

@bauplan.expectation()
@bauplan.python("3.13")
def test_order_ids_unique(
orders: Annotated[pa.Table, bauplan.Model("clean_orders", projection_schema=OrderIds)],
) -> bool:
"""Check that every order appears only once."""
return expect_column_all_unique(orders, "order_id")

If no standard expectation fits

Write the check as a function that returns a boolean. If the rule compares columns of the same row, filter the rows that break it and count them. For example, a driver's arrival must not come before the ride request:

from typing import Annotated

import bauplan
import pyarrow as pa

class TripTimingColumns(bauplan.TableSchema):
"""Trip timestamps used to check event ordering."""

request_datetime: bauplan.TimestampMicroUTC | None
on_scene_datetime: bauplan.TimestampMicroUTC | None

@bauplan.expectation()
@bauplan.python("3.13", pip={"polars": "1.40.0"})
def test_on_scene_not_before_request(
trips: Annotated[
pa.Table,
bauplan.Model("normalized_taxi_trips", projection_schema=TripTimingColumns),
],
) -> bool:
"""Check that the driver's arrival doesn't come before the ride request."""
import polars as pl

# Null comparisons produce null masks, and DataFrame.filter drops them
reversed_timestamps = pl.col("on_scene_datetime") < pl.col("request_datetime")
violation_count = pl.DataFrame(trips).filter(reversed_timestamps).height

Decide how the check treats nulls. If the columns can be null, decide whether a row with a null passes or fails. In the example, rows where either timestamp is null are left out of the comparison.

assert violation_count == 0, (
f"{violation_count} rows have on_scene_datetime before request_datetime"
)
return True

Fail with a message. The assert makes the failed run report how many rows broke the rule. A bare False only tells you that the check failed.

The whole expectation
from typing import Annotated

import bauplan
import pyarrow as pa

class TripTimingColumns(bauplan.TableSchema):
"""Trip timestamps used to check event ordering."""

request_datetime: bauplan.TimestampMicroUTC | None
on_scene_datetime: bauplan.TimestampMicroUTC | None

@bauplan.expectation()
@bauplan.python("3.13", pip={"polars": "1.40.0"})
def test_on_scene_not_before_request(
trips: Annotated[
pa.Table,
bauplan.Model("normalized_taxi_trips", projection_schema=TripTimingColumns),
],
) -> bool:
"""Check that the driver's arrival doesn't come before the ride request."""
import polars as pl

# Null comparisons produce null masks, and DataFrame.filter drops them
reversed_timestamps = pl.col("on_scene_datetime") < pl.col("request_datetime")
violation_count = pl.DataFrame(trips).filter(reversed_timestamps).height

assert violation_count == 0, (
f"{violation_count} rows have on_scene_datetime before request_datetime"
)
return True

Run the pipeline and fix what fails

Run the pipeline on your branch as usual. If an expectation fails, the run halts and nothing it wrote reaches the branch.

To fix the failure, change the model that produces the data, not the expectation. If the rows are wrong at the source, fix them where they're produced. If you decide to drop them, filter them at the end of the producing model:

# At the end of the model the expectation reads
result = result.filter(
pl.col("on_scene_datetime").is_null()
| pl.col("request_datetime").is_null()
| (pl.col("on_scene_datetime") >= pl.col("request_datetime"))
)

return result.to_arrow()

Run the pipeline again to confirm the expectation passes.