Bird
Raised Fist0
FastAPIframework~10 mins

Numeric validation (gt, lt, ge, le) in FastAPI - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Numeric validation (gt, lt, ge, le)
Receive input data
↓
Check numeric constraints
↓
Validation passes?
No→Return error message
Yes↓
Process valid data
↓
Return success
Input data is checked against numeric rules (greater than, less than, etc.). If it passes, processing continues; if not, an error is returned.
Execution Sample
FastAPI
from fastapi import FastAPI
from pydantic import BaseModel, Field

class Item(BaseModel):
    price: float = Field(..., gt=0, le=100)

app = FastAPI()
Defines a FastAPI model where 'price' must be greater than 0 and less or equal to 100.
Execution Table
StepInput priceValidation CheckResultAction
15050 > 0 and 50 <= 100TrueAccept input, proceed
200 > 0 and 0 <= 100FalseReject input, return error
3101101 > 0 and 101 <= 100FalseReject input, return error
4100100 > 0 and 100 <= 100TrueAccept input, proceed
5-5-5 > 0 and -5 <= 100FalseReject input, return error
💡 Validation stops when input fails numeric constraints or all checks pass.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5
priceundefined500101100-5
validation_resultundefinedTrueFalseFalseTrueFalse
Key Moments - 3 Insights
Why does input 0 fail validation when the rule is gt=0?
Because 'gt=0' means the value must be strictly greater than 0, not equal. Row 2 in execution_table shows 0 > 0 is False.
What happens if input is exactly 100 with le=100?
It passes because 'le=100' means less than or equal to 100. Row 4 confirms 100 <= 100 is True.
Why is negative input rejected even if le=100?
Because it must also satisfy gt=0. Negative numbers fail the greater than 0 check, as shown in row 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the validation_result at step 3 when price is 101?
AFalse
BTrue
CError
DUndefined
💡 Hint
Check the 'validation_result' column for step 3 in execution_table.
At which step does the input price exactly meet the upper limit of the validation?
AStep 1
BStep 2
CStep 4
DStep 5
💡 Hint
Look for price equal to 100 in execution_table.
If the 'gt' rule changed from 0 to -10, which step's validation_result would change from False to True?
AStep 2
BStep 5
CStep 3
DStep 1
💡 Hint
Check which input is negative and currently fails validation in execution_table.
Concept Snapshot
FastAPI numeric validation uses Field with gt, lt, ge, le.
'gt' means greater than, 'lt' less than.
'ge' means greater or equal, 'le' less or equal.
Input failing these returns validation error.
Use to ensure numbers fit expected ranges.
Full Transcript
This visual execution shows how FastAPI validates numeric inputs using gt, lt, ge, and le rules. The input value is checked step-by-step against these constraints. If the value passes all checks, it is accepted; otherwise, an error is returned. For example, a price must be greater than 0 and less or equal to 100. Inputs like 50 and 100 pass, while 0, 101, and -5 fail. This helps ensure data is within expected numeric ranges before processing.

Practice

(1/5)
1. In FastAPI, which parameter would you use in a Pydantic Field to ensure a number is strictly greater than 10?
easy
A. gt=10
B. lt=10
C. ge=10
D. le=10

Solution

  1. Step 1: Understand the meaning of gt

    The gt parameter means "greater than" and excludes the value itself.
  2. Step 2: Match the requirement

    Since the question asks for strictly greater than 10, gt=10 is the correct choice.
  3. Final Answer:

    gt=10 -> Option A
  4. Quick Check:

    Strictly greater than 10 = gt=10 [OK]
Hint: Use gt for strictly greater than, ge for greater or equal [OK]
Common Mistakes:
  • Confusing gt with ge and using ge=10 instead
  • Using lt or le which mean less than
  • Thinking gt includes the number 10
2. Which of the following is the correct syntax to validate a number field in FastAPI to be less than or equal to 100 using Pydantic?
easy
A. Field(ge=100)
B. Field(lt=100)
C. Field(le=100)
D. Field(gt=100)

Solution

  1. Step 1: Identify the parameter for less than or equal

    The parameter le stands for "less than or equal to".
  2. Step 2: Check the syntax

    The correct syntax is Field(le=100) to enforce the value to be at most 100.
  3. Final Answer:

    Field(le=100) -> Option C
  4. Quick Check:

    Less than or equal to 100 = le=100 [OK]
Hint: Use le for less or equal, lt for strictly less [OK]
Common Mistakes:
  • Using lt=100 which excludes 100
  • Using ge or gt which are for greater values
  • Writing Field(le100) without equals sign
3. Given this FastAPI model snippet:
from pydantic import BaseModel, Field

class Item(BaseModel):
    price: float = Field(gt=0, le=100)

What happens if you send a request with price=0?
medium
A. Accepted because price is a float
B. Accepted because price is less than or equal to 100
C. Validation error because price must be less than or equal to 0
D. Validation error because price must be greater than 0

Solution

  1. Step 1: Analyze the validation rules

    The field has gt=0 (greater than 0) and le=100 (less or equal to 100).
  2. Step 2: Check the input value

    Input price is 0, which is not greater than 0, so it violates the gt=0 rule.
  3. Final Answer:

    Validation error because price must be greater than 0 -> Option D
  4. Quick Check:

    Price 0 fails gt=0 validation [OK]
Hint: gt excludes the number itself; 0 is not > 0 [OK]
Common Mistakes:
  • Thinking 0 passes because it is less than 100
  • Confusing gt with ge and allowing 0
  • Ignoring validation errors in FastAPI
4. You wrote this FastAPI model:
from pydantic import BaseModel, Field

class User(BaseModel):
    age: int = Field(ge=18, lt=65)

But when you send age=65, it passes validation. Why?
medium
A. Because lt=65 means less than 65, so 65 should fail but it passes because you used le=65 instead of lt=65
B. Because lt=65 means less than 65, so 65 should fail but it passes because you used ge incorrectly
C. Because lt=65 means less than 65, so 65 should fail but it passes because the validation is not enforced on int
D. Because lt=65 means age must be less than 65, so 65 should fail but it passes due to a bug

Solution

  1. Step 1: Understand the meaning of lt and le

    lt=65 means strictly less than 65, so 65 should fail validation.
  2. Step 2: Identify the cause of passing validation

    If 65 passes, likely the code uses le=65 (less or equal) instead of lt=65, allowing 65.
  3. Final Answer:

    Because lt=65 means less than 65, so 65 should fail but it passes because you used le=65 instead of lt=65 -> Option A
  4. Quick Check:

    lt excludes value; le includes it [OK]
Hint: lt excludes value; le includes it. Check which you used [OK]
Common Mistakes:
  • Confusing lt and le parameters
  • Assuming validation is not enforced on integers
  • Thinking ge affects upper bound
5. You want to create a FastAPI model for a product rating that must be between 1 and 5 inclusive. Which is the correct way to define this field using Pydantic validation?
hard
A. rating: int = Field(gt=1, lt=5)
B. rating: int = Field(ge=1, le=5)
C. rating: int = Field(gt=1, le=5)
D. rating: int = Field(ge=1, lt=5)

Solution

  1. Step 1: Understand inclusive range requirements

    Inclusive means the value can be equal to the limits, so use ge (greater or equal) and le (less or equal).
  2. Step 2: Match parameters to the range 1 to 5 inclusive

    Use ge=1 and le=5 to allow 1 and 5 as valid values.
  3. Final Answer:

    rating: int = Field(ge=1, le=5) -> Option B
  4. Quick Check:

    Inclusive range uses ge and le [OK]
Hint: Use ge and le for inclusive ranges, gt and lt for exclusive [OK]
Common Mistakes:
  • Using gt or lt excludes boundary values
  • Mixing gt with le or ge with lt incorrectly
  • Forgetting to include both bounds
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard