Skip to content

feat: Make YAML serialization deterministic - #203

Closed
olafmersmann wants to merge 1 commit into
mainfrom
feat-canonical_yaml
Closed

olafmersmann wants to merge 1 commit 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

@kvdblom

kvdblom commented Oct 12, 2026

Copy link
Copy Markdown
Collaborator

This looks good. Only thing to check are the merge conflicts on problem.yaml and the schema (doesn't look too complicated). Then it should be good to go.

@olafmersmann

Copy link
Copy Markdown
Collaborator Author

All merge conflicts should be fixed now.

@olafmersmann

Copy link
Copy Markdown
Collaborator Author

Holding off until all problem pull requests are merged.

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

2 participants