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%.
@@ -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)
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
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.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).
- 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
- GitHub issue: #9319
- Fix PR: https://github.com/pydantic/pydantic/pull/10155
- First fixed release: 1.10.18
- 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.34
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”
“@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…”
“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 thanks I decided to go with just going with "Any" in few places in order to make it work in the short term ...”
Failure Signature (Search String)
- class Model4(BaseModel, frozen=True):
Error Message
Stack trace
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
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
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?
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.
When NOT to Use This Fix
- This fix should not be applied if the serialization schema behavior is expected to change.
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.18 | Fixed |
Related Issues
No related fixes found.
Sources
We don’t republish the full GitHub discussion text. Use the links above for context.