The Fix
pip install pydantic==2.12.0
Based on closed pydantic/pydantic issue #12349 · 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%.
@@ -10,7 +10,7 @@
from functools import cache, partial, wraps
from types import FunctionType
-from typing import TYPE_CHECKING, Any, Callable, Generic, Literal, NoReturn, cast
+from typing import TYPE_CHECKING, Any, Callable, Generic, Literal, NoReturn, TypeVar, cast
import pydantic
class T(pydantic.BaseModel):
a : str | None
print(T.model_fields["a"].annotation) # str | None
print(type(T.model_fields["a"].annotation)) # <class 'types.UnionType'>
print(issubclass(T.model_fields["a"].annotation, T)) # False
Re-run: uv run --python 3.13 --with pydantic==2.12 foo.py
Option A — Upgrade to fixed release\npip install pydantic==2.12.0\nWhen NOT to use: This fix is not applicable if the behavior of issubclass checks is intentionally required to fail.\n\n
Why This Fix Works in Production
- Trigger: str | None
- Mechanism: The custom __subclasscheck__ implementation in Pydantic models caused TypeError in issubclass checks
- Why the fix works: Addresses the issue with the custom __subclasscheck__ implementation in Pydantic models, which caused TypeError in issubclass checks. (first fixed release: 2.12.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.13 in real deployments (not just unit tests).
- The custom __subclasscheck__ implementation in Pydantic models caused TypeError in issubclass checks
- Surfaces as: str | None
Proof / Evidence
- GitHub issue: #12349
- Fix PR: https://github.com/pydantic/pydantic/pull/11669
- First fixed release: 2.12.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.47
Discussion
High-signal excerpts from the issue thread (symptoms, repros, edge-cases).
“You usually want to check if isinstance(obj, type) (or the equivalent inspect.isclass())”
“The issue reproduces on older Pydantic versions as well (and in general, is expected to fail: issubclass(str | None, <some_type>) isn't valid).”
“Hi @Viicos, thanks for the quick reply”
“The issue does not reproduce for me on 2.11 as written: vs So I figure something did change in the __instancecheck__, but this is probably…”
Failure Signature (Search String)
- str | None
Error Message
Stack trace
Error Message
-------------
str | None
<class 'types.UnionType'>
Traceback (most recent call last):
File "/home/guillaume/Documents/inmanta-module-git-ops/bug.py", line 9, in <module>
print(issubclass(T.model_fields["a"].annotation, T)) # False
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: issubclass() arg 1 must be a class
Stack trace
Error Message
-------------
$ uv run --python 3.13 --with pydantic==2.12 foo.py
str | None
<class 'types.UnionType'>
Traceback (most recent call last):
File "/home/pieter/foo.py", line 10, in <module>
print(issubclass(T.model_fields["a"].annotation, T)) # False
~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: issubclass() arg 1 must be a class
Minimal Reproduction
import pydantic
class T(pydantic.BaseModel):
a : str | None
print(T.model_fields["a"].annotation) # str | None
print(type(T.model_fields["a"].annotation)) # <class 'types.UnionType'>
print(issubclass(T.model_fields["a"].annotation, T)) # False
Environment
- Python: 3.13
- Pydantic: 2
Why It Broke
The custom __subclasscheck__ implementation in Pydantic models caused TypeError in issubclass checks
Fix Options (Details)
Option A — Upgrade to fixed release Safe default (recommended)
pip install pydantic==2.12.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/11669
First fixed release: 2.12.0
Last verified: 2026-02-09. Validate in your environment.
When NOT to Use This Fix
- This fix is not applicable if the behavior of issubclass checks is intentionally required to fail.
Verify Fix
Re-run: uv run --python 3.13 --with pydantic==2.12 foo.py
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.12.0 | Fixed |
Related Issues
No related fixes found.
Sources
We don’t republish the full GitHub discussion text. Use the links above for context.