Skip to main content

How to Set a Minimum Value for a Number in pydantic with Field

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A quantity can't be zero and a price can't be negative. pydantic checks limits like these when you declare the field with Field(ge=1) or Field(gt=0), and it names the field in its error when a value is out of range. This guide shows both with pydantic 2.14.4 on Python 3.15.2.

Syntax​

Syntax
from pydantic import BaseModel, Field


class <Model>(BaseModel):
<field>: int = Field(gt=<number>)
<field>: float = Field(ge=<number>, le=<number>)
PartWhat it means
gt=<number>Greater than: the value must be bigger than the number
ge=<number>Greater than or equal: the number itself is allowed
lt=<number>, le=<number>The same two checks for a maximum: less than, and less than or equal

pydantic's Field reference defines them this way: "Greater than. If set, value must be greater than this. Only applicable to numbers." and "Greater than or equal. If set, value must be greater than or equal to this."

The Example​

An Order has a quantity of at least one, and a price above zero. The script creates one good order and one with zeros in both fields.

Prerequisites​

  • pydantic in your project, for example with uv add pydantic. This guide uses pydantic 2.14.4 on Python 3.15.2.

Step 1: Write the Script​

order.py
from pydantic import BaseModel, Field, ValidationError


class Order(BaseModel):
quantity: int = Field(ge=1)
price: float = Field(gt=0)


print(Order(quantity=1, price=9.5))

try:
Order(quantity=0, price=0)
except ValidationError as error:
print(error)

The order.py script in a macOS-style code window: an Order model with quantity: int = Field(ge=1) and price: float = Field(gt=0), one valid order and one with zeros inside try and except ValidationError.

Step 2: Run It​

Terminal
uv run python order.py
Output
quantity=1 price=9.5
2 validation errors for Order
quantity
Input should be greater than or equal to 1 [type=greater_than_equal, input_value=0, input_type=int]
For further information visit https://errors.pydantic.dev/2.14/v/greater_than_equal
price
Input should be greater than 0 [type=greater_than, input_value=0, input_type=int]
For further information visit https://errors.pydantic.dev/2.14/v/greater_than

The terminal after uv run python order.py: quantity=1 price=9.5, then 2 validation errors for Order, one for quantity and one for price.

A quantity of 1 passed, because ge=1 allows 1 itself. The zeros failed, and pydantic reported both fields at once, each with the limit it broke. The command works unchanged in PowerShell.

A Minimum and a Maximum Together​

Both limits can go on one field. A star rating from one to five:

Rating
class Rating(BaseModel):
stars: int = Field(ge=1, le=5)

Rating(stars=5) passed, and Rating(stars=6) failed with:

Output
1 validation error for Rating
stars
Input should be less than or equal to 5 [type=less_than_equal, input_value=6, input_type=int]
For further information visit https://errors.pydantic.dev/2.14/v/less_than_equal

Common Mistakes​

MistakeWhat happensDo this instead
Using gt=1 for "at least one"1 itself is refused, because gt excludes the numberUse ge=1

Troubleshooting​

SymptomLikely causeFix
Input should be greater than 0 [type=greater_than ...]The value is 0 or less, and the field uses gt=0Send a positive number
2 validation errors for OrderTwo fields broke their limits at once; pydantic lists each oneFix every field the error names, not only the first

FAQ​

What's the difference between gt and ge?​

Whether the limit itself is allowed. ge=1 accepted 1; gt=0 refused 0.

Does the limit work on whole numbers and decimals alike?​

Yes. Here quantity is an int and price is a float, and both checks worked.

Conclusion​

Field(ge=1) and Field(gt=0) made Order refuse a zero quantity and a zero price, and pydantic named both fields in one ValidationError. Add le or lt on the same field for a maximum.

Additional Resources​