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.
@@ -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
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
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.
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).
- 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
- GitHub issue: #12492
- Fix PR: https://github.com/pydantic/pydantic/pull/12494
- First fixed release: 2.12.5
- Reproduced locally: No (not executed)
- Last verified: 2026-02-09
- Confidence: 0.85
- Did this fix it?: Yes (upstream fix exists)
- Own content ratio: 0.45
Discussion
High-signal excerpts from the issue thread (symptoms, repros, edge-cases).
“I asked my AI friend to fix this”
“The fix looks good to me, feel free to send a PR if you'd like to.”
“Hello, Any idea when there will be a release with this fix ? I see it's not in 2.12.5. Thank you !”
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
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 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
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
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.
When NOT to Use This Fix
- Do not use this fix if your models do not require deduplication of schemas.
Verify Fix
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
| Version | Status |
|---|---|
| 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.