Skip to content

feat: Make YAML serialization deterministic - #203

Open
olafmersmann wants to merge 3 commits into
mainfrom
feat-canonical_yaml
Open

feat: Make YAML serialization deterministic#203
olafmersmann wants to merge 3 commits into
mainfrom
feat-canonical_yaml

Conversation

@olafmersmann

Copy link
Copy Markdown
Collaborator

pydantic-yaml serializes model fields via model_dump_json() but set iteration is order depends on hash randomization, which is randomized per interpreter process, so to_yaml_str() produced a different ordering of these fields on every run even when the underlying data was unchanged.

Add a _CanonicalMixin used by new CanonicalModel/CanonicalRootModel base classes in schema.py. Each defines a single model_serializer(mode="wrap") that inspects a model's actual field values at dump time and sorts any that are a set into a list.

Fixes #202

olafmersmann and others added 3 commits July 28, 2026 15:38
pydantic-yaml serializes model fields via model_dump_json() but set iteration 
is order depends on hash randomization, which is randomized per interpreter
process, so `to_yaml_str()` produced a different ordering of these fields
on every run even when the underlying data was unchanged.

Add a `_CanonicalMixin` used by new `CanonicalModel`/`CanonicalRootModel`
base classes in schema.py. Each defines a single model_serializer(mode="wrap")
that inspects a model's actual field values at dump time and sorts any that
are a `set` into a list.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Canonical form for YAML

1 participant