Jump to solution
Details

The Fix

Improves the import times for modules in Pydantic by using caching, specifically for the `BaseModel` import.

Based on closed pydantic/pydantic issue #10055 · PR/commit linked

Production note: Watch p95/p99 latency and retry volume; timeouts can turn into retry storms and duplicate side-effects.

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]
fix.md
Option A — Apply the official fix\nImproves the import times for modules in Pydantic by using caching, specifically for the `BaseModel` import.\nWhen NOT to use: This fix should not be used if the application does not require external JSON Schema references.\n\n

Why This Fix Works in Production

  • Trigger: Thanks to @changhc for the sustained work on complex number support!
  • Mechanism: Pydantic throws a KeyError when WithJsonSchema is used with a $ref that defines an external JSON Schema via an https link
Production impact:
  • If left unfixed, tail latency can spike under load and surface as timeouts/retries (amplifying incident impact).

Why This Breaks in Prod

  • Pydantic throws a KeyError when WithJsonSchema is used with a $ref that defines an external JSON Schema via an https link
  • Production symptom (often without a traceback): Thanks to @changhc for the sustained work on complex number support!

Proof / Evidence

Discussion

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

“Hi Syndey, thank you for posting weekly reports”
@changhc · 2024-08-06 · source
“@changhc, Sure thing! In the meantime, you can filter by help wanted or good first issue. That being said, I'll go through the issues this…”
@sydney-runkle · 2024-08-06 · source
“Managed to make some forward progress on the union fixes with the following PRs: * https://github.com/pydantic/pydantic-core/pull/1398 * https://github.com/pydantic/pydantic-core/pull/1397 Would still like to implement some sort…”
@sydney-runkle · 2024-08-09 · source

Failure Signature (Search String)

  • Thanks to @changhc for the sustained work on complex number support!
  • Nothing at the moment, but I'm sure this will change. Ping me if you're looking for a review!
Copy-friendly signature
signature.txt
Failure Signature ----------------- Thanks to @changhc for the sustained work on complex number support! Nothing at the moment, but I'm sure this will change. Ping me if you're looking for a review!

Error Message

Signature-only (no traceback captured)
error.txt
Error Message ------------- Thanks to @changhc for the sustained work on complex number support! Nothing at the moment, but I'm sure this will change. Ping me if you're looking for a review!

What Broke

Users experience KeyErrors when using external JSON Schema references, causing disruptions in functionality.

Why It Broke

Pydantic throws a KeyError when WithJsonSchema is used with a $ref that defines an external JSON Schema via an https link

Fix Options (Details)

Option A — Apply the official fix

Improves the import times for modules in Pydantic by using caching, specifically for the `BaseModel` import.

When NOT to use: This fix should not be used if the application does not require external JSON Schema references.

Option D — Guard side-effects with OnceOnly Guardrail for side-effects

Mitigate duplicate external side-effects under retries/timeouts/agent loops by gating the operation before calling external systems.

  • Place OnceOnly between your code/agent and real side-effects (Stripe, emails, CRM, APIs).
  • Use a stable key per side-effect (e.g., customer_id + action + idempotency_key).
  • Fail-safe: configure fail-open vs fail-closed based on blast radius and spend risk.
Show example snippet (optional)
onceonly.py
from onceonly import OnceOnly import os once = OnceOnly(api_key=os.environ["ONCEONLY_API_KEY"], fail_open=True) # Stable idempotency key per real side-effect. # Use a request id / job id / webhook delivery id / Stripe event id, etc. event_id = "evt_..." # replace key = f"stripe:webhook:{event_id}" res = once.check_lock(key=key, ttl=3600) if res.duplicate: return {"status": "already_processed"} # Safe to execute the side-effect exactly once. handle_event(event_id)

See OnceOnly SDK

When NOT to use: Do not use this to hide logic bugs or data corruption. Use it to block duplicate external side-effects and enforce tool permissions/spend caps.

Fix reference: https://github.com/pydantic/pydantic/pull/9863

Last verified: 2026-02-12. 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 used if the application does not require external JSON Schema references.
  • Do not use this to hide logic bugs or data corruption. Use it to block duplicate external side-effects and enforce tool permissions/spend caps.

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.

Related Issues

No related fixes found.

Sources

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