Jump to solution
Verify

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%.

Jump to Verify Open PR/Commit
@@ -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__']) ):
repro.py
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__' """
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.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).
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

  • 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

Discussion

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

“@anci3ntr0ck, Thanks for reporting this”
@sydney-runkle · 2024-01-25 · source
“@sydney-runkle Sure. Very glad to be a contributor to this great library 😆! I'll work on this soon 👌.”
@anci3ntr0ck · 2024-01-25 · source

Failure Signature (Search String)

  • from pydantic import create_model, BaseModel

Error Message

Stack trace
error.txt
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

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

When NOT to use: This fix should not be used if the model's namespace is intentionally designed without a '__qualname__'.

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.

Get updates

We publish verified fixes weekly. No spam.

Subscribe

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

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