Jump to solution
Verify

The Fix

pip install pydantic==1.10.20

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

Jump to Verify Open PR/Commit
@@ -817,8 +817,12 @@ def __class_getitem__( submodel = _generics.create_generic_submodel(model_name, origin, args, params) - # Update cache - _generics.set_cached_generic_type(cls, typevar_values, submodel, origin, args) + # Cache the generated model *only* if not in the process of parametrizing
repro.py
from typing import Generic, TypeVar from pydantic import BaseModel T = TypeVar('T') class Model1(BaseModel, Generic[T]): model2: 'Model2[T]' S = TypeVar('S') class Model2(BaseModel, Generic[S]): model1: Model1[S] Model1[str].model_rebuild() Model2[str].model_rebuild()
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==1.10.20\nWhen NOT to use: This fix should not be applied if the model relies on caching for performance in a non-parametrizing context.\n\n

Why This Fix Works in Production

  • Trigger: `KeyError` happening during schema cleaning with generic models
  • Mechanism: KeyError occurs during schema cleaning due to caching parametrized models while parametrizing another model
  • Why the fix works: Fixes a KeyError occurring during schema cleaning with generic models by avoiding caching parametrized models while parametrizing another model. (first fixed release: 1.10.20).

Why This Breaks in Prod

  • KeyError occurs during schema cleaning due to caching parametrized models while parametrizing another model
  • Surfaces as: `KeyError` happening during schema cleaning with generic models

Proof / Evidence

Discussion

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

“@Viicos, Great analysis above. Almost felt like I was live debugging :). Re the potential fix - seems reasonable enough, though I'm not as familiar…”
@sydney-runkle · 2024-09-11 · source
“> Re the potential fix - seems reasonable enough, though I'm not as familiar with the context here as you are”
@Viicos · 2024-09-30 · source
“Here is what happens in the first example: Before calling Model<1|2>[str].model_rebuild(), a huge amount of recursive calls are being made to generate the core schema…”
@Viicos · 2024-09-11 · source

Failure Signature (Search String)

  • `KeyError` happening during schema cleaning with generic models

Error Message

Stack trace
error.txt
Error Message ------------- `KeyError` happening during schema cleaning with generic models

Minimal Reproduction

repro.py
from typing import Generic, TypeVar from pydantic import BaseModel T = TypeVar('T') class Model1(BaseModel, Generic[T]): model2: 'Model2[T]' S = TypeVar('S') class Model2(BaseModel, Generic[S]): model1: Model1[S] Model1[str].model_rebuild() Model2[str].model_rebuild()

Environment

  • Pydantic: 2

What Broke

The application encounters a KeyError, leading to failures in schema validation and model instantiation.

Why It Broke

KeyError occurs during schema cleaning due to caching parametrized models while parametrizing another model

Fix Options (Details)

Option A — Upgrade to fixed release Safe default (recommended)

pip install pydantic==1.10.20

When NOT to use: This fix should not be applied if the model relies on caching for performance in a non-parametrizing context.

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/10704

First fixed release: 1.10.20

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 should not be applied if the model relies on caching for performance in a non-parametrizing context.

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
1.10.20 Fixed

Related Issues

No related fixes found.

Sources

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