How to Set a Minimum Value for a Number in pydantic with Field
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
from pydantic import BaseModel, Field
class <Model>(BaseModel):
<field>: int = Field(gt=<number>)
<field>: float = Field(ge=<number>, le=<number>)
| Part | What 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
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)

Step 2: Run It
uv run python order.py
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

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:
class Rating(BaseModel):
stars: int = Field(ge=1, le=5)
Rating(stars=5) passed, and Rating(stars=6) failed with:
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
| Mistake | What happens | Do this instead |
|---|---|---|
Using gt=1 for "at least one" | 1 itself is refused, because gt excludes the number | Use ge=1 |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
Input should be greater than 0 [type=greater_than ...] | The value is 0 or less, and the field uses gt=0 | Send a positive number |
2 validation errors for Order | Two fields broke their limits at once; pydantic lists each one | Fix 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
- Field —
gt,ge,lt,leand the other constraints. - Validation errors — what
greater_thanmeans.
