The Fix
pip install pydantic==2.12.0
Based on closed pydantic/pydantic issue #12249 · 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%.
@@ -2,28 +2,31 @@
---
-Pydantic supports many common types from the Python standard library. If you need stricter processing see
-[Strict Types](../concepts/types.md#strict-types), including if you need to constrain the values allowed (e.g. to require a positive `int`).
+This section enumerates the supported built-in and standard library types: the allowed values,
import json
from pydantic import BaseModel, Field
from typing import Annotated
import re
compiled_pattern = re.compile(r'\Aa{,3}\Z')
class FooBar(BaseModel):
# regular expressions that will be rejected (as they are python only)
# when they are parsed as string
#matching_name : str = Field(pattern=r'^a{,3}$')
#matching_name2 : str = Field(pattern=r'\Anon\Z')
# but accepted when specified compiled, two variants
#matching_name3 : Annotated[str, Field(pattern=compiled_pattern)]
matching_name3 : str = Field(pattern=compiled_pattern)
print(json.dumps(FooBar.model_json_schema(), indent=2))
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.0\nWhen NOT to use: Avoid using compiled regex patterns for JSON schema generation without validation.\n\n
Why This Fix Works in Production
- Trigger: regex patterns parsed differently when compiled (e.g. as StringContraints `pattern`) leading to wrong jsonschema pattern
- Mechanism: Different regex engines in Python and Pydantic lead to inconsistent pattern validation
- Why the fix works: Improved documentation regarding regex patterns in Pydantic to clarify behavior differences between Python and JavaScript regex engines. (first fixed release: 2.12.0).
- 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
- Different regex engines in Python and Pydantic lead to inconsistent pattern validation
- Production symptom (often without a traceback): regex patterns parsed differently when compiled (e.g. as StringContraints `pattern`) leading to wrong jsonschema pattern
Proof / Evidence
- GitHub issue: #12249
- Fix PR: https://github.com/pydantic/pydantic/pull/12287
- First fixed release: 2.12.0
- 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.48
Verified Execution
We executed the runnable minimal repro in a temporary environment and captured exit codes + logs.
- Status: PASS
- Ran: 2026-02-11T16:52:29Z
- Package: pydantic
- Fixed: 2.12.0
- Mode: fixed_only
- Outcome: ok
Logs
{
"properties": {
"matching_name3": {
"pattern": "\\Aa{,3}\\Z",
"title": "Matching Name3",
"type": "string"
}
},
"required": [
"matching_name3"
],
"title": "FooBar",
"type": "object"
}
Discussion
High-signal excerpts from the issue thread (symptoms, repros, edge-cases).
“@Viicos I can work on this if it's free.”
“I'm not sure we'll be able to do much here”
“> The regex engine used can be configured using the regex_engine configuration value”
Failure Signature (Search String)
- regex patterns parsed differently when compiled (e.g. as StringContraints `pattern`) leading to wrong jsonschema pattern
- jsonschema interprets restricting `"pattern"` as Javascript regular expression (Reference: https://json-schema.org/draft/2020-12/json-schema-core#name-regular-expressions)
Copy-friendly signature
Failure Signature
-----------------
regex patterns parsed differently when compiled (e.g. as StringContraints `pattern`) leading to wrong jsonschema pattern
jsonschema interprets restricting `"pattern"` as Javascript regular expression (Reference: https://json-schema.org/draft/2020-12/json-schema-core#name-regular-expressions)
Error Message
Signature-only (no traceback captured)
Error Message
-------------
regex patterns parsed differently when compiled (e.g. as StringContraints `pattern`) leading to wrong jsonschema pattern
jsonschema interprets restricting `"pattern"` as Javascript regular expression (Reference: https://json-schema.org/draft/2020-12/json-schema-core#name-regular-expressions)
Minimal Reproduction
import json
from pydantic import BaseModel, Field
from typing import Annotated
import re
compiled_pattern = re.compile(r'\Aa{,3}\Z')
class FooBar(BaseModel):
# regular expressions that will be rejected (as they are python only)
# when they are parsed as string
#matching_name : str = Field(pattern=r'^a{,3}$')
#matching_name2 : str = Field(pattern=r'\Anon\Z')
# but accepted when specified compiled, two variants
#matching_name3 : Annotated[str, Field(pattern=compiled_pattern)]
matching_name3 : str = Field(pattern=compiled_pattern)
print(json.dumps(FooBar.model_json_schema(), indent=2))
Environment
- Pydantic: 2
What Broke
Users experience invalid JSON schema generation due to regex pattern mismatches.
Why It Broke
Different regex engines in Python and Pydantic lead to inconsistent pattern validation
Fix Options (Details)
Option A — Upgrade to fixed release Safe default (recommended)
pip install pydantic==2.12.0
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/12287
First fixed release: 2.12.0
Last verified: 2026-02-09. Validate in your environment.
When NOT to Use This Fix
- Avoid using compiled regex patterns for JSON schema generation without 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.12.0 | Fixed |
Related Issues
No related fixes found.
Sources
We don’t republish the full GitHub discussion text. Use the links above for context.