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%.
@@ -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,
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__'
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==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).
- 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
- GitHub issue: #10239
- Fix PR: https://github.com/pydantic/pydantic/pull/10242
- First fixed release: 2.9.0
- 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.44
Discussion
High-signal excerpts from the issue thread (symptoms, repros, edge-cases).
“Yes, sorry for the deep pydantic-compat magic”
“Yes indeed no one is to blame here, compat libraries such as pydantic-compat have to use private stuff in some way :)”
“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.”
“> pydantic-compat is doing pretty unusual things (subclassing our metaclass)”
Failure Signature (Search String)
- class PydanticCompatMixin(metaclass=_MixinMeta):
Error Message
Stack trace
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
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
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.
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
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 |
|---|---|
| 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.