Challenge - 5 Problems
Response Model 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 endpoint with a response model?
Consider this FastAPI code snippet. What will the client receive when calling the endpoint?
FastAPI
from fastapi import FastAPI from pydantic import BaseModel app = FastAPI() class User(BaseModel): id: int name: str password: str class UserResponse(BaseModel): id: int name: str @app.get('/user', response_model=UserResponse) async def get_user(): return User(id=1, name='Alice', password='secret')
Attempts:
2 left
💡 Hint
Think about what the response_model does to the returned data.
✗ Incorrect
The response_model tells FastAPI to filter the output to only include the fields declared in UserResponse. So the password field is excluded from the response sent to the client.
📝 Syntax
intermediate2:00remaining
Which option correctly declares a response model for a list of items in FastAPI?
You want to return a list of UserResponse models from a FastAPI endpoint. Which code snippet correctly declares the response model?
FastAPI
from fastapi import FastAPI from pydantic import BaseModel from typing import List app = FastAPI() class UserResponse(BaseModel): id: int name: str @app.get('/users', response_model=??? ) async def get_users(): return [UserResponse(id=1, name='Alice'), UserResponse(id=2, name='Bob')]
Attempts:
2 left
💡 Hint
Use Python's typing module for type hints.
✗ Incorrect
FastAPI expects the response_model to be a type hint. For lists, use List[Model]. The other options are invalid syntax or not recognized types.
🔧 Debug
advanced2:00remaining
Why does this FastAPI endpoint raise a validation error despite correct response_model?
Examine the code and identify why a validation error occurs when returning the data.
FastAPI
from fastapi import FastAPI from pydantic import BaseModel app = FastAPI() class Item(BaseModel): name: str price: float @app.get('/item', response_model=Item) async def get_item(): return {"name": "Book", "price": "free"}
Attempts:
2 left
💡 Hint
Check the data types of returned values against the model fields.
✗ Incorrect
Pydantic validates the returned data against the response_model. The price field expects a float but got a string 'free', so validation fails.
❓ state_output
advanced2:00remaining
What is the output when using response_model with orm_mode enabled?
Given this FastAPI code using an ORM model, what will the client receive?
FastAPI
from fastapi import FastAPI from pydantic import BaseModel app = FastAPI() class UserORM: def __init__(self, id, name, password): self.id = id self.name = name self.password = password class UserResponse(BaseModel): id: int name: str class Config: orm_mode = True @app.get('/user', response_model=UserResponse) async def get_user(): user = UserORM(1, 'Alice', 'secret') return user
Attempts:
2 left
💡 Hint
Consider what orm_mode does in Pydantic models.
✗ Incorrect
orm_mode allows Pydantic to read data from ORM objects by accessing attributes. The response_model filters fields to only id and name, so password is excluded.
🧠 Conceptual
expert2:00remaining
Which statement best describes the purpose of response_model in FastAPI?
Choose the most accurate explanation of what response_model does in a FastAPI endpoint.
Attempts:
2 left
💡 Hint
Think about what happens to the data after the endpoint returns it.
✗ Incorrect
response_model tells FastAPI to validate and serialize the returned data according to the model, ensuring the client only receives the specified fields and correct types.