0
0
FastAPIframework~20 mins

Response model declaration in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Response Model 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 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')
A{"id": 1, "name": "Alice", "password": "secret"}
BHTTP 500 Internal Server Error
C{"id": 1, "name": "Alice"}
D{"password": "secret"}
Attempts:
2 left
💡 Hint
Think about what the response_model does to the returned data.
📝 Syntax
intermediate
2: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')]
AList[UserResponse]
BuserResponse[]
Clist(UserResponse)
D[UserResponse]
Attempts:
2 left
💡 Hint
Use Python's typing module for type hints.
🔧 Debug
advanced
2: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"}
AThe endpoint is missing async keyword causing runtime error.
BThe response_model is missing a required field.
CThe return type is a dict instead of an Item instance, causing type error.
DThe price field is a string 'free' but should be a float, causing validation error.
Attempts:
2 left
💡 Hint
Check the data types of returned values against the model fields.
state_output
advanced
2: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
A{"password": "secret"}
B{"id": 1, "name": "Alice"}
CTypeError: Object of type UserORM is not JSON serializable
D{"id": 1, "name": "Alice", "password": "secret"}
Attempts:
2 left
💡 Hint
Consider what orm_mode does in Pydantic models.
🧠 Conceptual
expert
2: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.
AIt validates and filters the data returned by the endpoint to match the declared model before sending to the client.
BIt automatically converts the request body into the declared model type.
CIt defines the database schema for storing the response data.
DIt disables validation and returns raw data from the endpoint.
Attempts:
2 left
💡 Hint
Think about what happens to the data after the endpoint returns it.