The Fix
pip install pydantic==1.10.18
Based on closed pydantic/pydantic issue #9394 · PR/commit linked
@@ -2201,6 +2201,7 @@ def _apply_field_serializers(
ref = typing.cast('str|None', schema.get('ref', None))
if ref is not None:
+ self.defs.definitions[ref] = schema
schema = core_schema.definition_reference_schema(ref)
from enum import Enum
from pydantic import BaseModel, computed_field, field_serializer
class MyEnum(Enum):
A = "a"
B = "b"
class MyModel(BaseModel):
@computed_field
@property
def computed_a_or_b(self) -> MyEnum:
return MyEnum.B
@field_serializer("computed_a_or_b")
def serialize_my_enum(self, a_or_b: MyEnum) -> str:
return a_or_b.value
if __name__ == "__main__":
m = MyModel()
assert m.model_dump()['computed_a_or_b'] == "b"
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 serializer behavior is expected to remain unchanged.\n\nOption C — Workaround\nis to annotate the enum with a WrapSerializer.\nWhen NOT to use: This fix should not be applied if the serializer behavior is expected to remain unchanged.\n\n
Why This Fix Works in Production
- Trigger: KeyError: '__main__.MyEnum:5602143824'
- Mechanism: A key error occurs due to an internal schema generation bug with custom serializers for computed fields returning enums
- Why the fix works: Fixes a key error that occurs when using a custom serializer for computed fields returning enums. (first fixed release: 1.10.18).
- If left unfixed, this can cause silent data inconsistencies that propagate (bad cache entries, incorrect downstream decisions).
Why This Breaks in Prod
- Shows up under Python 3.12 in real deployments (not just unit tests).
- A key error occurs due to an internal schema generation bug with custom serializers for computed fields returning enums
- Surfaces as: KeyError: '__main__.MyEnum:5602143824'
Proof / Evidence
- GitHub issue: #9394
- Fix PR: https://github.com/pydantic/pydantic/pull/10200
- First fixed release: 1.10.18
- Reproduced locally: No (not executed)
- Last verified: 2026-02-09
- Confidence: 0.85
- Did this fix it?: Yes (upstream fix exists)
- Own content ratio: 0.60
Discussion
High-signal excerpts from the issue thread (symptoms, repros, edge-cases).
“FWIW: I don't use the Sequence as well (as mentioned above), and it seems like it has been introduced after I added the SkipValidation to…”
“One other observation to note, if I add a static field to the model with the same enum type, the field_serializer on the computed field…”
“Another workaround is to annotate the enum with a WrapSerializer.”
“@graydenshand, Thanks for reporting. Adding to our 2.7 issues milestone. Looks similar (at least in terms of the error) to https://github.com/pydantic/pydantic/issues/9319.”
Failure Signature (Search String)
- KeyError: '__main__.MyEnum:5602143824'
Error Message
Stack trace
Error Message
-------------
KeyError: '__main__.MyEnum:5602143824'
Minimal Reproduction
from enum import Enum
from pydantic import BaseModel, computed_field, field_serializer
class MyEnum(Enum):
A = "a"
B = "b"
class MyModel(BaseModel):
@computed_field
@property
def computed_a_or_b(self) -> MyEnum:
return MyEnum.B
@field_serializer("computed_a_or_b")
def serialize_my_enum(self, a_or_b: MyEnum) -> str:
return a_or_b.value
if __name__ == "__main__":
m = MyModel()
assert m.model_dump()['computed_a_or_b'] == "b"
Environment
- Python: 3.12
- Pydantic: 2
What Broke
Serialization of computed fields fails, causing application errors and potential data handling issues.
Why It Broke
A key error occurs due to an internal schema generation bug with custom serializers for computed fields returning enums
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
is to annotate the enum with a WrapSerializer.
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/10200
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 serializer behavior is expected to remain unchanged.
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.