Jump to solution
Verify

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

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

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”
@Danipulok · 2024-08-27 · confirmation · source
“Yep - I've just pushed a PR that'll be included in v2.10 that does just that :)”
@sydney-runkle · 2024-09-17 · source
“Yep, marking this as a dupe of https://github.com/pydantic/pydantic/issues/9783. Thanks for your question @vgavro!”
@sydney-runkle · 2024-07-26 · source
“@Danipulok, Thanks for bringing this to our attention! I'll take a look at fixing this for our next beta release.”
@sydney-runkle · 2024-08-28 · source

Failure Signature (Search String)

  • Using $ref" in examples raises KeyError on .model_json_schema() generation

Error Message

Stack trace
error.txt
Error Message ------------- Using $ref" in examples raises KeyError on .model_json_schema() generation

Minimal Reproduction

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

When NOT to use: Avoid using $ref in examples if you need strict schema validation.

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.

Get updates

We publish verified fixes weekly. No spam.

Subscribe

When NOT to Use This Fix

  • Avoid using $ref in examples if you need strict schema validation.

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