Jump to solution
Verify

The Fix

pip install pydantic==1.10.18

Based on closed pydantic/pydantic issue #9783 · 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
@@ -2050,10 +2050,15 @@ def handle_ref_overrides(self, json_schema: JsonSchemaValue) -> JsonSchemaValue: def get_schema_from_definitions(self, json_ref: JsonRef) -> JsonSchemaValue | None: - def_ref = self.json_to_defs_refs[json_ref] - if def_ref in self._core_defs_invalid_for_json_schema: - raise self._core_defs_invalid_for_json_schema[def_ref]
repro.py
import typing import pydantic class Model(pydantic.BaseModel): config_jsonschema: typing.Annotated[ dict, pydantic.WithJsonSchema( {"$ref": "https://json-schema.org/draft/2020-12/schema"} ), ] 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==1.10.18\nWhen NOT to use: Do not use this fix if your application relies on internal definitions for JSON schemas.\n\n

Why This Fix Works in Production

  • Trigger: Referencing an external JSON Schema when generating a JSONSchema fails with a KeyError
  • Mechanism: A KeyError occurs when using WithJsonSchema with an external JSON Schema reference due to missing definitions
  • Why the fix works: Fixes a KeyError when using WithJsonSchema with an external JSON Schema reference. (first fixed release: 1.10.18).
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).
  • A KeyError occurs when using WithJsonSchema with an external JSON Schema reference due to missing definitions
  • Surfaces as: Referencing an external JSON Schema when generating a JSONSchema fails with a KeyError

Proof / Evidence

Discussion

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

“I don't know that this has to do with defs/refs simplifications. That's in the core schema, this is json schema. I'm guessing we can just…”
@adriangb · 2024-06-28 · source
“@reversefold, Thanks for reporting this. Looks like this is causing an issue in the defs / refs simplification process. @adriangb, Any ideas on how we…”
@sydney-runkle · 2024-06-28 · source

Failure Signature (Search String)

  • Referencing an external JSON Schema when generating a JSONSchema fails with a KeyError

Error Message

Stack trace
error.txt
Error Message ------------- Referencing an external JSON Schema when generating a JSONSchema fails with a KeyError

Minimal Reproduction

repro.py
import typing import pydantic class Model(pydantic.BaseModel): config_jsonschema: typing.Annotated[ dict, pydantic.WithJsonSchema( {"$ref": "https://json-schema.org/draft/2020-12/schema"} ), ] Model.model_json_schema()

Environment

  • Python: 3.11
  • Pydantic: 2

What Broke

Users experience crashes when attempting to generate JSON schema for models with external references.

Why It Broke

A KeyError occurs when using WithJsonSchema with an external JSON Schema reference due to missing definitions

Fix Options (Details)

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

pip install pydantic==1.10.18

When NOT to use: Do not use this fix if your application relies on internal definitions for JSON 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/9863

First fixed release: 1.10.18

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 application relies on internal definitions for JSON 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
1.10.18 Fixed

Related Issues

No related fixes found.

Sources

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