Jump to solution
Verify

The Fix

Ensures deterministic JSON schema defaults by sorting sets, addressing the issue of non-deterministic ordering in JSON schema generation when using unordered collections as defaults.

Based on closed pydantic/pydantic issue #12754 · PR/commit linked

Jump to Verify Open PR/Commit
@@ -965,6 +965,10 @@ print(Model(set_of_ints=['1', '2', '3']).set_of_ints) ``` +<h4>JSON Schema</h4> + +Pydantic does best effort to sort default values that are [`collections.abc.Set`][] instances.
repro.py
import enum import json import pydantic class MyEnum(str, enum.Enum): A = "a" B = "b" # strictly speaking, only 2 enum members are required to reproduce, but # it is more consistent the more members you have C = "c" D = "d" E = "e" F = "f" G = "g" class MyModel(pydantic.BaseModel): field: frozenset[MyEnum] = frozenset(MyEnum) schema = MyModel.model_json_schema() print(json.dumps(schema, indent=2))
verify
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.
fix.md
Option A — Apply the official fix\nEnsures deterministic JSON schema defaults by sorting sets, addressing the issue of non-deterministic ordering in JSON schema generation when using unordered collections as defaults.\nWhen NOT to use: This fix is not suitable for cases where the order of set elements is significant.\n\n

Why This Fix Works in Production

  • Trigger: import enum
  • Mechanism: The JSON schema generation fails to maintain consistent ordering for set-like defaults due to Python's hash randomization
Production impact:
  • If left unfixed, this can cause silent data inconsistencies that propagate (bad cache entries, incorrect downstream decisions).

Why This Breaks in Prod

  • The JSON schema generation fails to maintain consistent ordering for set-like defaults due to Python's hash randomization
  • Surfaces as: import enum

Proof / Evidence

Discussion

High-signal excerpts from the issue thread (symptoms, repros, edge-cases).

“This looks like a determinism issue in json schema generation rather than a schema design question”
@AghastyGD · 2026-01-31 · source
“I think we could go this way, but this will possibly impact existing setups, so we'll have to wait for https://github.com/pydantic/pydantic/labels/v3 if we want to…”
@Viicos · 2026-01-31 · source
“> I think we could go this way, but this will possibly impact existing setups, so we'll have to wait for v3 Under consideration for…”
@AghastyGD · 2026-01-31 · source
“> possibly impact existing setups Can you expand on this? What kind of impact are you concerned about? To me, this feels more like a…”
@cj81499 · 2026-01-31 · source

Failure Signature (Search String)

  • import enum

Error Message

Stack trace
error.txt
Error Message ------------- import enum import json import pydantic class MyEnum(str, enum.Enum): A = "a" B = "b" # strictly speaking, only 2 enum members are required to reproduce, but # it is more consistent the more members you have C = "c" D = "d" E = "e" F = "f" G = "g" class MyModel(pydantic.BaseModel): field: frozenset[MyEnum] = frozenset(MyEnum) schema = MyModel.model_json_schema() print(json.dumps(schema, indent=2))

Minimal Reproduction

repro.py
import enum import json import pydantic class MyEnum(str, enum.Enum): A = "a" B = "b" # strictly speaking, only 2 enum members are required to reproduce, but # it is more consistent the more members you have C = "c" D = "d" E = "e" F = "f" G = "g" class MyModel(pydantic.BaseModel): field: frozenset[MyEnum] = frozenset(MyEnum) schema = MyModel.model_json_schema() print(json.dumps(schema, indent=2))

Environment

  • Pydantic: 2

What Broke

Generated JSON schema differs across runs, causing CI validation failures against checked-in schema.

Why It Broke

The JSON schema generation fails to maintain consistent ordering for set-like defaults due to Python's hash randomization

Fix Options (Details)

Option A — Apply the official fix

Ensures deterministic JSON schema defaults by sorting sets, addressing the issue of non-deterministic ordering in JSON schema generation when using unordered collections as defaults.

When NOT to use: This fix is not suitable for cases where the order of set elements is significant.

Fix reference: https://github.com/pydantic/pydantic/pull/12760

Last verified: 2026-02-09. Validate in your environment.

Get updates

We publish verified fixes weekly. No spam.

Subscribe

When NOT to Use This Fix

  • This fix is not suitable for cases where the order of set elements is significant.

Verify Fix

verify
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.

Did This Fix Work in Your Case?

Quick signal helps us prioritize which fixes to verify and improve.

Prevention

  • Add a CI check that diffs key outputs after upgrades (OpenAPI schema snapshots, JSON payload shapes, CLI output).
  • Upgrade behind a canary and run integration tests against the canary before 100% rollout.

Related Issues

No related fixes found.

Sources

We don’t republish the full GitHub discussion text. Use the links above for context.