The Fix
pip install pydantic==1.10.20
Based on closed pydantic/pydantic issue #9887 · PR/commit linked
@@ -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
from typing import Generic, TypeVar
from pydantic import BaseModel
LeafType = TypeVar("LeafType")
class AndCondition(BaseModel, Generic[LeafType]):
and_conditions: list["LeafType | AndCondition | OrCondition"]
class OrCondition(BaseModel, Generic[LeafType]):
or_conditions: list["LeafType | OrCondition | AndCondition"]
AndCondition[int](and_conditions=[1, 2])
OrCondition[int](or_conditions=[3, 4])
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==1.10.20\nWhen NOT to use: This fix should not be applied if the models are not parametrized or if the caching behavior is critical.\n\nOption C — Workaround\nthat is working on 2.9:\nWhen NOT to use: This fix should not be applied if the models are not parametrized or if the caching behavior is critical.\n\n
Why This Fix Works in Production
- Trigger: KeyError: '__main__.AndCondition:4400097808[int:4381856056]'
- Mechanism: A caching issue with parametrized models caused KeyErrors when instantiating models that depend on each other
- Why the fix works: Fixes a caching issue with parametrized models that caused KeyErrors when instantiating models that depend on each other. (first fixed release: 1.10.20).
- If left unfixed, this can cause silent data inconsistencies that propagate (bad cache entries, incorrect downstream decisions).
Why This Breaks in Prod
- A caching issue with parametrized models caused KeyErrors when instantiating models that depend on each other
- Surfaces as: KeyError: '__main__.AndCondition:4400097808[int:4381856056]'
Proof / Evidence
- GitHub issue: #9887
- Fix PR: https://github.com/pydantic/pydantic/pull/10704
- First fixed release: 1.10.20
- Reproduced locally: No (not executed)
- Last verified: 2026-02-09
- Confidence: 0.85
- Did this fix it?: Yes (upstream fix exists)
- Own content ratio: 0.59
Discussion
High-signal excerpts from the issue thread (symptoms, repros, edge-cases).
“Alrighty, I'm unable to repro on main with pydantic-core==2.22.0. Closing as resolved. Let me know if anyone still encounters this issue with the above versions!”
“Can confirm I'm getting the issue on v2.8.2 as well. Thanks for the bug report @arthur-encord! Maybe this is related to https://github.com/pydantic/pydantic/issues/9319, based on the…”
“Could you try on 2.8? Seems like it was fixed there.”
“I just tried with 2.8.2 and can still reproduce the issue, yes”
Failure Signature (Search String)
- KeyError: '__main__.AndCondition:4400097808[int:4381856056]'
Error Message
Stack trace
Error Message
-------------
KeyError: '__main__.AndCondition:4400097808[int:4381856056]'
Minimal Reproduction
from typing import Generic, TypeVar
from pydantic import BaseModel
LeafType = TypeVar("LeafType")
class AndCondition(BaseModel, Generic[LeafType]):
and_conditions: list["LeafType | AndCondition | OrCondition"]
class OrCondition(BaseModel, Generic[LeafType]):
or_conditions: list["LeafType | OrCondition | AndCondition"]
AndCondition[int](and_conditions=[1, 2])
OrCondition[int](or_conditions=[3, 4])
Environment
- Pydantic: 2
What Broke
Users experienced KeyErrors when trying to instantiate dependent BaseModels, leading to application crashes.
Why It Broke
A caching issue with parametrized models caused KeyErrors when instantiating models that depend on each other
Fix Options (Details)
Option A — Upgrade to fixed release Safe default (recommended)
pip install pydantic==1.10.20
Use when you can deploy the upstream fix. It is usually lower-risk than long-lived workarounds.
Option C — Workaround Temporary workaround
that is working on 2.9:
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/10704
First fixed release: 1.10.20
Last verified: 2026-02-09. Validate in your environment.
When NOT to Use This Fix
- This fix should not be applied if the models are not parametrized or if the caching behavior is critical.
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 |
|---|---|
| 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.