[KEP-7] Dataset Validation #5562
Replies: 5 comments 5 replies
|
Quick update from Neeraj (Pandera maintainer + Kedro user) on the annotation style question: he confirmed that |
|
Is there a way to disable this validation even when the model is defined? My use case is from the IDE I probably don't want to directly use this approach, but rather having some kind of API to validate dataset from the extension, and report it as a diagnosis. So I will need:
|
|
Overall, I'm very excited to see native Pandera integration in Kedro. However, I do think there are some issues that need to be addressed. On schema conflicts and schema definition location
I (still) think this is concerning. I also think this is fundamentally confusing from a design perspective. If validation is tied to datasets, great, clearly define the schema on the dataset. https://pandera.readthedocs.io/en/latest/schema_inference.html#write-to-yaml provides a clear path to doing so with Kedro. However, if validation is tied to nodes, the schema should be tied to the function argument, not the dataset. I think I gave this example in the tech design:
In short, I think it's OK to choose one path or the other to start here (schema tied to dataset feels more common, intuitively, but I'm not sure), but I could easily see the desire to support both at some point down the road. Furthermore, I definitely think we should make sure that, should we focus on associating one schema per dataset (and caching results on subsequent loads, etc.), then the schema is more closely tied to the catalog. If do go the route of defining validation config alongside the dataset, I think most of the The other benefit of having the schema on the data catalog (as opposed to the nodes) is that you can potentially validate data used outside of the pipeline run flow. P.S. As I was writing this, I think the cleanest way to support caching even if you go with the schema-on-node approach is to have a mapping from dataset/schema combination to result, not just dataset to result. When should data validation happen?The proposal focuses on data validation after load, but data validation before write is also a very common pattern. If schemas are defined alongside the dataset definitions, I think it's fair to assume that the load and save schemas are the same. You may consider when to run validation (on save/load/both); hopefully, if you wrote the data, you don't need to revalidate on load (can use the cached result). Supporting configuration optionsWorth considering how you plan to support configuration options. If the answer for now is, use the environment variable, I guess that's OK? User friendlinessIf I understand correctly, if users inspect the At a minimum, I think the But, more importantly—why not hooks? The The other benefit of data catalog hooks are that they're also referenced when executing outside of pipeline runs. |
|
Thanks @deepyaman, @Adeikalam, and @noklam for the substantive comments. Reading them together, you've all pointed at the same gap from different angles:
I'm pausing the vote and pivoting the KEP to address all of this in one coherent design. The new shape: Schema-on-dataset with a pluggable validator protocol
I'll post the restructured KEP and reply individually to each of your comments to show how the new design maps to your specific points. Thank you everyone! |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Context: Explore a Kedro-First Approach to Data Validation (#5390)
Spike: Prototype Dataset Validation Using Pandera (#5391)
Authors: @SajidAlamQB
KEP shepherd: @SajidAlamQB
Tech design recording: Here
Tech design notes: #5391 (comment)
What are we trying to do?
Introduce native dataset validation in Kedro by extending the existing parameter validation pattern from KEP-1.
Users annotate a node input with a Pandera
DataFrameModel, and Kedro validates the DataFrame automatically at the I/O boundary, before the node function runs.The same
TypeExtractordiscovers both:The user-facing pattern is identical: type hints drive validation.
Example: Pandera
The DataFrame loaded from
companiesis validated againstCompaniesSchemabefore reaching the node.Validation failures raise
DataValidationError, listing every failure at once via Pandera’slazy=True.What this is not
This is not:
kedro[validation]How is it done today, and what are the limits of current practice?
Data validation in Kedro projects today is commonly implemented using hooks and third-party libraries.
The validation logic lives in a separate hook class registered in
settings.py, often referencing schemas defined in another module.Limits
What is new in this approach, and why will it be successful?
This proposes a dataset validation layer integrated with the existing Kedro Validation Framework from KEP-1.
Overview
Dataset validation reuses the validation infrastructure introduced for parameters and adds two new components:
_ValidatingDataset, a proxy wrappervalidate_dataframe(), the Pandera validation callThe same
TypeExtractordiscovers both parameter and dataset type hints:params:prefixparams:prefixArchitecture
The dataset validation extension consists of two new components plus reuse of existing components.
Core components
TypeExtractorLocation:
type_extractor.pyStatus: existing, extended
Shared discovery engine. Gains a new method,
extract_dataset_schemas(), which filters for PanderaDataFrameModelannotations on non-params:inputs._ValidatingDatasetLocation:
dataset_validator.pyStatus: new
Proxy that wraps a catalog dataset.
It:
load()save(), versioning, and dataset-specific methods, to the wrapped dataset via__getattr__validate_dataframe()Location:
dataset_validator.pyStatus: new
Runs:
and wraps failures in
DataValidationErrorwith dataset and schema context.DataValidationErrorLocation:
exceptions.pyStatus: new
Lives alongside:
ParameterValidationErrorModelInstantiationError_apply_dataset_validation()Location:
context.pyStatus: new
Wires the validation layer into
KedroContext._get_catalog().Integration points
Context-level integration
Dataset validation integrates with
KedroContext._get_catalog()via a new method:_apply_dataset_validation().This runs after the catalog is built and parameters are added, but before
after_catalog_createdhooks fire.Matching catalog datasets are replaced with
_ValidatingDatasetwrappers.The runner is unaware of this:
catalog.load()works exactly as it does today.Dataset validation flow
Key features and benefits
Strengths
TypeExtractor, around 40 todataset_validator.py, and around 30 tocontext.py._is_pandera_model()returnsFalseand the feature silently disappears without import errors.lazy=Truereports all validation failures in a single error, not one at a time.Drawbacks
load()for annotated datasets. This may be noticeable for very large DataFrames.Usage
Reference
prototype/validation-dataset-panderaWho cares? If successful, what difference will it make?
This proposal would:
What are the risks?
Performance
Validation adds overhead on every
load()for annotated datasets.Pandera is fast on pandas DataFrames, but the cost may be noticeable on very large data. This is mitigated by the feature being fully opt-in and by Pandera supporting native validation on most backends.
Lazy backend behaviour
We need to verify per backend that validation does not force unnecessary materialisation.
An earlier draft of this prototype assumed Pandera forced
.compute()on Dask. This was corrected during the tech design: Pandera supports Polars, Ibis, Spark, and Dask natively since approximately0.16.Only truly unsupported backends should be skipped.
Annotation style ambiguity
Pandera’s documented convention uses parameterised generics:
The prototype uses the direct schema annotation:
CompaniesSchemaBoth should be supported. The implementation must unwrap generics.
Compatibility issues reported in other frameworks, such as Dagster, need investigation.
Schema conflicts
If two pipelines annotate the same dataset with different schemas, the last one wins silently today.
Per-node schemas may be a legitimate use case, because different nodes may need different guarantees. This needs real-world examples from users.
Validation timing
Validation currently runs at catalog build time.
This may wrap datasets that are not part of the current run. We could defer to materialisation time to align with
--pipelinescoping. This relates to PR #5443.Maintenance
This adds surface area to Kedro core.
The risk is mitigated by reusing the existing validation framework.
How long will it take?
Around 2–3 weeks for the production implementation, assuming input and output validation are both in scope and annotation/backend questions are resolved during the initial PRs.
Appendix A: Proposed API changes
User-facing
Framework-facing
Packaging
Appendix B: Optional design sketch
graph TD %% ============================= %% SECTION 1: Startup (catalog build) %% ============================= subgraph CB["Startup: Catalog Build"] A["KedroContext._get_catalog()"] --> B["Build catalog from YAML"] B --> C["Validate parameters (existing)"] C --> D["_apply_dataset_validation() NEW"] D --> E["TypeExtractor.extract_dataset_schemas()"] E --> F["Schema Map<br/>{ds_name: schema_class}"] F --> G["Wrap matching datasets<br/>with _ValidatingDataset"] G --> H["Fire after_catalog_created hook"] end %% ============================= %% SECTION 2: Runtime (catalog.load) %% ============================= subgraph RT["Runtime: Per Node Load"] I["catalog.load('companies')"] --> J["_ValidatingDataset.load()"] J --> K["original.load()<br/>CSV/Parquet/etc."] K --> L["validate_dataframe()<br/>schema.validate(df, lazy=True)"] L -->|valid| M["Node receives validated DataFrame"] L -->|invalid| N["DataValidationError<br/>(all failures listed)"] end %% ============================= %% STYLES %% ============================= style CB fill:#fafafa,stroke:#ccc,stroke-width:1px style RT fill:#fafafa,stroke:#ccc,stroke-width:1px style A fill:#e3f2fd,stroke:#90caf9,stroke-width:1px style B fill:#f3e5f5,stroke:#ce93d8,stroke-width:1px style C fill:#fff9c4,stroke:#fbc02d,stroke-width:1px style D fill:#fce4ec,stroke:#f48fb1,stroke-width:2px style E fill:#ffe0b2,stroke:#ffb74d,stroke-width:1px style F fill:#e8f5e9,stroke:#81c784,stroke-width:1px style G fill:#fce4ec,stroke:#f48fb1,stroke-width:2px style H fill:#fff3e0,stroke:#ffb74d,stroke-width:1px style I fill:#e3f2fd,stroke:#90caf9,stroke-width:1px style J fill:#fce4ec,stroke:#f48fb1,stroke-width:2px style K fill:#f3e5f5,stroke:#ce93d8,stroke-width:1px style L fill:#fff9c4,stroke:#fbc02d,stroke-width:1px style M fill:#66bb6a,color:#fff,stroke-width:1px style N fill:#ef5350,color:#fff,stroke-width:1pxAppendix C: Optional rejected designs
Hooks, current approach
Rejected because validation has hidden control flow.
Validation lives in a separate hook class registered in
settings.py. Node functions show no indication that validation runs.This provides full flexibility, but low discoverability.
Wrapper dataset configured in YAML
Rejected because the user would need to configure a validating dataset class in
catalog.yml, with schemas referenced by string path.This has several drawbacks:
Pandera’s
@check_typesdecorator on node functionsPandera natively supports input/output validation via
@check_typeson functions.Rejected because it couples validation to the function itself. Validation runs every time the function is called, even in tests or notebooks where it may not be wanted.
Keeping validation at the framework/catalog layer means node functions remain plain Python.
Pluggable multi-backend validator interface
The parent issue (#5390) mentions extensibility for Great Expectations and other libraries.
Rejected for v1 to avoid premature abstraction.
The prototype validates that the abstraction point exists:
_is_pandera_modelandvalidate_dataframecould become part of aValidatorprotocol.This can be revisited once the Pandera-only implementation ships.
Open questions
type_extractorto target pipeline instead of all registered pipelines for Validation #5443, which scopesTypeExtractorto the target pipeline.CompaniesSchemaandpd.DataFrame[CompaniesSchema]be supported? We need to investigate Dagster-style compatibility issues.kedro-telemetrycreating a new catalog inside hooks trigger validation multiple times?All reactions