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%.
@@ -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]
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()
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.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).
- 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
- GitHub issue: #9783
- Fix PR: https://github.com/pydantic/pydantic/pull/9863
- First fixed release: 1.10.18
- 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.64
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…”
“@reversefold, Thanks for reporting this. Looks like this is causing an issue in the defs / refs simplification process. @adriangb, Any ideas on how we…”
Failure Signature (Search String)
- Referencing an external JSON Schema when generating a JSONSchema fails with a KeyError
Error Message
Stack trace
Error Message
-------------
Referencing an external JSON Schema when generating a JSONSchema fails with a KeyError
Minimal Reproduction
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
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.
When NOT to Use This Fix
- Do not use this fix if your application relies on internal definitions for JSON 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 |
|---|---|
| 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.