The Fix
pip install pydantic==2.12.5
Based on closed pydantic/pydantic issue #12540 · PR/commit linked
@@ -24,6 +24,12 @@ dependencies = [
]
+[[package]]
+name = "allocator-api2"
+version = "0.2.21"
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
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.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).
- 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
- GitHub issue: #12540
- Fix PR: https://github.com/pydantic/pydantic/pull/12549
- First fixed release: 2.12.5
- Reproduced locally: No (not executed)
- Last verified: 2026-02-09
- Confidence: 0.95
- Did this fix it?: Yes (upstream fix exists)
- Own content ratio: 0.51
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”
“I agree that it can be reproduced in 2.10.6”
“> I will try to narrow down to the root cause”
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
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 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
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
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)
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)
Fix reference: https://github.com/pydantic/pydantic/pull/12549
First fixed release: 2.12.5
Last verified: 2026-02-09. Validate in your environment.
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
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.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.