Jump to solution
Verify

The Fix

pip install pydantic==2.9.0

Based on closed pydantic/pydantic issue #10239 · 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
@@ -243,7 +243,10 @@ def wrapped_model_post_init(self: BaseModel, context: Any, /) -> None: # These are instance variables, but have been assigned to `NoInitField` to trick the type checker. for instance_slot in '__pydantic_fields_set__', '__pydantic_extra__', '__pydantic_private__': - del namespace[instance_slot] + namespace.pop( + instance_slot,
repro.py
File "/tmp/.tox/py312-linux-pyqt5-no_cov/lib/python3.12/site-packages/pydantic_compat/_v2/mixin.py", line 58, in <module> class PydanticCompatMixin(metaclass=_MixinMeta): File "/tmp/.tox/py312-linux-pyqt5-no_cov/lib/python3.12/site-packages/pydantic_compat/_v2/mixin.py", line 55, in __new__ return super().__new__(cls, name, bases, namespace, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/tmp/.tox/py312-linux-pyqt5-no_cov/lib/python3.12/site-packages/pydantic/_internal/_model_construction.py", line 246, in __new__ del namespace[instance_slot] ~~~~~~~~~^^^^^^^^^^^^^^^ KeyError: '__pydantic_fields_set__'
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.9.0\nWhen NOT to use: Do not apply this fix if using a version of pydantic that does not require compatibility adjustments.\n\n

Why This Fix Works in Production

  • Trigger: class PydanticCompatMixin(metaclass=_MixinMeta):
  • Mechanism: The crash was caused by attempting to delete a key from the namespace that did not exist
  • Why the fix works: Fixes a crash when cleaning the namespace in `ModelMetaclass`, which was causing issues with compatibility for other libraries like `napari`. (first fixed release: 2.9.0).
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 crash was caused by attempting to delete a key from the namespace that did not exist
  • Surfaces as: File "/tmp/.tox/py312-linux-pyqt5-no_cov/lib/python3.12/site-packages/pydantic_compat/_v2/mixin.py", line 58, in <module>

Proof / Evidence

Discussion

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

“Yes, sorry for the deep pydantic-compat magic”
@tlambert03 · 2024-08-27 · confirmation · source
“Yes indeed no one is to blame here, compat libraries such as pydantic-compat have to use private stuff in some way :)”
@Viicos · 2024-08-27 · source
“pydantic-compat is doing pretty unusual things (subclassing our metaclass). I pushed a fix and checked that pydantic-compat tests are passing with the fix.”
@Viicos · 2024-08-27 · source
“> pydantic-compat is doing pretty unusual things (subclassing our metaclass)”
@Czaki · 2024-08-27 · source

Failure Signature (Search String)

  • class PydanticCompatMixin(metaclass=_MixinMeta):

Error Message

Stack trace
error.txt
Error Message ------------- File "/tmp/.tox/py312-linux-pyqt5-no_cov/lib/python3.12/site-packages/pydantic_compat/_v2/mixin.py", line 58, in <module> class PydanticCompatMixin(metaclass=_MixinMeta): File "/tmp/.tox/py312-linux-pyqt5-no_cov/lib/python3.12/site-packages/pydantic_compat/_v2/mixin.py", line 55, in __new__ return super().__new__(cls, name, bases, namespace, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/tmp/.tox/py312-linux-pyqt5-no_cov/lib/python3.12/site-packages/pydantic/_internal/_model_construction.py", line 246, in __new__ del namespace[instance_slot] ~~~~~~~~~^^^^^^^^^^^^^^^ KeyError: '__pydantic_fields_set__'

Minimal Reproduction

repro.py
File "/tmp/.tox/py312-linux-pyqt5-no_cov/lib/python3.12/site-packages/pydantic_compat/_v2/mixin.py", line 58, in <module> class PydanticCompatMixin(metaclass=_MixinMeta): File "/tmp/.tox/py312-linux-pyqt5-no_cov/lib/python3.12/site-packages/pydantic_compat/_v2/mixin.py", line 55, in __new__ return super().__new__(cls, name, bases, namespace, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/tmp/.tox/py312-linux-pyqt5-no_cov/lib/python3.12/site-packages/pydantic/_internal/_model_construction.py", line 246, in __new__ del namespace[instance_slot] ~~~~~~~~~^^^^^^^^^^^^^^^ KeyError: '__pydantic_fields_set__'

Environment

  • Python: 3.12
  • Pydantic: 2

What Broke

This led to a crash in napari's CI, causing build failures.

Why It Broke

The crash was caused by attempting to delete a key from the namespace that did not exist

Fix Options (Details)

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

pip install pydantic==2.9.0

When NOT to use: Do not apply this fix if using a version of pydantic that does not require compatibility adjustments.

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

Fix reference: https://github.com/pydantic/pydantic/pull/10242

First fixed release: 2.9.0

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

  • Do not apply this fix if using a version of pydantic that does not require compatibility adjustments.

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

Related Issues

No related fixes found.

Sources

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