Skip to content

feat: Make YAML serialization deterministic - #219

Merged
kvdblom merged 3 commits into
mainfrom
feat-canonical_yaml
Nov 2, 2026
Merged

kvdblom merged 3 commits into
mainfrom
feat-canonical_yaml

Conversation

@olafmersmann

@olafmersmann olafmersmann commented Nov 2, 2026 •

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

github-actions Bot and others added 2 commits October 26, 2026 14:04
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.
@olafmersmann
olafmersmann requested a review from kvdblom November 2, 2026 12:11
@kvdblom

kvdblom commented Nov 2, 2026

Copy link
Copy Markdown
Collaborator

This would resolve #202 right?

@olafmersmann

Copy link
Copy Markdown
Collaborator Author

This would resolve #202 right?

Yes. It was the broken PR I closed because it had some merge conflict that couldn't be easily resolved.

@kvdblom kvdblom linked an issue Nov 2, 2026 that may be closed by this pull request
@kvdblom

kvdblom commented Nov 2, 2026

Copy link
Copy Markdown
Collaborator

This would resolve #202 right?

Yes. It was the broken PR I closed because it had some merge conflict that couldn't be easily resolved.

OK, I linked the issue now.

@kvdblom
kvdblom merged commit a3152a9 into main Nov 2, 2026
13 checks passed
@olafmersmann
olafmersmann deleted the feat-canonical_yaml branch November 2, 2026 12:37
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