The Fix
pip install pydantic==2.6.3
Based on closed pydantic/pydantic issue #8709 · PR/commit linked
@@ -54,6 +54,12 @@ def inner(s: core_schema.CoreSchema, recurse: _core_utils.Recurse) -> core_schem
if definitions is None:
definitions = collect_definitions(schema)
+ # After we collect the definitions schemas, we must run through the discriminator
+ # application logic for each one. This step is crucial to prevent an exponential
+ # increase in complexity that occurs if schemas are left as 'union' schemas
from __future__ import annotations
from typing import Literal, Annotated
from pydantic import Field, TypeAdapter, BaseModel
class NestedState(BaseModel):
state_type: Literal["nested"]
substate: AnyState
# If this type is left out, the model behaves normally again
class LoopState(BaseModel):
state_type: Literal["loop"]
substate: AnyState
class LeafState(BaseModel):
state_type: Literal["leaf"]
AnyState = Annotated[NestedState | LoopState | LeafState, Field(..., discriminator="state_type")]
def build_nested_state(n):
if n <= 0:
return {"state_type": "leaf"}
else:
return {"state_type": "loop", "substate": {"state_type": "nested", "substate": build_nested_state(n-1)}}
adapter = TypeAdapter(AnyState)
# the next statement takes around 0.8s
adapter.validate_python(build_nested_state(9))
# the next statement takes around 3.5s
adapter.validate_python(build_nested_state(10))
# the next statement takes around 12.5s
adapter.validate_python(build_nested_state(11))
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.
Option A — Upgrade to fixed release\npip install pydantic==2.6.3\nWhen NOT to use: This fix should not be applied if the model does not use discriminated unions.\n\nOption C — Workaround\nfor this performance issue.\nWhen NOT to use: This fix should not be applied if the model does not use discriminated unions.\n\n
Why This Fix Works in Production
- Trigger: Recursive model with discriminated union has exponential time and space complexity for validation
- Mechanism: The recursive model validation had exponential time and space complexity due to improper handling of discriminated unions
- Why the fix works: Fixes exponential time and space complexity in recursive model validation by improving discriminated union schema generation. (first fixed release: 2.6.3).
- If left unfixed, this can cause silent data inconsistencies that propagate (bad cache entries, incorrect downstream decisions).
Why This Breaks in Prod
- Shows up under Python 3.11 in real deployments (not just unit tests).
- The recursive model validation had exponential time and space complexity due to improper handling of discriminated unions
- Production symptom (often without a traceback): Recursive model with discriminated union has exponential time and space complexity for validation
Proof / Evidence
- GitHub issue: #8709
- Fix PR: https://github.com/pydantic/pydantic/pull/8904
- First fixed release: 2.6.3
- Reproduced locally: No (not executed)
- Last verified: 2026-02-09
- Confidence: 0.75
- Did this fix it?: Yes (upstream fix exists)
- Own content ratio: 0.44
Discussion
High-signal excerpts from the issue thread (symptoms, repros, edge-cases).
“Hmm, this looks similar to this issue: https://github.com/pydantic/pydantic/issues/8688, which we fixed here https://github.com/pydantic/pydantic/pull/8702 for the 2.6.1 release”
“@sydney-runkle unfortunately still reproducible with 2.6.1, as David already noticed.”
“@palle-k, Thanks for reporting this. @davidhewitt, could you please take a look at this performance issue when you have a chance? Thank you!”
“The schema simplifies to: The issue is that the tagged union is not carried along so it's loss after the first union and thus performance…”
Failure Signature (Search String)
- Recursive model with discriminated union has exponential time and space complexity for validation
Copy-friendly signature
Failure Signature
-----------------
Recursive model with discriminated union has exponential time and space complexity for validation
python version: 3.11.7 (main, Dec 4 2023, 18:10:11) [Clang 15.0.0 (clang-1500.1.0.2.5)]
Error Message
Signature-only (no traceback captured)
Error Message
-------------
Recursive model with discriminated union has exponential time and space complexity for validation
python version: 3.11.7 (main, Dec 4 2023, 18:10:11) [Clang 15.0.0 (clang-1500.1.0.2.5)]
Minimal Reproduction
from __future__ import annotations
from typing import Literal, Annotated
from pydantic import Field, TypeAdapter, BaseModel
class NestedState(BaseModel):
state_type: Literal["nested"]
substate: AnyState
# If this type is left out, the model behaves normally again
class LoopState(BaseModel):
state_type: Literal["loop"]
substate: AnyState
class LeafState(BaseModel):
state_type: Literal["leaf"]
AnyState = Annotated[NestedState | LoopState | LeafState, Field(..., discriminator="state_type")]
def build_nested_state(n):
if n <= 0:
return {"state_type": "leaf"}
else:
return {"state_type": "loop", "substate": {"state_type": "nested", "substate": build_nested_state(n-1)}}
adapter = TypeAdapter(AnyState)
# the next statement takes around 0.8s
adapter.validate_python(build_nested_state(9))
# the next statement takes around 3.5s
adapter.validate_python(build_nested_state(10))
# the next statement takes around 12.5s
adapter.validate_python(build_nested_state(11))
Environment
- Python: 3.11
- Pydantic: 2
What Broke
Validation time and memory usage could grow indefinitely, leading to potential denial-of-service attacks.
Why It Broke
The recursive model validation had exponential time and space complexity due to improper handling of discriminated unions
Fix Options (Details)
Option A — Upgrade to fixed release Safe default (recommended)
pip install pydantic==2.6.3
Use when you can deploy the upstream fix. It is usually lower-risk than long-lived workarounds.
Option C — Workaround Temporary workaround
for this performance issue.
Use only if you cannot change versions today. Treat this as a stopgap and remove once upgraded.
Fix reference: https://github.com/pydantic/pydantic/pull/8904
First fixed release: 2.6.3
Last verified: 2026-02-09. Validate in your environment.
When NOT to Use This Fix
- This fix should not be applied if the model does not use discriminated unions.
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.
Version Compatibility Table
| Version | Status |
|---|---|
| 2.6.3 | Fixed |
Related Issues
No related fixes found.
Sources
We don’t republish the full GitHub discussion text. Use the links above for context.