The Fix
pip install pydantic==1.10.18
Based on closed pydantic/pydantic issue #10151 · 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%.
@@ -5,7 +5,7 @@
from typing import TYPE_CHECKING, Any, Callable, Iterable
-from pydantic_core import CoreSchema, PydanticCustomError, to_jsonable_python
+from pydantic_core import CoreSchema, PydanticCustomError, ValidationError, to_jsonable_python
from pydantic_core import core_schema as cs
from pydantic import BaseModel, Field
class A(BaseModel):
data: int = Field(pattern="1")
A(data=1)
# or
A(data="1")
"""
pydantic_core._pydantic_core.ValidationError: 1 validation error for A
data
Input should be a valid string [type=string_type, input_value=1, input_type=int]
For further information visit https://errors.pydantic.dev/2.8/v/string_type
"""
class B(BaseModel):
data: list[int] = Field(pattern="1") # TypeError: The following constraints cannot be applied to list[int]: 'pattern'
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==1.10.18\nWhen NOT to use: Do not apply string constraints like pattern to non-string fields.\n\n
Why This Fix Works in Production
- Trigger: from pydantic import BaseModel, Field
- Mechanism: The pattern constraint was incorrectly applied to non-string types, leading to misleading validation errors
- Why the fix works: Improves runtime errors for string constraints like `pattern` when applied to incompatible types, addressing issue #10151. (first fixed release: 1.10.18).
- 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
- The pattern constraint was incorrectly applied to non-string types, leading to misleading validation errors
- Surfaces as: from pydantic import BaseModel, Field
Proof / Evidence
- GitHub issue: #10151
- Fix PR: https://github.com/pydantic/pydantic/pull/10158
- First fixed release: 1.10.18
- Reproduced locally: No (not executed)
- Last verified: 2026-02-09
- Confidence: 0.85
- Did this fix it?: Yes (upstream fix exists)
- Own content ratio: 0.30
Discussion
High-signal excerpts from the issue thread (symptoms, repros, edge-cases).
“Thanks, I'm not familiar with the subclass use case, but in my use case I was very confused when I accidentally applied a pattern to…”
“Perhaps there could be a compromise? allow the class definition but during construction describe the situation that the constraint doesn't match the type”
“We tried that, and that is the case for some of the more lax validators, but not for the ones applied as chain schemas. For…”
“I guess we could try to customize the errors for the chain schema validators, but I'm not sure of an easy way to do that...”
Failure Signature (Search String)
- from pydantic import BaseModel, Field
Error Message
Stack trace
Error Message
-------------
from pydantic import BaseModel, Field
class A(BaseModel):
data: int = Field(pattern="1")
A(data=1)
# or
A(data="1")
"""
pydantic_core._pydantic_core.ValidationError: 1 validation error for A
data
Input should be a valid string [type=string_type, input_value=1, input_type=int]
For further information visit https://errors.pydantic.dev/2.8/v/string_type
"""
class B(BaseModel):
data: list[int] = Field(pattern="1") # TypeError: The following constraints cannot be applied to list[int]: 'pattern'
Stack trace
Error Message
-------------
from pydantic import BaseModel, Field
class A(BaseModel):
data: list[int] = Field(gt=0)
a = A(data=[1, 2, 3])
"""
Traceback (most recent call last):
File "/Users/programming/pydantic_work/pydantic/pydantic/_internal/_validators.py", line 265, in validator
if not predicate(x, constraint_value):
File "/Users/programming/pydantic_work/pydantic/pydantic/_internal/_validators.py", line 277, in <lambda>
'gt': create_constraint_validator('gt', lambda x, gt: x > gt, 'greater_than'),
TypeError: '>' not supported between instances of 'list' and 'int'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/programming/pydantic_work/pydantic/test.py", line 6, in <module>
a = A(data=[1, 2, 3])
File "/Users/programming/pydantic_work/pydantic/pydantic/main.py", line 195, in __init__
self.__pydantic_validator__.validate_python(data, self_instance=self)
File "/Users/programming/pydantic_work/pydantic/pydantic/_internal/_validators.py", line 270, in validator
raise TypeError(f"Unable to apply constraint '{constraint_id}' to supplied value {x}")
TypeError: Unable to apply constraint 'gt' to supplied value [1, 2, 3]
"""
Minimal Reproduction
from pydantic import BaseModel, Field
class A(BaseModel):
data: int = Field(pattern="1")
A(data=1)
# or
A(data="1")
"""
pydantic_core._pydantic_core.ValidationError: 1 validation error for A
data
Input should be a valid string [type=string_type, input_value=1, input_type=int]
For further information visit https://errors.pydantic.dev/2.8/v/string_type
"""
class B(BaseModel):
data: list[int] = Field(pattern="1") # TypeError: The following constraints cannot be applied to list[int]: 'pattern'
Environment
- Pydantic: 2
What Broke
Users experienced cryptic validation errors when applying string constraints to incompatible types.
Why It Broke
The pattern constraint was incorrectly applied to non-string types, leading to misleading validation errors
Fix Options (Details)
Option A — Upgrade to fixed release Safe default (recommended)
pip install pydantic==1.10.18
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/10158
First fixed release: 1.10.18
Last verified: 2026-02-09. Validate in your environment.
When NOT to Use This Fix
- Do not apply string constraints like pattern to non-string fields.
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 |
|---|---|
| 1.10.18 | Fixed |
Related Issues
No related fixes found.
Sources
We don’t republish the full GitHub discussion text. Use the links above for context.