Jump to solution
Verify

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%.

Jump to Verify Open PR/Commit
@@ -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
repro.py
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'
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==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).
Production impact:
  • 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

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…”
@KotlinIsland · 2024-08-16 · source
“Perhaps there could be a compromise? allow the class definition but during construction describe the situation that the constraint doesn't match the type”
@KotlinIsland · 2024-08-16 · source
“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…”
@sydney-runkle · 2024-08-16 · source
“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...”
@sydney-runkle · 2024-08-16 · source

Failure Signature (Search String)

  • from pydantic import BaseModel, Field

Error Message

Stack trace
error.txt
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.txt
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

repro.py
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

When NOT to use: Do not apply string constraints like pattern to non-string fields.

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.

Get updates

We publish verified fixes weekly. No spam.

Subscribe

When NOT to Use This Fix

  • Do not apply string constraints like pattern to non-string fields.

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