Great Expectations (GX)
When to Use
Load this skill when the user needs to:
- Validate data quality in pipelines (null checks, range checks, uniqueness, schema)
- Build Expectation Suites for tables or DataFrames
- Set up Checkpoints that run validations and route results
- Integrate GX with Airflow, dbt, Spark, or CI/CD pipelines
- Generate Data Docs for data quality reporting
- Write custom Expectations
Core Concepts
DataContext — entrypoint; manages config, stores, connections
└─ DataSource — connection to a data backend (Pandas, Spark, Postgres, etc.)
└─ DataAsset — logical dataset (table, DataFrame, file pattern)
└─ BatchDefinition — how to slice the asset (whole / partition by date)
└─ Batch — actual data sample to validate
ExpectationSuite — named collection of Expectation objects
ValidationDefinition — links (BatchDefinition → ExpectationSuite)
Checkpoint — runs one or more ValidationDefinitions; routes results to actions
└─ Actions: store results, update Data Docs, send Slack/email alerts
ValidationResult — pass/fail per expectation with metrics (unexpected %, samples)
Installation
bash
pip install great_expectations # base
pip install great_expectations[spark] # + PySpark support
pip install great_expectations[sqlalchemy] # + SQL (PostgreSQL, MySQL, etc.)
pip install great_expectations[trino] # + Trino/Starburst
DataContext Setup
python
import great_expectations as gx
# File-based context (persists config + results to disk)
context = gx.get_context(mode="file", project_root_dir="/opt/gx")
# Ephemeral context (in-memory, no persistence — useful in notebooks/CI)
context = gx.get_context(mode="ephemeral")
# Cloud context (GX Cloud SaaS) — requires GREAT_EXPECTATIONS_CLOUD_* env vars
context = gx.get_context(mode="cloud")
Data Sources
Pandas (DataFrame)
python
import pandas as pd
import great_expectations as gx
context = gx.get_context(mode="ephemeral")
df = pd.read_parquet("/data/silver/orders/dt=2024-01-15/part-0.parquet")
data_source = context.data_sources.add_pandas("orders-pandas")
data_asset = data_source.add_dataframe_asset(name="orders")
batch_def = data_asset.add_batch_definition_whole_dataframe("full")
batch = batch_def.get_batch(batch_parameters={"dataframe": df})
PySpark (DataFrame)
python
from pyspark.sql import SparkSession
spark = SparkSession.builder.getOrCreate()
spark_df = spark.table("silver.orders").filter("dt = '2024-01-15'")
data_source = context.data_sources.add_spark("orders-spark")
data_asset = data_source.add_dataframe_asset(name="orders")
batch_def = data_asset.add_batch_definition_whole_dataframe("full")
batch = batch_def.get_batch(batch_parameters={"dataframe": spark_df})
SQL (PostgreSQL / Trino / MySQL / SQLite)
python
# PostgreSQL
ds = context.data_sources.add_postgres(
name="warehouse",
connection_string="postgresql+psycopg2://user:pass@host:5432/db",
)
# Trino
ds = context.data_sources.add_sql(
name="trino",
connection_string="trino://user@host:8080/catalog/schema",
)
# Add table asset
table_asset = ds.add_table_asset(name="orders", table_name="silver.orders")
batch_def = table_asset.add_batch_definition_whole_table("full")
# Partitioned by date (runs incrementally)
from great_expectations.datasource.fluent.sql_datasource import ColumnSplitterDaily
daily_batch_def = table_asset.add_batch_definition_daily(
name="daily",
column="event_time",
)
batch = daily_batch_def.get_batch(
batch_parameters={"year": 2024, "month": 1, "day": 15}
)
Expectation Suites
python
suite = context.suites.add(
gx.core.ExpectationSuite(name="orders-silver-quality")
)
# Add expectations one by one
suite.add_expectation(gx.expectations.ExpectColumnValuesToNotBeNull(column="order_id"))
suite.add_expectation(gx.expectations.ExpectColumnValuesToNotBeNull(column="user_id"))
suite.add_expectation(gx.expectations.ExpectColumnValuesToBeBetween(
column="amount", min_value=0, max_value=1_000_000
))
# Save (for file-based context)
context.suites.update(suite)
Built-in Expectations
Completeness (Null Checks)
python
gx.expectations.ExpectColumnValuesToNotBeNull(column="order_id")
# Tolerate up to 5% nulls
gx.expectations.ExpectColumnValuesToNotBeNull(
column="discount_code",
mostly=0.95, # 0.0–1.0: fraction that MUST pass (1.0 = 100%)
)
Uniqueness
python
gx.expectations.ExpectColumnValuesToBeUnique(column="order_id")
# Compound uniqueness (multi-column)
gx.expectations.ExpectMultiColumnSumToEqual(column_list=["order_id", "line_item_id"])
# Distinct count range
gx.expectations.ExpectColumnUniqueValueCountToBeBetween(
column="status", min_value=1, max_value=10
)
Value Range
python
gx.expectations.ExpectColumnValuesToBeBetween(
column="amount", min_value=0, max_value=1_000_000
)
gx.expectations.ExpectColumnMinToBeBetween(column="amount", min_value=0)
gx.expectations.ExpectColumnMaxToBeBetween(column="amount", max_value=1_000_000)
gx.expectations.ExpectColumnMeanToBeBetween(column="amount", min_value=50, max_value=500)
gx.expectations.ExpectColumnMedianToBeBetween(column="amount", min_value=10, max_value=1000)
gx.expectations.ExpectColumnStdevToBeBetween(column="amount", min_value=0, max_value=10_000)
Set Membership
python
gx.expectations.ExpectColumnValuesToBeInSet(
column="status",
value_set=["placed", "shipped", "delivered", "cancelled", "returned"],
)
gx.expectations.ExpectColumnValuesToNotBeInSet(
column="status",
value_set=["DELETED", "ERROR"], # legacy values that shouldn't appear
)
# Categorical distribution check
gx.expectations.ExpectColumnDistinctValuesToBeInSet(
column="country_code",
value_set=["RU", "BY", "KZ", "UA"],
)
Pattern / Regex
python
gx.expectations.ExpectColumnValuesToMatchRegex(
column="email",
regex=r"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$",
)
gx.expectations.ExpectColumnValuesToNotMatchRegex(
column="phone",
regex=r"^\s+$", # not whitespace-only
)
gx.expectations.ExpectColumnValuesToMatchLikePattern(
column="order_id",
like_pattern="ORD-%", # SQL LIKE pattern
)
Type Checking
python
gx.expectations.ExpectColumnValuesToBeOfType(column="order_id", type_="int64")
gx.expectations.ExpectColumnValuesToBeInTypeList(
column="amount", type_list=["float64", "float32", "Decimal"]
)
Table-Level
python
# Row count
gx.expectations.ExpectTableRowCountToBeBetween(min_value=1000, max_value=10_000_000)
gx.expectations.ExpectTableRowCountToEqual(value=86_400) # exactly N rows
# Column presence
gx.expectations.ExpectTableColumnsToMatchOrderedList(
column_list=["order_id", "user_id", "amount", "status", "event_time"]
)
gx.expectations.ExpectTableColumnsToMatchSet(
column_set={"order_id", "user_id", "amount"},
exact_match=False, # allow extra columns
)
gx.expectations.ExpectTableColumnCountToBeBetween(min_value=5, max_value=50)
# Column existence
gx.expectations.ExpectColumnToExist(column="order_id")
Freshness / Recency
python
import datetime
gx.expectations.ExpectColumnMaxToBeBetween(
column="event_time",
min_value=datetime.datetime.utcnow() - datetime.timedelta(hours=2),
max_value=datetime.datetime.utcnow(),
)
Referential Integrity (Custom / SQL pattern)
python
# SQL-based: count orphan FK references
gx.expectations.UnexpectedRowsExpectation(
unexpected_rows_query="""
SELECT o.order_id
FROM orders o
LEFT JOIN customers c ON o.user_id = c.customer_id
WHERE c.customer_id IS NULL
""",
)
Severity Levels
python
gx.expectations.ExpectColumnValuesToNotBeNull(
column="order_id",
result_format="COMPLETE",
meta={"severity": "critical"}, # metadata (GX doesn't enforce programmatically)
)
# GX v1.x formal severity
gx.expectations.ExpectColumnValuesToBeBetween(
column="discount_pct",
min_value=0,
max_value=100,
severity="warning", # warning | critical (v1.x+)
)
Validation Definitions & Checkpoints
python
import great_expectations as gx
context = gx.get_context(mode="file", project_root_dir="/opt/gx")
# 1. Data source + asset + batch definition
ds = context.data_sources.add_postgres("warehouse", connection_string="postgresql+psycopg2://...")
asset = ds.add_table_asset(name="orders", table_name="silver.orders")
batch_def = asset.add_batch_definition_whole_table("full")
# 2. Expectation suite
suite = context.suites.add(gx.core.ExpectationSuite(name="orders-quality"))
suite.add_expectation(gx.expectations.ExpectColumnValuesToNotBeNull(column="order_id"))
suite.add_expectation(gx.expectations.ExpectTableRowCountToBeBetween(min_value=1))
suite.add_expectation(gx.expectations.ExpectColumnValuesToBeBetween(column="amount", min_value=0))
# 3. Validation definition
val_def = context.validation_definitions.add(
gx.core.ValidationDefinition(
name="orders-daily-validation",
data=batch_def,
suite=suite,
)
)
# 4. Checkpoint with actions
checkpoint = context.checkpoints.add(
gx.checkpoint.Checkpoint(
name="orders-daily-checkpoint",
validation_definitions=[val_def],
actions=[
gx.checkpoint.UpdateDataDocsAction(name="update_data_docs"),
gx.checkpoint.SlackNotificationAction(
name="slack_on_fail",
slack_webhook="https://hooks.slack.com/services/...",
notify_on="failure",
notify_with=["update_data_docs"],
),
],
result_format={"result_format": "COMPLETE"},
)
)
# 5. Run
result = checkpoint.run()
print(result.success) # True / False
print(result.describe()) # human-readable summary
Failing Fast on Critical Failures
python
result = checkpoint.run()
if not result.success:
failed_expectations = [
v for v in result.run_results.values()
for r in v["validation_result"].results
if not r.success
]
raise ValueError(f"Data quality FAILED: {len(failed_expectations)} expectations failed")
Airflow Integration
python
from airflow.sdk import dag, task
import pendulum
@dag(
schedule="@daily",
start_date=pendulum.datetime(2024, 1, 1, tz="UTC"),
catchup=False,
tags=["dq"],
)
def orders_dq_pipeline():
@task
def run_dq_checkpoint(ds: str) -> bool:
import great_expectations as gx
context = gx.get_context(mode="file", project_root_dir="/opt/airflow/gx")
checkpoint = context.checkpoints.get("orders-daily-checkpoint")
result = checkpoint.run(
batch_parameters={"year": int(ds[:4]), "month": int(ds[5:7]), "day": int(ds[8:10])}
)
if not result.success:
# Count critical failures
failures = sum(
1 for v in result.run_results.values()
for r in v["validation_result"].results
if not r.success
)
raise ValueError(f"DQ FAILED: {failures} expectations failed for {ds}")
return result.success
@task
def load_to_gold(dq_passed: bool, ds: str):
if not dq_passed:
raise ValueError("Cannot load to Gold: DQ check failed")
# ... actual load logic ...
dq_ok = run_dq_checkpoint()
load_to_gold(dq_ok)
orders_dq_pipeline()
dbt Integration
python
# Run GX after dbt run using dbt artifacts
# great_expectations/checkpoints/post_dbt_checkpoint.py
import great_expectations as gx
context = gx.get_context(mode="file")