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
@@ -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.
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))
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.
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
- 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
- GitHub issue: #12754
- Fix PR: https://github.com/pydantic/pydantic/pull/12760
- Reproduced locally: No (not executed)
- Last verified: 2026-02-09
- Confidence: 0.70
- Did this fix it?: Yes (upstream fix exists)
- Own content ratio: 0.56
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”
“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…”
“> 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…”
“> possibly impact existing setups Can you expand on this? What kind of impact are you concerned about? To me, this feels more like a…”
Failure Signature (Search String)
- import enum
Error Message
Stack trace
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
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.
Fix reference: https://github.com/pydantic/pydantic/pull/12760
Last verified: 2026-02-09. Validate in your environment.
When NOT to Use This Fix
- This fix is not suitable for cases where the order of set elements is significant.
Verify Fix
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.