Jump to solution
Verify

The Fix

pip install pydantic==2.12.5

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

Jump to Verify Open PR/Commit
@@ -24,6 +24,12 @@ dependencies = [ ] +[[package]] +name = "allocator-api2" +version = "0.2.21"
repro.py
Example 1: from typing import Annotated from pydantic import BaseModel, Field, ValidationError ConstraintType = Annotated[str, Field(pattern="^(\\p{L}|_)(\\p{L}|\\p{N}|[.\\-_])*$")] class TestModel1(BaseModel): # 400 instances of the type help highlight the poor scaling behavior field_1: ConstraintType field_2: ConstraintType ... field_400: ConstraintType ================================================================== Example 2: from typing import Annotated from pydantic import BaseModel, Field, RootModel class RootType(RootModel[str]): root: Annotated[ str, Field( pattern="^(\\p{L}|_)(\\p{L}|\\p{N}|[.\\-_])*$" ), ] class TestModel2(BaseModel): field_1: RootType field_2: RootType ... field_400: RootType
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.12.5\nWhen NOT to use: This fix should not be used if regex patterns are not reused across models.\n\n

Why This Fix Works in Production

  • Trigger: I agree that it can be reproduced in 2.10.6. I looks like something else changed then that just caused us to notice the regex memory usage. I will try to…
  • Mechanism: The regex validator creation in Pydantic V2 leads to excessive memory usage due to lack of caching
  • Why the fix works: Caches compiled regex patterns in pydantic-core, significantly reducing memory usage for regex validators. (first fixed release: 2.12.5).
Production impact:
  • If left unfixed, this can cause silent data inconsistencies that propagate (bad cache entries, incorrect downstream decisions).

Why This Breaks in Prod

  • The regex validator creation in Pydantic V2 leads to excessive memory usage due to lack of caching
  • Production symptom (often without a traceback): from pydantic import BaseModel, Field, ValidationError

Proof / Evidence

Discussion

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

“I get roughly the same numbers as you, but can also reproduce on 2.10”
@Viicos · 2025-11-19 · source
“I agree that it can be reproduced in 2.10.6”
@dzaslavskiy · 2025-11-19 · source
“> I will try to narrow down to the root cause”
@Viicos · 2025-11-19 · source

Failure Signature (Search String)

  • I agree that it can be reproduced in 2.10.6. I looks like something else changed then that just caused us to notice the regex memory usage. I will try to narrow down to the root
Copy-friendly signature
signature.txt
Failure Signature ----------------- from pydantic import BaseModel, Field, ValidationError I agree that it can be reproduced in 2.10.6. I looks like something else changed then that just caused us to notice the regex memory usage. I will try to narrow down to the root cause. Here are the stats for the actual code:

Error Message

Signature-only (no traceback captured)
error.txt
Error Message ------------- from pydantic import BaseModel, Field, ValidationError I agree that it can be reproduced in 2.10.6. I looks like something else changed then that just caused us to notice the regex memory usage. I will try to narrow down to the root cause. Here are the stats for the actual code:

Minimal Reproduction

repro.py
Example 1: from typing import Annotated from pydantic import BaseModel, Field, ValidationError ConstraintType = Annotated[str, Field(pattern="^(\\p{L}|_)(\\p{L}|\\p{N}|[.\\-_])*$")] class TestModel1(BaseModel): # 400 instances of the type help highlight the poor scaling behavior field_1: ConstraintType field_2: ConstraintType ... field_400: ConstraintType ================================================================== Example 2: from typing import Annotated from pydantic import BaseModel, Field, RootModel class RootType(RootModel[str]): root: Annotated[ str, Field( pattern="^(\\p{L}|_)(\\p{L}|\\p{N}|[.\\-_])*$" ), ] class TestModel2(BaseModel): field_1: RootType field_2: RootType ... field_400: RootType

Environment

  • Pydantic: 2

What Broke

Increased memory usage during application startup causes slowdowns and potential crashes.

Why It Broke

The regex validator creation in Pydantic V2 leads to excessive memory usage due to lack of caching

Fix Options (Details)

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

pip install pydantic==2.12.5

When NOT to use: This fix should not be used if regex patterns are not reused across models.

Use when you can deploy the upstream fix. It is usually lower-risk than long-lived workarounds.

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.
  • This does NOT fix data corruption; it only prevents duplicate side-effects.
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/12549

First fixed release: 2.12.5

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

  • This fix should not be used if regex patterns are not reused across models.
  • 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.

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

Related Issues

No related fixes found.

Sources

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