0
0
FastAPIframework~20 mins

Optional and nullable fields in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
FastAPI Optional Fields Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when sending a JSON with missing optional field?

Consider this FastAPI Pydantic model:

from pydantic import BaseModel
from typing import Optional

class Item(BaseModel):
    name: str
    description: Optional[str] = None

If you send a POST request with JSON {"name": "Book"}, what will be the value of description in the parsed Item?

FastAPI
from pydantic import BaseModel
from typing import Optional

class Item(BaseModel):
    name: str
    description: Optional[str] = None

item = Item(name="Book")
print(item.description)
AMissing attribute error
BNone
CRaises a validation error
DEmpty string ''
Attempts:
2 left
💡 Hint

Optional fields with a default value are set to that default if missing.

📝 Syntax
intermediate
2:00remaining
Which code correctly defines a nullable string field in Pydantic?

In FastAPI, you want a field comment that can be None or a string. Which of these definitions is correct?

Acomment: Optional[str] = None
Bcomment: str = None
Ccomment: str | None
Dcomment: Optional[str]
Attempts:
2 left
💡 Hint

Use Optional with a default None to allow null values.

state_output
advanced
2:00remaining
What is the output of this FastAPI endpoint with nullable field?

Given this FastAPI endpoint:

from fastapi import FastAPI
from pydantic import BaseModel
from typing import Optional

app = FastAPI()

class User(BaseModel):
    username: str
    bio: Optional[str] = None

@app.post("/users")
async def create_user(user: User):
    return {"username": user.username, "bio": user.bio or "No bio provided"}

If you POST {"username": "alice", "bio": null}, what is the JSON response?

A{"username": "alice", "bio": ""}
B{"username": "alice", "bio": null}
CValidation error because bio is null
D{"username": "alice", "bio": "No bio provided"}
Attempts:
2 left
💡 Hint

Check how user.bio or "No bio provided" works when bio is None.

🔧 Debug
advanced
2:00remaining
Why does this FastAPI model raise a validation error on null input?

Consider this Pydantic model:

from pydantic import BaseModel

class Product(BaseModel):
    name: str
    price: float
    description: str = None

When sending JSON {"name": "Pen", "price": 1.5, "description": null}, a validation error occurs. Why?

ABecause name is missing a default value
BBecause price cannot be float with decimal
CBecause description is typed as str but default None does not allow null
DBecause null is not valid JSON
Attempts:
2 left
💡 Hint

Check the type of description and how Pydantic treats default values.

🧠 Conceptual
expert
3:00remaining
How does FastAPI handle missing vs null fields in request bodies?

In FastAPI, what is the difference between a missing optional field and a field explicitly set to null in the JSON request body?

AMissing optional fields get default values; null fields map to None if type allows it
BBoth missing and null fields cause validation errors
CMissing fields are ignored; null fields are converted to empty strings
DMissing fields are set to None; null fields cause validation errors
Attempts:
2 left
💡 Hint

Think about how Pydantic treats defaults and null values.