The Fix
pip install pydantic==2.9.2
Based on closed pydantic/pydantic issue #9981 · 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%.
@@ -2172,7 +2172,9 @@ def _add_json_refs(schema: Any) -> None:
raise
- for v in schema.values():
+ for k, v in schema.items():
+ if k == 'examples':
from pydantic import BaseModel, Field
class Test(BaseModel):
x: dict = Field(example={"$ref": "#/components/schemas/Pet"})
Test.model_json_schema()
# raises KeyError: '#/components/schemas/Pet'
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.9.2\nWhen NOT to use: Avoid using $ref in examples if you need strict schema validation.\n\n
Why This Fix Works in Production
- Trigger: Using $ref" in examples raises KeyError on .model_json_schema() generation
- Mechanism: Using $ref in Field example causes KeyError during model_json_schema generation
- Why the fix works: Allows arbitrary references in JSON schema examples, addressing the KeyError issue when using $ref in examples. (first fixed release: 2.9.2).
- 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).
- Using $ref in Field example causes KeyError during model_json_schema generation
- Surfaces as: Using $ref" in examples raises KeyError on .model_json_schema() generation
Proof / Evidence
- GitHub issue: #9981
- Fix PR: https://github.com/pydantic/pydantic/pull/10417
- First fixed release: 2.9.2
- 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.68
Discussion
High-signal excerpts from the issue thread (symptoms, repros, edge-cases).
“Hey here! @sydney-runkle, I just tested a similar code at the beta pydantic version”
“Yep - I've just pushed a PR that'll be included in v2.10 that does just that :)”
“Yep, marking this as a dupe of https://github.com/pydantic/pydantic/issues/9783. Thanks for your question @vgavro!”
“@Danipulok, Thanks for bringing this to our attention! I'll take a look at fixing this for our next beta release.”
Failure Signature (Search String)
- Using $ref" in examples raises KeyError on .model_json_schema() generation
Error Message
Stack trace
Error Message
-------------
Using $ref" in examples raises KeyError on .model_json_schema() generation
Minimal Reproduction
from pydantic import BaseModel, Field
class Test(BaseModel):
x: dict = Field(example={"$ref": "#/components/schemas/Pet"})
Test.model_json_schema()
# raises KeyError: '#/components/schemas/Pet'
Environment
- Python: 3.11
- Pydantic: 2
What Broke
KeyError occurs when generating JSON schema, preventing schema creation.
Why It Broke
Using $ref in Field example causes KeyError during model_json_schema generation
Fix Options (Details)
Option A — Upgrade to fixed release Safe default (recommended)
pip install pydantic==2.9.2
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/10417
First fixed release: 2.9.2
Last verified: 2026-02-09. Validate in your environment.
When NOT to Use This Fix
- Avoid using $ref in examples if you need strict schema validation.
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.9.2 | Fixed |
Related Issues
No related fixes found.
Sources
We don’t republish the full GitHub discussion text. Use the links above for context.