0
0
FastAPIframework~20 mins

Nested models in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Nested Models Mastery
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 FastAPI nested model response?
Given the following FastAPI models and endpoint, what JSON response will the client receive when requesting /user/1?
FastAPI
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class Address(BaseModel):
    street: str
    city: str

class User(BaseModel):
    id: int
    name: str
    address: Address

@app.get('/user/{user_id}', response_model=User)
async def get_user(user_id: int):
    return {
        'id': user_id,
        'name': 'Alice',
        'address': {
            'street': '123 Main St',
            'city': 'Wonderland'
        }
    }
A{"id": 1, "name": "Alice", "address": {"street": "123 Main St", "city": "Wonderland"}}
B{"id": 1, "name": "Alice", "address": "123 Main St, Wonderland"}
C{"id": 1, "name": "Alice", "address": {"street": "123 Main St"}}
D{"id": 1, "name": "Alice"}
Attempts:
2 left
💡 Hint
Think about how nested Pydantic models serialize data in FastAPI responses.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines nested Pydantic models in FastAPI?
You want to define a nested model where Order contains a list of Item models. Which code snippet correctly defines this?
A
class Item(BaseModel):
    name: str
    price: float

class Order(BaseModel):
    id: int
    items: Item[]
B
class Item(BaseModel):
    name: str
    price: float

class Order(BaseModel):
    id: int
    items: list[Item]
C
class Item(BaseModel):
    name: str
    price: float

class Order(BaseModel):
    id: int
    items: List[Item]
D
class Item(BaseModel):
    name: str
    price: float

class Order(BaseModel):
    id: int
    items: dict[Item]
Attempts:
2 left
💡 Hint
Use Python 3.9+ built-in generic types for lists.
🔧 Debug
advanced
2:00remaining
Why does this nested model validation fail in FastAPI?
Consider these models and endpoint:
class Profile(BaseModel):
    age: int
    bio: str

class User(BaseModel):
    username: str
    profile: Profile

@app.post('/users')
async def create_user(user: User):
    return user
If the client sends {"username": "bob", "profile": {"age": "twenty", "bio": "Hi"}}, what error occurs and why?
ATypeError because 'profile' is missing required fields
BKeyError because 'bio' key is missing
CNo error, the data is accepted as is
DValidationError because 'age' expects an int but received a string 'twenty'
Attempts:
2 left
💡 Hint
Check the data types expected by Pydantic models.
state_output
advanced
2:00remaining
What is the value of user.address.city after this code runs?
Given these Pydantic models and code snippet, what is the value of user.address.city?
class Address(BaseModel):
    street: str
    city: str

class User(BaseModel):
    name: str
    address: Address

user = User(name='Eve', address={'street': '1st Ave', 'city': 'Metropolis'})
ANone
B"1st Ave"
C"Metropolis"
DRaises AttributeError
Attempts:
2 left
💡 Hint
Pydantic converts nested dicts into model instances automatically.
🧠 Conceptual
expert
2:00remaining
Which statement about nested Pydantic models in FastAPI is TRUE?
Select the one true statement about nested models in FastAPI using Pydantic.
APydantic automatically converts nested dictionaries into nested model instances during validation.
BNested models must always be declared with the <code>Field()</code> function to work correctly.
CFastAPI requires manual serialization of nested models before returning them in responses.
DYou cannot use lists of nested models in FastAPI request or response models.
Attempts:
2 left
💡 Hint
Think about how Pydantic handles nested data structures.