Generate pydantic v2 BaseModel classes from schema-salad definitions
Project description
schema-salad-plus-pydantic
Generate pydantic v2 BaseModel classes from
schema-salad definitions.
What it does
Schema-salad defines record types, enums, inheritance, and unions in YAML.
This tool reads those definitions and emits a Python module of pydantic
BaseModel classes that can validate, parse, and serialize data conforming
to the schema.
Key features:
- Proper pydantic inheritance -- abstract bases declare fields, children inherit them, multiple inheritance works naturally.
- Schema annotations for types schema-salad can't express natively:
pydantic:type-- override the generated type annotation (e.g.dict[str, NativeStep])pydantic:alias-- set a Field alias for JSON keys that differ from the Python namepydantic:discriminator_field/pydantic:discriminator_map-- discriminated unions
- Enums -- multi-symbol enums become
str, Enumclasses; single-symbol enums becomeLiteral["value"]with auto-defaults. - Forward references --
model_rebuild()for all classes,from __future__ import annotations. - Permissive by default --
extra="allow",populate_by_name=True.
Installation
pip install schema-salad-plus-pydantic
Or with uv:
uv pip install schema-salad-plus-pydantic
Usage
CLI
Generate pydantic models from a schema-salad YAML file:
schema-salad-plus-pydantic generate schema.yml -o models.py
Pass --strict to emit models with extra="forbid" (reject unknown JSON keys); the default is permissive extra="allow".
Or write to stdout:
schema-salad-plus-pydantic generate schema.yml > models.py
Python API
from io import StringIO
from schema_salad_plus_pydantic.orchestrate import generate_from_schema
buf = StringIO()
generate_from_schema("path/to/schema.yml", buf)
code = buf.getvalue()
# Or write directly to a file
with open("models.py", "w") as f:
generate_from_schema("path/to/schema.yml", f)
# Optional: strict=True emits models with extra="forbid" (unknown keys rejected)
with open("models_strict.py", "w") as f:
generate_from_schema("path/to/schema.yml", f, strict=True)
Using the generated models
import json
from generated_models import MyRecord # the module you generated
# Validate a dict
obj = MyRecord.model_validate({"field": "value", "count": 42})
# Validate from JSON
with open("data.json") as f:
obj = MyRecord.model_validate(json.load(f))
# Access fields
print(obj.field)
print(obj.count)
# Serialize back to dict/JSON
print(obj.model_dump())
print(obj.model_dump_json(indent=2))
Schema annotations
Add pydantic:* keys to schema-salad field definitions to control the
generated type annotations. Requires a pydantic namespace declaration:
$namespaces:
pydantic: "https://example.org/pydantic#"
$graph:
- name: MyRecord
type: record
fields:
- name: steps
type: Any?
pydantic:type: "dict[str, Step]"
- name: format_version
type: string
pydantic:alias: "format-version"
- name: creator
type: Any?
pydantic:type: "list[Person | Organization] | None"
pydantic:discriminator_field: "class"
pydantic:discriminator_map: '{"Person": "Person", "Organization": "Organization"}'
Development
Setup with uv:
uv sync --group test --group lint --group mypy
Run checks:
make test # pytest
make lint # ruff + black
make mypy # type checking
Releasing
See RELEASE_CHECKLIST.md. Quick version:
make add-history # generate PR acknowledgements in HISTORY.rst
make release # tag, build, push (triggers PyPI publish via GitHub Actions)
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 schema_salad_plus_pydantic-0.1.4.tar.gz.
File metadata
- Download URL: schema_salad_plus_pydantic-0.1.4.tar.gz
- Upload date:
- Size: 19.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
105d3b9e9d29621027af31ffd8e6c08e536f7d5df10b0269d2eb8790b32d2808
|
|
| MD5 |
7f91e19d5e7f91088ceb1987c30a5c83
|
|
| BLAKE2b-256 |
097da836f3fd2a4bf0237ca782d6941761b2cee721b6ee443c07e68d1b9acc5f
|
Provenance
The following attestation bundles were made for schema_salad_plus_pydantic-0.1.4.tar.gz:
Publisher:
deploy.yaml on jmchilton/schema-salad-plus-pydantic
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
schema_salad_plus_pydantic-0.1.4.tar.gz -
Subject digest:
105d3b9e9d29621027af31ffd8e6c08e536f7d5df10b0269d2eb8790b32d2808 - Sigstore transparency entry: 1170490052
- Sigstore integration time:
-
Permalink:
jmchilton/schema-salad-plus-pydantic@a3a05381b49a1d0ed8635d667b858c84e803ce47 -
Branch / Tag:
refs/tags/0.1.4 - Owner: https://github.com/jmchilton
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
deploy.yaml@a3a05381b49a1d0ed8635d667b858c84e803ce47 -
Trigger Event:
push
-
Statement type:
File details
Details for the file schema_salad_plus_pydantic-0.1.4-py3-none-any.whl.
File metadata
- Download URL: schema_salad_plus_pydantic-0.1.4-py3-none-any.whl
- Upload date:
- Size: 15.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ef0851d8144659cc335d889ddedeffd6e113dd7e67598cbbb895ab18b914ed98
|
|
| MD5 |
271c2f8537ece4caf86e6f32df110569
|
|
| BLAKE2b-256 |
327bcd00639049e6898bc826e4faf60fd9042d068f33c2371667c703daa5be76
|
Provenance
The following attestation bundles were made for schema_salad_plus_pydantic-0.1.4-py3-none-any.whl:
Publisher:
deploy.yaml on jmchilton/schema-salad-plus-pydantic
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
schema_salad_plus_pydantic-0.1.4-py3-none-any.whl -
Subject digest:
ef0851d8144659cc335d889ddedeffd6e113dd7e67598cbbb895ab18b914ed98 - Sigstore transparency entry: 1170490096
- Sigstore integration time:
-
Permalink:
jmchilton/schema-salad-plus-pydantic@a3a05381b49a1d0ed8635d667b858c84e803ce47 -
Branch / Tag:
refs/tags/0.1.4 - Owner: https://github.com/jmchilton
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
deploy.yaml@a3a05381b49a1d0ed8635d667b858c84e803ce47 -
Trigger Event:
push
-
Statement type: