The Fix
pip install pydantic==2.12.2
Based on closed pydantic/pydantic issue #12396 · PR/commit linked
@@ -764,7 +764,7 @@ def _model_schema(self, cls: type[BaseModel]) -> core_schema.CoreSchema:
fields = getattr(cls, '__pydantic_fields__', {})
else:
- if not hasattr(cls, '__pydantic_fields__'):
+ if '__pydantic_fields__' not in cls.__dict__:
# This happens when we have a loop in the schema generation:
from typing import Generic, Optional, TypeVar
from pydantic import BaseModel
class ConfiguredBaseModel(BaseModel):
"""Base model to share configuration settings."""
# example model_config commented out since the bug seems present with or
# without the model_config set
# model_config = ConfigDict(
# extra="ignore",
# revalidate_instances="always",
# validate_assignment=True,
# )
T = TypeVar("T")
class Base(ConfiguredBaseModel, Generic[T]):
t: Optional[T]
class Other(ConfiguredBaseModel): # if we change this to inherit from BaseModel then the model validates as expected
children: "Base[Other]"
a = Other.model_validate({"children": {"t": {"children": {"t": None}}}})
print(a)
# Expected Output: children=Base[Other](t=Other(children=Base[Other](t=None)))
# Actual Output: children=Base[Other](t=Other())
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.12.2\nWhen NOT to use: Do not use this fix if your model does not involve recursive generics.\n\n
Why This Fix Works in Production
- Trigger: Unfortunately it seems that the bug is still there as soon as we change the original MRE to make the `Other` model class inherit from another model 🥲
- Mechanism: The validation logic for recursive generic models was incorrectly checking for parent classes
- Why the fix works: Fixes an issue with recursive generic models that inherit from a parent model class. (first fixed release: 2.12.2).
- If left unfixed, this can cause silent data inconsistencies that propagate (bad cache entries, incorrect downstream decisions).
Why This Breaks in Prod
- The validation logic for recursive generic models was incorrectly checking for parent classes
- Production symptom (often without a traceback): Unfortunately it seems that the bug is still there as soon as we change the original MRE to make the `Other` model class inherit from another model 🥲
Proof / Evidence
- GitHub issue: #12396
- Fix PR: https://github.com/pydantic/pydantic/pull/12398
- First fixed release: 2.12.2
- Reproduced locally: No (not executed)
- Last verified: 2026-02-09
- Confidence: 0.95
- Did this fix it?: Yes (upstream fix exists)
- Own content ratio: 0.42
Discussion
High-signal excerpts from the issue thread (symptoms, repros, edge-cases).
“Thanks for the report, I have a fix that I will backport in the next patch release.”
Failure Signature (Search String)
- Unfortunately it seems that the bug is still there as soon as we change the original MRE to make the `Other` model class inherit from another model 🥲
Copy-friendly signature
Failure Signature
-----------------
Unfortunately it seems that the bug is still there as soon as we change the original MRE to make the `Other` model class inherit from another model 🥲
class Other(ConfiguredBaseModel): # if we change this to inherit from BaseModel then the model validates as expected
Error Message
Signature-only (no traceback captured)
Error Message
-------------
Unfortunately it seems that the bug is still there as soon as we change the original MRE to make the `Other` model class inherit from another model 🥲
class Other(ConfiguredBaseModel): # if we change this to inherit from BaseModel then the model validates as expected
Minimal Reproduction
from typing import Generic, Optional, TypeVar
from pydantic import BaseModel
class ConfiguredBaseModel(BaseModel):
"""Base model to share configuration settings."""
# example model_config commented out since the bug seems present with or
# without the model_config set
# model_config = ConfigDict(
# extra="ignore",
# revalidate_instances="always",
# validate_assignment=True,
# )
T = TypeVar("T")
class Base(ConfiguredBaseModel, Generic[T]):
t: Optional[T]
class Other(ConfiguredBaseModel): # if we change this to inherit from BaseModel then the model validates as expected
children: "Base[Other]"
a = Other.model_validate({"children": {"t": {"children": {"t": None}}}})
print(a)
# Expected Output: children=Base[Other](t=Other(children=Base[Other](t=None)))
# Actual Output: children=Base[Other](t=Other())
Environment
- Pydantic: 2
What Broke
Models with recursive generics fail to validate correctly, leading to unexpected outputs.
Why It Broke
The validation logic for recursive generic models was incorrectly checking for parent classes
Fix Options (Details)
Option A — Upgrade to fixed release Safe default (recommended)
pip install pydantic==2.12.2
Use when you can deploy the upstream fix. It is usually lower-risk than long-lived workarounds.
Fix reference: https://github.com/pydantic/pydantic/pull/12398
First fixed release: 2.12.2
Last verified: 2026-02-09. Validate in your environment.
When NOT to Use This Fix
- Do not use this fix if your model does not involve recursive generics.
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.12.2 | Fixed |
Related Issues
No related fixes found.
Sources
We don’t republish the full GitHub discussion text. Use the links above for context.