How to create a new Expectation Suite without the CLI

In some environments, you might not be able to use the CLI to create a new Expectation Suite. This guide shows how to create a new Expectation Suite and start adding Expectations using Python code, for example in a Jupyter notebook. Note: If you want to get started creating Expectations with the fewest possible dependencies, e.g. without configuring a Data Context, you should check out How to quickly explore Expectations in a notebook.

Show Docs for V2 (Batch Kwargs) API

Prerequisites: This how-to guide assumes you have already:

This code is very similar to the boilerplate you see after creating an Expectation Suite using the CLI, with the only difference being that the Expectation Suite is created not loaded from the Data Context:

import great_expectations as gx

context = gx.data_context.DataContext()
suite = context.create_expectation_suite(
    "my_suite_name", overwrite_existing=True # Configure these parameters for your needs
)

This block just creates an empty Expectation Suite object. Next up, you want to create a Batch to start creating Expectations:

batch_kwargs = {
    "datasource": "my_datasource",
    "schema": "my_schema",
    "table": "my_table_name",
}
batch = context.get_batch(batch_kwargs, suite)

Note: The batch_kwargs depend on what type of data asset you want to connect to (a database table or view, Pandas datasource, etc.). See Creating Batches for your configuration. You can then start creating Expectations on your batch using the methods described in the Glossary of Expectations and eventually save the Suite to JSON:

# Start creating Expectations here
batch.expect_column_values_to_not_be_null('my_column')

...

# And save the final state to JSON
batch.save_expectation_suite(discard_failed_expectations=False)

This will create a JSON file with your Expectation Suite in the Store you have configured, which you can then load and use for Validation.

Show Docs for V3 (Batch Request) API

Prerequisites: This how-to guide assumes you have already:

This code is very similar to the boilerplate you see after creating an Expectation Suite using the CLI, with the only difference being that the Expectation Suite is created not loaded from the Data Context:

import great_expectations as gx
from great_expectations.core.batch import BatchRequest

context = gx.data_context.DataContext()
suite = context.create_expectation_suite(
    "my_suite_name", overwrite_existing=True # Configure these parameters for your needs
)

This block just creates an empty Expectation Suite object. Next up, you want to create a Validator to start creating Expectations:

batch_request = BatchRequest(
    datasource_name="my_datasource",
    data_connector_name="my_data_connector",
    data_asset_name="my_data_asset"
)
validator = context.get_validator(batch_request=batch_request, expectation_suite=suite)

Note: The batch_request depends on what type of data asset you want to connect to (a database table or view, Pandas dataframe, etc.). See Datasources Reference to learn more about specifying batch requests. You can then start creating Expectations based on your batch using the methods described in the Glossary of Expectations and eventually save the Suite to JSON:

# Start creating Expectations here
validator.expect_column_values_to_not_be_null('my_column')

...

# And save the final state to JSON
validator.save_expectation_suite(discard_failed_expectations=False)

This will create a JSON file with your Expectation Suite in the Store you have configured, which you can then load and use for Validation.