Jump to solution
Verify

The Fix

pip install pydantic==1.10.20

Based on closed pydantic/pydantic issue #9887 · 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 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])
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 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).
Production impact:
  • 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

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!”
@sydney-runkle · 2024-08-16 · confirmation · source
“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…”
@sydney-runkle · 2024-07-17 · source
“Could you try on 2.8? Seems like it was fixed there.”
@Viicos · 2024-07-13 · source
“I just tried with 2.8.2 and can still reproduce the issue, yes”
@arthur-encord · 2024-07-15 · source

Failure Signature (Search String)

  • KeyError: '__main__.AndCondition:4400097808[int:4381856056]'

Error Message

Stack trace
error.txt
Error Message ------------- KeyError: '__main__.AndCondition:4400097808[int:4381856056]'

Minimal Reproduction

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

When NOT to use: This fix should not be applied if the models are not parametrized or if the caching behavior is critical.

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:

When NOT to use: This fix should not be applied if the models are not parametrized or if the caching behavior is critical.

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.

Get updates

We publish verified fixes weekly. No spam.

Subscribe

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

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.