Challenge - 5 Problems
Nested Models Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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' } }
Attempts:
2 left
💡 Hint
Think about how nested Pydantic models serialize data in FastAPI responses.
✗ Incorrect
The nested Address model is included as a dictionary inside the User model's address field. FastAPI uses Pydantic to serialize nested models into nested JSON objects.
📝 Syntax
intermediate2: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?Attempts:
2 left
💡 Hint
Use Python 3.9+ built-in generic types for lists.
✗ Incorrect
Option B uses the correct Python 3.9+ syntax for a list of Item models: list[Item]. Option B would be correct if List was imported from typing, but it is not shown here. Options B and D use invalid syntax.
🔧 Debug
advanced2: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?Attempts:
2 left
💡 Hint
Check the data types expected by Pydantic models.
✗ Incorrect
Pydantic validates types strictly. The 'age' field expects an integer but receives a string 'twenty', causing a ValidationError.
❓ state_output
advanced2: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'})Attempts:
2 left
💡 Hint
Pydantic converts nested dicts into model instances automatically.
✗ Incorrect
The address dict is converted to an Address model instance. Accessing user.address.city returns the city string 'Metropolis'.
🧠 Conceptual
expert2:00remaining
Which statement about nested Pydantic models in FastAPI is TRUE?
Select the one true statement about nested models in FastAPI using Pydantic.
Attempts:
2 left
💡 Hint
Think about how Pydantic handles nested data structures.
✗ Incorrect
Pydantic automatically parses nested dictionaries into nested model instances, simplifying data validation and serialization in FastAPI.