0
0
FastAPIframework~20 mins

Pydantic model basics in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Pydantic Model Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Pydantic model validation?
Given the Pydantic model and input data below, what will be the value of user.age after validation?
FastAPI
from pydantic import BaseModel

class User(BaseModel):
    name: str
    age: int = 18

input_data = {'name': 'Alice'}
user = User(**input_data)
print(user.age)
ARaises ValidationError
BNone
C0
D18
Attempts:
2 left
💡 Hint
Check the default value assigned to the age field in the model.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a Pydantic model with an optional string field?
Select the code snippet that correctly defines a Pydantic model with an optional nickname field of type string.
A
from typing import Optional
from pydantic import BaseModel

class User(BaseModel):
    nickname: str | None = None
B
from pydantic import BaseModel

class User(BaseModel):
    nickname: str = Optional[None]
C
from typing import Optional
from pydantic import BaseModel

class User(BaseModel):
    nickname: Optional[str] = None
D
from pydantic import BaseModel

class User(BaseModel):
    nickname: str = None
Attempts:
2 left
💡 Hint
Use the typing module's Optional type and assign None as default.
state_output
advanced
2:00remaining
What error does this Pydantic model raise on invalid input?
Given the model and input below, what error will be raised when creating the model instance?
FastAPI
from pydantic import BaseModel

class Product(BaseModel):
    id: int
    price: float

input_data = {'id': 'abc', 'price': 10.5}
product = Product(**input_data)
ATypeError
Bpydantic.ValidationError
CValueError
DNo error, instance created successfully
Attempts:
2 left
💡 Hint
Check the type of the 'id' field and the input value provided.
🧠 Conceptual
advanced
2:00remaining
How does Pydantic handle extra fields by default?
If you pass extra fields not defined in the Pydantic model, what happens by default?
APydantic raises a ValidationError for extra fields
BExtra fields are ignored and not included in the model instance
CExtra fields are included as attributes in the model instance
DExtra fields overwrite existing model fields
Attempts:
2 left
💡 Hint
Think about Pydantic's strictness with unexpected data.
🔧 Debug
expert
3:00remaining
Why does this Pydantic model fail to parse nested data correctly?
Examine the code below. Why does Order fail to parse the nested Item data correctly?
FastAPI
from pydantic import BaseModel

class Item(BaseModel):
    name: str
    quantity: int

class Order(BaseModel):
    item: Item

input_data = {'item': {'name': 'Book', 'quantity': '3'}}
order = Order(**input_data)
print(order.item.quantity, type(order.item.quantity))
APydantic converts 'quantity' string '3' to int 3, so no failure occurs
BPydantic raises ValidationError because 'quantity' is a string, not int
CThe nested model is missing a Config class to allow parsing
DThe input data keys must be uppercase to match model fields
Attempts:
2 left
💡 Hint
Pydantic automatically converts compatible types during validation.