Jump to solution
Verify

The Fix

pip install pydantic==1.10.18

Based on closed pydantic/pydantic issue #9394 · PR/commit linked

Jump to Verify Open PR/Commit
@@ -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)
repro.py
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"
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 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).
Production impact:
  • 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

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…”
@resurtm · 2024-08-12 · source
“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…”
@graydenshand · 2024-05-05 · source
“Another workaround is to annotate the enum with a WrapSerializer.”
@graydenshand · 2024-05-05 · source
“@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.”
@sydney-runkle · 2024-05-14 · source

Failure Signature (Search String)

  • KeyError: '__main__.MyEnum:5602143824'

Error Message

Stack trace
error.txt
Error Message ------------- KeyError: '__main__.MyEnum:5602143824'

Minimal Reproduction

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

When NOT to use: This fix should not be applied if the serializer behavior is expected to remain unchanged.

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.

When NOT to use: This fix should not be applied if the serializer behavior is expected to remain unchanged.

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.

Get updates

We publish verified fixes weekly. No spam.

Subscribe

When NOT to Use This Fix

  • This fix should not be applied if the serializer behavior is expected to remain unchanged.

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.