Jump to solution
Verify

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

Jump to Verify Open PR/Commit
@@ -4,8 +4,6 @@ if TYPE_CHECKING: - from pydantic_core import CoreSchema - from ..config import JsonDict, JsonSchemaExtraCallable
repro.py
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())
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.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).
Production impact:
  • 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

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?)”
@thejcannon · 2024-12-03 · source
“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”
@thejcannon · 2024-12-04 · source
“Which means the workaround is obviously to make the input schema.... a TypeAliasType! 😂”
@thejcannon · 2024-12-04 · source
“@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…”
@sydney-runkle · 2024-12-06 · source

Error Message

Stack trace
error.txt
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

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

When NOT to use: This fix should not be applied if the schema relies on complex metadata patterns.

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`! 😂

When NOT to use: This fix should not be applied if the schema relies on complex metadata patterns.

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.

Get updates

We publish verified fixes weekly. No spam.

Subscribe

When NOT to Use This Fix

  • This fix should not be applied if the schema relies on complex metadata patterns.

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