Jump to solution
Verify

The Fix

pip install pydantic==2.12.2

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

Jump to Verify Open PR/Commit
@@ -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:
repro.py
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())
verify
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.
fix.md
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).
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 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

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.”
@Viicos · 2025-10-14 · source

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
signature.txt
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.txt
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

repro.py
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

When NOT to use: Do not use this fix if your model does not involve recursive generics.

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.

Get updates

We publish verified fixes weekly. No spam.

Subscribe

When NOT to Use This Fix

  • Do not use this fix if your model does not involve recursive generics.

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.

Version Compatibility Table

VersionStatus
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.