Jump to solution
Verify

The Fix

pip install pydantic==2.12.5

Based on closed pydantic/pydantic issue #12492 · PR/commit linked

Production note: This usually shows up under retries/timeouts. Treat it as a side-effect risk until you can verify behavior with a canary + real traffic.

Jump to Verify Open PR/Commit
@@ -176,7 +176,12 @@ def from_prioritized_choices( remapped_defs_ref = next(x for x in alternatives if len(schemas_for_alternatives[x]) == 1) defs_remapping[original_defs_ref] = remapped_defs_ref - json_remapping[defs_to_json[original_defs_ref]] = defs_to_json[remapped_defs_ref] + + # Map all alternatives after the remapped one to the remapped one
repro.py
from pydantic.json_schema import GenerateJsonSchema from pydantic import BaseModel REF_TEMPLATE = "#/components/schemas/{model}" schema_generator = GenerateJsonSchema(ref_template=REF_TEMPLATE) class Level3(BaseModel): level_4: str class Level2(BaseModel): # if level_3 is a str, the test passes. If it's Level3, it fails. level_3: Level3 # level_3: str class Level1(BaseModel): level_2: Level2 inputs = [ (Level1, "validation", Level1.__pydantic_core_schema__), (Level1, "serialization", Level1.__pydantic_core_schema__), ] field_mapping, definitions = schema_generator.generate_definitions(inputs=inputs) assert "Level1" in definitions assert "Level1-Input" not in definitions assert "Level1-Output" not in definitions
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.12.5\nWhen NOT to use: Do not use this fix if your models do not require deduplication of schemas.\n\n

Why This Fix Works in Production

  • Trigger: Duplicated models in JSON schema generation
  • Mechanism: The json_remapping dictionary was not mapping intermediate simplified names for nested models
  • Why the fix works: This fix addresses an issue where nested models with identical Input/Output schemas were not being properly deduplicated, resulting in duplicate schemas. (first fixed release: 2.12.5).
Production impact:
  • If left unfixed, retries/timeouts can trigger duplicate external side-effects (double charges, duplicate emails, repeated writes).

Why This Breaks in Prod

  • The json_remapping dictionary was not mapping intermediate simplified names for nested models
  • Production symptom (often without a traceback): Duplicated models in JSON schema generation

Proof / Evidence

Discussion

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

“I asked my AI friend to fix this”
@marwan-alloreview · 2025-11-06 · source
“The fix looks good to me, feel free to send a PR if you'd like to.”
@Viicos · 2025-11-07 · source
“Hello, Any idea when there will be a release with this fix ? I see it's not in 2.12.5. Thank you !”
@marwan-alloreview · 2026-01-30 · source

Failure Signature (Search String)

  • Duplicated models in JSON schema generation
  • When Pydantic models are "deeply" "nested" (one of the attribute is also a Pydantic model), the `GenerateJsonSchema.generate_definitions` function sometimes creates duplicated
Copy-friendly signature
signature.txt
Failure Signature ----------------- Duplicated models in JSON schema generation When Pydantic models are "deeply" "nested" (one of the attribute is also a Pydantic model), the `GenerateJsonSchema.generate_definitions` function sometimes creates duplicated identical schema (e.g. Schema-Input and Schema-Output).

Error Message

Signature-only (no traceback captured)
error.txt
Error Message ------------- Duplicated models in JSON schema generation When Pydantic models are "deeply" "nested" (one of the attribute is also a Pydantic model), the `GenerateJsonSchema.generate_definitions` function sometimes creates duplicated identical schema (e.g. Schema-Input and Schema-Output).

Minimal Reproduction

repro.py
from pydantic.json_schema import GenerateJsonSchema from pydantic import BaseModel REF_TEMPLATE = "#/components/schemas/{model}" schema_generator = GenerateJsonSchema(ref_template=REF_TEMPLATE) class Level3(BaseModel): level_4: str class Level2(BaseModel): # if level_3 is a str, the test passes. If it's Level3, it fails. level_3: Level3 # level_3: str class Level1(BaseModel): level_2: Level2 inputs = [ (Level1, "validation", Level1.__pydantic_core_schema__), (Level1, "serialization", Level1.__pydantic_core_schema__), ] field_mapping, definitions = schema_generator.generate_definitions(inputs=inputs) assert "Level1" in definitions assert "Level1-Input" not in definitions assert "Level1-Output" not in definitions

Environment

  • Pydantic: 2

What Broke

Duplicate schemas were generated during JSON schema creation, causing confusion in API documentation.

Why It Broke

The json_remapping dictionary was not mapping intermediate simplified names for nested models

Fix Options (Details)

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

pip install pydantic==2.12.5

When NOT to use: Do not use this fix if your models do not require deduplication of schemas.

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

First fixed release: 2.12.5

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

  • Do not use this fix if your models do not require deduplication of schemas.

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

Related Issues

No related fixes found.

Sources

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