Jump to solution
Verify

The Fix

pip install pydantic==2.10.4

Based on closed pydantic/pydantic issue #9269 · 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
@@ -85,11 +85,27 @@ def is_literal(tp: Any, /) -> bool: -# TODO remove and replace with `get_args` when we drop support for Python 3.8 -# (see https://docs.python.org/3/whatsnew/3.9.html#id4). def literal_values(tp: Any, /) -> list[Any]:
repro.py
from typing import Literal from pydantic import BaseModel type TestType0 = Literal["test1", "test2"] type TestType1 = Literal[TestType0, Literal["test3"]] type TestType2 = Literal[Literal["test1", "test2"], Literal["test3"]] # TestType1 does not work # TestType2 works class Model(BaseModel): f: TestType1 print(Model.model_json_schema())
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.10.4\nWhen NOT to use: This fix should not be used if the code relies on the previous behavior of nested Literals.\n\n

Why This Fix Works in Production

  • Trigger: Nested Literals together with PEP-695 type statement are failed to produce json schema
  • Mechanism: Nested Literals with PEP 695 type aliases were not being unpacked correctly in JSON schema generation
  • Why the fix works: Recursively unpacks `Literal` values when using PEP 695 type aliases, addressing the issue of nested Literals not producing JSON schema correctly. (first fixed release: 2.10.4).
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

  • Nested Literals with PEP 695 type aliases were not being unpacked correctly in JSON schema generation
  • Production symptom (often without a traceback): Nested Literals together with PEP-695 type statement are failed to produce json schema

Proof / Evidence

Discussion

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

“@AlexanderPodorov, Thanks for the report. Seems like we don't fully support this syntax yet - PRs welcome with added support!”
@sydney-runkle · 2024-04-18 · source
“I've raised a discussion to clarify what should happen here”
@Viicos · 2024-11-01 · source

Failure Signature (Search String)

  • Nested Literals together with PEP-695 type statement are failed to produce json schema
  • print(Model.model_json_schema())
Copy-friendly signature
signature.txt
Failure Signature ----------------- Nested Literals together with PEP-695 type statement are failed to produce json schema print(Model.model_json_schema())

Error Message

Signature-only (no traceback captured)
error.txt
Error Message ------------- Nested Literals together with PEP-695 type statement are failed to produce json schema print(Model.model_json_schema())

Minimal Reproduction

repro.py
from typing import Literal from pydantic import BaseModel type TestType0 = Literal["test1", "test2"] type TestType1 = Literal[TestType0, Literal["test3"]] type TestType2 = Literal[Literal["test1", "test2"], Literal["test3"]] # TestType1 does not work # TestType2 works class Model(BaseModel): f: TestType1 print(Model.model_json_schema())

Environment

  • Pydantic: 2

What Broke

JSON schema generation fails when using nested Literals with PEP 695 type aliases.

Why It Broke

Nested Literals with PEP 695 type aliases were not being unpacked correctly in JSON schema generation

Fix Options (Details)

Option A — Upgrade to fixed release Safe default (recommended)

pip install pydantic==2.10.4

When NOT to use: This fix should not be used if the code relies on the previous behavior of nested Literals.

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/11114

First fixed release: 2.10.4

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 code relies on the previous behavior of nested Literals.

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

Related Issues

No related fixes found.

Sources

We don’t republish the full GitHub discussion text. Use the links above for context.