The Fix
pip install pydantic==1.10.19
Based on closed pydantic/pydantic issue #9402 · 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.
@@ -777,9 +777,11 @@ def literal_schema(self, schema: core_schema.LiteralSchema) -> JsonSchemaValue:
expected = [to_jsonable_python(v) for v in expected]
- result: dict[str, Any] = {'enum': expected}
+ result: dict[str, Any] = {}
if len(expected) == 1:
class CustomGenerateJsonSchema(GenerateJsonSchema):
def literal_schema(self, schema: core_schema.LiteralSchema) -> JsonSchemaValue:
"""Generates a JSON schema that matches a literal value.
Args:
schema: The core schema.
Returns:
The generated JSON schema.
"""
expected = [v.value if isinstance(v, Enum) else v for v in schema['expected']]
# jsonify the expected values
expected = [to_jsonable_python(v) for v in expected]
result: dict[str, Any] = {'enum': expected}
if len(expected) == 1:
result['const'] = expected[0]
types = {type(e) for e in expected}
if types == {str}: # pragma: no branch
result['type'] = 'string'
elif types == {int}: # pragma: no cover
result['type'] = 'integer'
elif types == {float}: # pragma: no cover
result['type'] = 'number'
elif types == {bool}: # pragma: no cover
result['type'] = 'boolean'
elif types == {list}: # pragma: no cover
result['type'] = 'array'
return result
# Patch the fastapi GenerateJsonSchema to use our custom one
fastapi.openapi.utils.GenerateJsonSchema = CustomGenerateJsonSchema # type: ignore
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==1.10.19\nWhen NOT to use: This fix should not be applied if backward compatibility with previous schema formats is required.\n\nOption C — Workaround\nfor others), I used this code prior to #8944 being merged, which monkeypatches the `GenerateJsonSchema` type used by fastapi:\nWhen NOT to use: This fix should not be applied if backward compatibility with previous schema formats is required.\n\n
Why This Fix Works in Production
- Trigger: Schema for `Literal` differs between version 2.6 and 2.7
- Mechanism: The JSON schema generation for literals and enums incorrectly included both 'const' and 'enum' keys
- Why the fix works: Changes the JSON schema generation for literals and enums to avoid including both 'const' and 'enum' in the same schema, addressing the issue reported in #9402. (first fixed release: 1.10.19).
- 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.11 in real deployments (not just unit tests).
- The JSON schema generation for literals and enums incorrectly included both 'const' and 'enum' keys
- Production symptom (often without a traceback): Schema for `Literal` differs between version 2.6 and 2.7
Proof / Evidence
- GitHub issue: #9402
- Fix PR: https://github.com/pydantic/pydantic/pull/10692
- First fixed release: 1.10.19
- 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).
“Here is what I propose: - Always generate an enum for enums, even if it contains a single member”
“thanks for the info”
“@lindapaiste can you point me at some reference indicating that including both enum and const in the same schema is invalid? Or at some tools…”
“> I think that indeed there is an issue. If you try to run the output through swagger, it will say it's invalid as: You…”
Failure Signature (Search String)
- Schema for `Literal` differs between version 2.6 and 2.7
- The schema for the fields that are marked `Literal` is different between versions 2.6 and 2.7.
Copy-friendly signature
Failure Signature
-----------------
Schema for `Literal` differs between version 2.6 and 2.7
The schema for the fields that are marked `Literal` is different between versions 2.6 and 2.7.
Error Message
Signature-only (no traceback captured)
Error Message
-------------
Schema for `Literal` differs between version 2.6 and 2.7
The schema for the fields that are marked `Literal` is different between versions 2.6 and 2.7.
Minimal Reproduction
class CustomGenerateJsonSchema(GenerateJsonSchema):
def literal_schema(self, schema: core_schema.LiteralSchema) -> JsonSchemaValue:
"""Generates a JSON schema that matches a literal value.
Args:
schema: The core schema.
Returns:
The generated JSON schema.
"""
expected = [v.value if isinstance(v, Enum) else v for v in schema['expected']]
# jsonify the expected values
expected = [to_jsonable_python(v) for v in expected]
result: dict[str, Any] = {'enum': expected}
if len(expected) == 1:
result['const'] = expected[0]
types = {type(e) for e in expected}
if types == {str}: # pragma: no branch
result['type'] = 'string'
elif types == {int}: # pragma: no cover
result['type'] = 'integer'
elif types == {float}: # pragma: no cover
result['type'] = 'number'
elif types == {bool}: # pragma: no cover
result['type'] = 'boolean'
elif types == {list}: # pragma: no cover
result['type'] = 'array'
return result
# Patch the fastapi GenerateJsonSchema to use our custom one
fastapi.openapi.utils.GenerateJsonSchema = CustomGenerateJsonSchema # type: ignore
Environment
- Python: 3.11
- Pydantic: 2
What Broke
Generated schemas caused validation errors in tools like Swagger and ReDoc.
Why It Broke
The JSON schema generation for literals and enums incorrectly included both 'const' and 'enum' keys
Fix Options (Details)
Option A — Upgrade to fixed release Safe default (recommended)
pip install pydantic==1.10.19
Use when you can deploy the upstream fix. It is usually lower-risk than long-lived workarounds.
Option C — Workaround Temporary workaround
for others), I used this code prior to #8944 being merged, which monkeypatches the `GenerateJsonSchema` type used by fastapi:
Use only if you cannot change versions today. Treat this as a stopgap and remove once upgraded.
Fix reference: https://github.com/pydantic/pydantic/pull/10692
First fixed release: 1.10.19
Last verified: 2026-02-09. Validate in your environment.
When NOT to Use This Fix
- This fix should not be applied if backward compatibility with previous schema formats is required.
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 |
|---|---|
| 1.10.19 | Fixed |
Related Issues
No related fixes found.
Sources
We don’t republish the full GitHub discussion text. Use the links above for context.