The Fix
pip install pydantic==2.10.4
Based on closed pydantic/pydantic issue #11033 · PR/commit linked
Production note: Most teams hit this during upgrades or environment changes. Roll out with a canary and smoke critical endpoints (health, OpenAPI/docs) before 100%.
@@ -4,8 +4,6 @@
if TYPE_CHECKING:
- from pydantic_core import CoreSchema
-
from ..config import JsonDict, JsonSchemaExtraCallable
from pydantic.plugin import TypeAlias
from typing_extensions import TypeAliasType
from typing import Annotated
from pydantic import BaseModel, BeforeValidator, AfterValidator
class TypeA(BaseModel):
pass
class TypeB(BaseModel):
pass
FieldType = Annotated[list[str], BeforeValidator(lambda x: x if isinstance(x, list) else [x], TypeA | TypeB)]
class Model(BaseModel):
field: FieldType
print(Model.model_json_schema())
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.10.4\nWhen NOT to use: This fix should not be applied if the schema relies on complex metadata patterns.\n\nOption C — Workaround\nis obviously to make the input schema.... a `TypeAliasType`! 😂\nWhen NOT to use: This fix should not be applied if the schema relies on complex metadata patterns.\n\n
Why This Fix Works in Production
- Mechanism: The JSON Schema generation fails due to unresolved references in union types with BeforeValidator
- Why the fix works: Partly fixes the issue with JSON Schema generation when using BeforeValidator with union types. (first fixed release: 2.10.4).
- If left unfixed, the same config can fail only in production (env differences), causing startup failures or partial feature outages.
Why This Breaks in Prod
- Shows up under Python 3.11 in real deployments (not just unit tests).
- The JSON Schema generation fails due to unresolved references in union types with BeforeValidator
- Surfaces as: Traceback (most recent call last):
Proof / Evidence
- GitHub issue: #11033
- Fix PR: https://github.com/pydantic/pydantic/pull/11085
- First fixed release: 2.10.4
- 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.57
Discussion
High-signal excerpts from the issue thread (symptoms, repros, edge-cases).
“I suspect this may be related to https://github.com/pydantic/pydantic/blob/aee6057378ccfec02126bf9c984a9b6d6b411777/pydantic/functional_validators.py#L130 (maybe it doesn't handle more complex schemas?)”
“Oh yeah the input_schema is {'type': 'union', 'choices': [{'type': 'definition-ref', 'schema_ref': '__main__.TypeA:5517702256'}, {'type': 'definition-ref', 'schema_ref': '__main__.TypeB:5805303600'}]} so the resolve_ref_schema call should be recursive over the”
“Which means the workaround is obviously to make the input schema.... a TypeAliasType! 😂”
“@Viicos, if I understand your point correctly, we'd be migrating this info out of metadata and onto the core schema directly? I'm in favor of…”
Error Message
Stack trace
Error Message
-------------
Traceback (most recent call last):
...
File "/<...>/.venv/lib/python3.11/site-packages/pydantic/json_schema.py", line 2245, in _add_json_refs
_add_json_refs(self.definitions[defs_ref])
~~~~~~~~~~~~~~~~^^^^^^^^^^
KeyError: '__main____TypeA-Input__1'
Minimal Reproduction
from pydantic.plugin import TypeAlias
from typing_extensions import TypeAliasType
from typing import Annotated
from pydantic import BaseModel, BeforeValidator, AfterValidator
class TypeA(BaseModel):
pass
class TypeB(BaseModel):
pass
FieldType = Annotated[list[str], BeforeValidator(lambda x: x if isinstance(x, list) else [x], TypeA | TypeB)]
class Model(BaseModel):
field: FieldType
print(Model.model_json_schema())
Environment
- Python: 3.11
- Pydantic: 2
What Broke
JSON Schema generation results in a KeyError, causing application failures.
Why It Broke
The JSON Schema generation fails due to unresolved references in union types with BeforeValidator
Fix Options (Details)
Option A — Upgrade to fixed release Safe default (recommended)
pip install pydantic==2.10.4
Use when you can deploy the upstream fix. It is usually lower-risk than long-lived workarounds.
Option C — Workaround Temporary workaround
is obviously to make the input schema.... a `TypeAliasType`! 😂
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/11085
First fixed release: 2.10.4
Last verified: 2026-02-09. Validate in your environment.
When NOT to Use This Fix
- This fix should not be applied if the schema relies on complex metadata patterns.
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.10.4 | Fixed |
Related Issues
No related fixes found.
Sources
We don’t republish the full GitHub discussion text. Use the links above for context.