Jump to solution
Verify

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.

Jump to Verify Open PR/Commit
@@ -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:
repro.py
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
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==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).
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.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

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”
@Viicos · 2024-10-18 · source
“thanks for the info”
@vigneshmanick · 2024-05-07 · source
“@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…”
@dmontagu · 2024-05-30 · confirmation · source
“> 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…”
@Viicos · 2024-10-18 · source

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
signature.txt
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.txt
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

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

When NOT to use: This fix should not be applied if backward compatibility with previous schema formats is required.

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:

When NOT to use: This fix should not be applied if backward compatibility with previous schema formats is required.

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.

Get updates

We publish verified fixes weekly. No spam.

Subscribe

When NOT to Use This Fix

  • This fix should not be applied if backward compatibility with previous schema formats is required.

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