Jump to solution
Verify

The Fix

pip install pydantic==1.10.18

Based on closed pydantic/pydantic issue #9319 · 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
@@ -212,6 +212,7 @@ def _handle_other_schemas(self, schema: core_schema.CoreSchema, f: Walk) -> core schema: core_schema.CoreSchema | None = ser_schema.get('schema', None) if schema is not None: + ser_schema = ser_schema.copy() ser_schema['schema'] = self.walk(schema, f) # type: ignore return_schema: core_schema.CoreSchema | None = ser_schema.get('return_schema', None)
repro.py
from collections.abc import Sequence from pydantic import PlainValidator, BaseModel from typing import Annotated class Model1(BaseModel, frozen=True): ... def parse_model1(x: object) -> Model1: return Model1() class Model2(BaseModel, frozen=True): model1: Annotated[Model1, PlainValidator(parse_model1)] class Model3(BaseModel, frozen=True): model2: Sequence[Model2] class Model4(BaseModel, frozen=True): model2: Model2
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.18\nWhen NOT to use: This fix should not be applied if the serialization schema behavior is expected to change.\n\nOption C — Workaround\nfor specific code base?\nWhen NOT to use: This fix should not be applied if the serialization schema behavior is expected to change.\n\n

Why This Fix Works in Production

  • Trigger: class Model4(BaseModel, frozen=True):
  • Mechanism: The core schema simplification fails when serialization schemas are involved, leading to a KeyError
  • Why the fix works: Fix core schema simplification when serialization schemas are involved in specific scenarios, addressing the KeyError issue. (first fixed release: 1.10.18).
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.12 in real deployments (not just unit tests).
  • The core schema simplification fails when serialization schemas are involved, leading to a KeyError
  • Surfaces as: Traceback (most recent call last):

Proof / Evidence

Discussion

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

“Hmph, definitely a bug. Thanks for the report. Will attempt to fix for 2.7.2”
@sydney-runkle · 2024-04-25 · source
“@jherbel We have a similar problem but we are not using Sequence, I'm wondering if you have any hints on what is the issue here…”
@cielecki · 2024-05-13 · source
“Hi @cielecki, by chance, I discovered yesterday that list instead of Sequence works in the scenario above. Maybe this helps, though you wrote that you…”
@jherbel · 2024-05-14 · source
“@jherbel thanks I decided to go with just going with "Any" in few places in order to make it work in the short term ...”
@cielecki · 2024-05-18 · source

Failure Signature (Search String)

  • class Model4(BaseModel, frozen=True):

Error Message

Stack trace
error.txt
Error Message ------------- Traceback (most recent call last): File "/home/joerg/tmp/pydantic_key_error.py", line 22, in <module> class Model4(BaseModel, frozen=True): File "/home/joerg/.pyenv/versions/3.12.1/lib/python3.12/site-packages/pydantic/_internal/_model_construction.py", line 202, in __new__ complete_model_class( File "/home/joerg/.pyenv/versions/3.12.1/lib/python3.12/site-packages/pydantic/_internal/_model_construction.py", line 549, in complete_model_class schema = gen_schema.clean_schema(schema) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/joerg/.pyenv/versions/3.12.1/lib/python3.12/site-packages/pydantic/_internal/_generate_schema.py", line 435, in clean_schema schema = simplify_schema_references(schema) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/joerg/.pyenv/versions/3.12.1/lib/python3.12/site-packages/pydantic/_internal/_core_utils.py", line 463, in simplify_schema_references schema = walk_core_schema(schema, count_refs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/joerg/.pyenv/versions/3.12.1/lib/python3.12/site-packages/pydantic/_internal/_core_utils.py", line 414, in walk_core_schema return f(schema.copy(), _dispatch) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/joerg/.pyenv/versions/3.12.1/lib/python3.12/site-packages/pydantic/_internal/_core_utils.py", line 459, in count_refs recurse(definiti ... (truncated) ...

Minimal Reproduction

repro.py
from collections.abc import Sequence from pydantic import PlainValidator, BaseModel from typing import Annotated class Model1(BaseModel, frozen=True): ... def parse_model1(x: object) -> Model1: return Model1() class Model2(BaseModel, frozen=True): model1: Annotated[Model1, PlainValidator(parse_model1)] class Model3(BaseModel, frozen=True): model2: Sequence[Model2] class Model4(BaseModel, frozen=True): model2: Model2

Environment

  • Python: 3.12
  • Pydantic: 2

What Broke

Users experience crashes with KeyError when defining models with serialization schemas.

Why It Broke

The core schema simplification fails when serialization schemas are involved, leading to a KeyError

Fix Options (Details)

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

pip install pydantic==1.10.18

When NOT to use: This fix should not be applied if the serialization schema behavior is expected to change.

Use when you can deploy the upstream fix. It is usually lower-risk than long-lived workarounds.

Option C — Workaround Temporary workaround

for specific code base?

When NOT to use: This fix should not be applied if the serialization schema behavior is expected to change.

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

First fixed release: 1.10.18

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 serialization schema behavior is expected to change.

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

Related Issues

No related fixes found.

Sources

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