Jump to solution
Verify

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

Jump to Verify Open PR/Commit
@@ -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
repro.py
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
verify
Re-run: uv run --python 3.13 --with pydantic==2.12 foo.py
fix.md
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).
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.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

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())”
@Viicos · 2025-10-08 · repro detail · source
“The issue reproduces on older Pydantic versions as well (and in general, is expected to fail: issubclass(str | None, <some_type>) isn't valid).”
@Viicos · 2025-10-07 · source
“Hi @Viicos, thanks for the quick reply”
@edvgui · 2025-10-07 · source
“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…”
@bijlpieter · 2025-10-08 · source

Failure Signature (Search String)

  • str | None

Error Message

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

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

When NOT to use: This fix is not applicable if the behavior of issubclass checks is intentionally required to fail.

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.

Get updates

We publish verified fixes weekly. No spam.

Subscribe

When NOT to Use This Fix

  • This fix is not applicable if the behavior of issubclass checks is intentionally required to fail.

Verify Fix

verify
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

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