pydantic/pydantic · error · ValueError
On field "{field_name}" the following field constraints are
Error message
On field "{field_name}" the following field constraints are set but not enforced: {", ".join(unused_constraints)}.
For more details see https://docs.pydantic.dev/usage/schema/#unenforced-field-constraints What it means
Raised as `ValueError` by `get_annotation_from_field_info` when constraints declared on a `Field(...)` are not actually applicable to the field's type. pydantic computes `constraints - used_constraints`; if anything is left over (e.g. `gt` on an `int` is fine, but `max_length` on an `int`, or `regex` on a number), it refuses to silently ignore them.
Source
Thrown at pydantic/v1/schema.py:1021
) -> Type[Any]:
"""
Get an annotation with validation implemented for numbers and strings based on the field_info.
:param annotation: an annotation from a field specification, as ``str``, ``ConstrainedStr``
:param field_info: an instance of FieldInfo, possibly with declarations for validations and JSON Schema
:param field_name: name of the field for use in error messages
:param validate_assignment: default False, flag for BaseModel Config value of validate_assignment
:return: the same ``annotation`` if unmodified or a new annotation with validation in place
"""
constraints = field_info.get_constraints()
used_constraints: Set[str] = set()
if constraints:
annotation, used_constraints = get_annotation_with_constraints(annotation, field_info)
if validate_assignment:
used_constraints.add('allow_mutation')
unused_constraints = constraints - used_constraints
if unused_constraints:
raise ValueError(
f'On field "{field_name}" the following field constraints are set but not enforced: '
f'{", ".join(unused_constraints)}. '
f'\nFor more details see https://docs.pydantic.dev/usage/schema/#unenforced-field-constraints'
)
return annotation
def get_annotation_with_constraints(annotation: Any, field_info: FieldInfo) -> Tuple[Type[Any], Set[str]]: # noqa: C901
"""
Get an annotation with used constraints implemented for numbers and strings based on the field_info.
:param annotation: an annotation from a field specification, as ``str``, ``ConstrainedStr``
:param field_info: an instance of FieldInfo, possibly with declarations for validations and JSON Schema
:return: the same ``annotation`` if unmodified or a new annotation along with the used constraints.
"""
used_constraints: Set[str] = set()
View on GitHub (pinned to 2e5f0e2b42)
Solutions
- Remove or correct the constraint that does not apply to the annotated type (see the unused list in the message).
- Move the validation into a @validator if you genuinely need it on that type.
- Re-read the message: it names exactly which constraints are unenforced.
Example fix
// before
class M(BaseModel):
age: int = Field(min_length=1) # min_length is for strings
// after
class M(BaseModel):
age: int = Field(ge=1) Defensive patterns
Strategy: validation
Validate before calling
# map of which constraints apply to which base type
_STR = {'min_length','max_length','regex'}
_NUM = {'gt','ge','lt','le','multiple_of'}
def constraints_match(type_, used) -> bool:
import numbers
return used <= (_STR if isinstance(type_, type) and issubclass(type_, str)
else _NUM if isinstance(type_, type) and issubclass(type_, numbers.Number) else set()) Prevention
- Match Field constraints to the field's primitive type (length->str/bytes, bounds->number).
- Treat leftover constraints as a bug, not a no-op.
When it happens
Trigger: Declaring a Field with constraints that do not match the annotated type — e.g. `x: int = Field(min_length=4)`, `x: float = Field(regex='...')`, or `x: str = Field(gt=0)` (gt is numeric-only and unused on str). Also when applying a constraint that only exists for schema but not validation on an unsupported type.
Common situations: Copying a Field definition from another field of a different type, using a validator-style constraint on the wrong type, or assuming constraints are silently ignored if inapplicable.
Related errors
- Unable to apply constraint '{constraint}' to supplied value
- create-model-field-definitions
- invalid-annotated-type
- validate-by-alias-and-name-false
- The core schema type {choice["type"]!r} is not a valid discr
AI-assisted analysis of pydantic/pydantic@2e5f0e2b42 (2026-08-04).
Data as JSON: /data/errors/31e357158b0792d2.json.
Report an issue: GitHub.