The Fix
pip install pydantic==2.6.0
Based on closed pydantic/pydantic issue #8633 · 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%.
@@ -334,6 +334,7 @@ def inspect_namespace( # noqa C901
isinstance(value, type)
and value.__module__ == namespace['__module__']
+ and '__qualname__' in namespace
and value.__qualname__.startswith(namespace['__qualname__'])
):
from pydantic import create_model, BaseModel
class A:
pass
class StaticModel(BaseModel):
a_cls: type[A] = A # this works fine
DynamicModel = create_model("DynamicModel", a_cls=(type[A], A))
"""
Traceback (most recent call last):
File "path\to\test.py", line 8, in <module>
DynamicModel = create_model("DynamicModel", a_cls=(type[A], A))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "path\to\.venv\Lib\site-packages\pydantic\main.py", line 1490, in create_model
return meta(
^^^^^
File "path\to\.venv\Lib\site-packages\pydantic\_internal\_model_construction.py", line 92, in __new__
private_attributes = inspect_namespace(
^^^^^^^^^^^^^^^^^^
File "path\to\.venv\Lib\site-packages\pydantic\_internal\_model_construction.py", line 337, in inspect_namespace
and value.__qualname__.startswith(namespace['__qualname__'])
~~~~~~~~~^^^^^^^^^^^^^^^^
KeyError: '__qualname__'
"""
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.6.0\nWhen NOT to use: This fix should not be used if the model's namespace is intentionally designed without a '__qualname__'.\n\n
Why This Fix Works in Production
- Trigger: from pydantic import create_model, BaseModel
- Mechanism: The model's namespace lacks a '__qualname__', causing a KeyError when adding a type-type field
- Why the fix works: Adds a check on the existence of `__qualname__` in the namespace to avoid unexpected KeyError when setting a type-type default value in `create_model`. (first fixed release: 2.6.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
- The model's namespace lacks a '__qualname__', causing a KeyError when adding a type-type field
- Surfaces as: from pydantic import create_model, BaseModel
Proof / Evidence
- GitHub issue: #8633
- Fix PR: https://github.com/pydantic/pydantic/pull/8642
- First fixed release: 2.6.0
- 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.34
Discussion
High-signal excerpts from the issue thread (symptoms, repros, edge-cases).
“@anci3ntr0ck, Thanks for reporting this”
“@sydney-runkle Sure. Very glad to be a contributor to this great library 😆! I'll work on this soon 👌.”
Failure Signature (Search String)
- from pydantic import create_model, BaseModel
Error Message
Stack trace
Error Message
-------------
from pydantic import create_model, BaseModel
class A:
pass
class StaticModel(BaseModel):
a_cls: type[A] = A # this works fine
DynamicModel = create_model("DynamicModel", a_cls=(type[A], A))
"""
Traceback (most recent call last):
File "path\to\test.py", line 8, in <module>
DynamicModel = create_model("DynamicModel", a_cls=(type[A], A))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "path\to\.venv\Lib\site-packages\pydantic\main.py", line 1490, in create_model
return meta(
^^^^^
File "path\to\.venv\Lib\site-packages\pydantic\_internal\_model_construction.py", line 92, in __new__
private_attributes = inspect_namespace(
^^^^^^^^^^^^^^^^^^
File "path\to\.venv\Lib\site-packages\pydantic\_internal\_model_construction.py", line 337, in inspect_namespace
and value.__qualname__.startswith(namespace['__qualname__'])
~~~~~~~~~^^^^^^^^^^^^^^^^
KeyError: '__qualname__'
"""
Minimal Reproduction
from pydantic import create_model, BaseModel
class A:
pass
class StaticModel(BaseModel):
a_cls: type[A] = A # this works fine
DynamicModel = create_model("DynamicModel", a_cls=(type[A], A))
"""
Traceback (most recent call last):
File "path\to\test.py", line 8, in <module>
DynamicModel = create_model("DynamicModel", a_cls=(type[A], A))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "path\to\.venv\Lib\site-packages\pydantic\main.py", line 1490, in create_model
return meta(
^^^^^
File "path\to\.venv\Lib\site-packages\pydantic\_internal\_model_construction.py", line 92, in __new__
private_attributes = inspect_namespace(
^^^^^^^^^^^^^^^^^^
File "path\to\.venv\Lib\site-packages\pydantic\_internal\_model_construction.py", line 337, in inspect_namespace
and value.__qualname__.startswith(namespace['__qualname__'])
~~~~~~~~~^^^^^^^^^^^^^^^^
KeyError: '__qualname__'
"""
Environment
- Pydantic: 2
What Broke
KeyError occurs when creating models with type-type fields defined in the same module.
Why It Broke
The model's namespace lacks a '__qualname__', causing a KeyError when adding a type-type field
Fix Options (Details)
Option A — Upgrade to fixed release Safe default (recommended)
pip install pydantic==2.6.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/8642
First fixed release: 2.6.0
Last verified: 2026-02-09. Validate in your environment.
When NOT to Use This Fix
- This fix should not be used if the model's namespace is intentionally designed without a '__qualname__'.
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.6.0 | Fixed |
Related Issues
No related fixes found.
Sources
We don’t republish the full GitHub discussion text. Use the links above for context.