Python-native data contracts powered by a Canonical Contract Model and Pydantic.
Project description
ContractModel
Python-native data contracts built on a Canonical Contract Model (CCM) and Pydantic V2. Import contracts from ODCS, generate typed models, validate records and DataFrames, detect breaking changes, and export to JSON Schema, OpenAPI, Markdown, RDF, SHACL, and OWL — all through one stable internal representation.
Features
- CCM-first architecture — ODCS, Pydantic, and other formats adapt in and out of a single canonical model
- ODCS import/export — load and round-trip Open Data Contract Standard YAML with constraints and extensions
- Pydantic generation — produce validated model classes with constraints, enums, nested objects, and reverse mapping
- Validation modes —
STRICT,PERMISSIVE,SCHEMA_ONLY, andQUALITY_ONLY - Multi-format validation — records, JSON, CSV, Parquet, Pandas, and Polars (optional extras)
- Contract diffing — field-level changes with breaking-change classification and rename detection
- Exporters — JSON Schema, OpenAPI, Markdown, ODCS, RDF, SHACL, OWL
- Plugin SDK — entry-point discovery for validators, exporters, and registries
- CLI —
init,validate,diff,generate,export,publish, anddoctor
Installation
pip install contractmodel
Optional extras:
pip install "contractmodel[pandas]" # Pandas validation
pip install "contractmodel[polars]" # Polars validation
pip install "contractmodel[parquet]" # Parquet file validation
pip install "contractmodel[semantic]" # RDF / SHACL / OWL export
pip install "contractmodel[all]" # everything
Requires Python 3.10+. The contract CLI is included in the base install.
Quick start
from contractmodel import DataContract
from contractmodel.core.types import ValidationMode
contract = DataContract.from_odcs("examples/customer_events.odcs.yaml")
CustomerEvent = contract.to_pydantic()
result = contract.validate_record(
{
"event_id": "550e8400-e29b-41d4-a716-446655440000",
"customer_id": "C123",
"event_timestamp": "2026-06-23T12:00:00",
"event_type": "created",
},
mode=ValidationMode.STRICT,
)
print(result.success) # True
Load a native CCM contract:
contract = DataContract.from_yaml("examples/customer_events.ccm.yaml")
Compare contract versions:
diff = old_contract.diff(new_contract)
print(diff.is_breaking)
Export formats:
contract.to_json_schema()
contract.to_openapi()
contract.to_markdown()
contract.to_odcs()
contract.to_shacl() # requires contractmodel[semantic]
CLI
contract init contract.yaml
contract init myapp --template fastapi
contract validate contract.yaml data.json
contract validate contract.yaml data.json --output sarif
contract diff old.yaml new.yaml
contract generate pydantic contract.yaml --output models.py
contract export contract.yaml --to json-schema
contract export contract.yaml --to shacl
contract publish contract.yaml --registry https://registry.example.com
contract doctor
Validation modes
| Mode | Behavior |
|---|---|
STRICT |
Reject extra fields; full constraint validation |
PERMISSIVE |
Allow extra fields |
SCHEMA_ONLY |
Structure and types only |
QUALITY_ONLY |
Run CCM quality rules (freshness, completeness) |
Plugins
Register plugins via pyproject.toml entry points:
[project.entry-points."contractmodel.validators"]
my_validator = "my_package:MyValidator"
Run contract doctor to list discovered plugins and optional dependencies.
Architecture
All external representations flow through the CCM:
ODCS / YAML / JSON → CCM → Pydantic / Validation / Diff / Export
The CCM is format-agnostic. Adapters handle conversion; engines operate only on the canonical model. See docs/architecture/ and docs/tutorials/ for details.
Development
git clone https://github.com/eddiethedean/contractmodel.git
cd contractmodel
pip install -e ".[all]" pytest pytest-cov ruff mypy
pytest
ruff check src tests
mypy src
License
MIT — see LICENSE.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file contractmodel-0.1.1.tar.gz.
File metadata
- Download URL: contractmodel-0.1.1.tar.gz
- Upload date:
- Size: 48.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b8aa21004b791c1d1c423db0145a8fe5c3789d0054ad55fc6af0210626ee1a6c
|
|
| MD5 |
4e116ba8c9bc6d73c10fe4f0466c69da
|
|
| BLAKE2b-256 |
34f2f8373fbc5c3dc8dd94c4cedb1bd6ba382fc4bc0ccf01bc1defe21c10b706
|
File details
Details for the file contractmodel-0.1.1-py3-none-any.whl.
File metadata
- Download URL: contractmodel-0.1.1-py3-none-any.whl
- Upload date:
- Size: 37.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5d27c889be6577214a46a0afc61cf9d947762bc6fa4f5e1d8e4a9273d5981af5
|
|
| MD5 |
5d8e2ff2e63b704584a03f77f406ba48
|
|
| BLAKE2b-256 |
7730da046a476c510d6f70a323f00d9773ab76891ef53d87b6720381f69eedb4
|