Challenge - 5 Problems
FastAPI Response 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 response?
Consider this FastAPI endpoint that returns a structured JSON response. What will the client receive when calling this endpoint?
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': 12.99, 'extra': 'ignored'}
Attempts:
2 left
💡 Hint
Think about how FastAPI uses the response_model to filter output fields.
✗ Incorrect
FastAPI uses the response_model to serialize and validate the response. Extra fields not defined in the model are excluded by default, so 'extra' is not included in the response.
❓ state_output
intermediate2:00remaining
What is the JSON response content when returning a dict with nested data?
Given this FastAPI endpoint returning nested data, what will the JSON response look like?
FastAPI
from fastapi import FastAPI app = FastAPI() @app.get('/user') async def get_user(): return {"user": {"id": 1, "name": "Alice"}, "status": "active"}
Attempts:
2 left
💡 Hint
FastAPI automatically converts Python dicts to JSON objects preserving structure.
✗ Incorrect
FastAPI returns the Python dict as JSON, preserving nested dictionaries as nested JSON objects.
📝 Syntax
advanced2:00remaining
Which option causes a validation error due to missing required fields in response_model?
Given this Pydantic model and FastAPI endpoint, which response will cause a validation error?
FastAPI
from fastapi import FastAPI from pydantic import BaseModel app = FastAPI() class Product(BaseModel): id: int name: str @app.get('/product', response_model=Product) async def get_product(): return {}
Attempts:
2 left
💡 Hint
All required fields must be present in the response to match the model.
✗ Incorrect
Returning an empty dict misses required fields 'id' and 'name', causing a validation error.
🔧 Debug
advanced2:00remaining
Why does this FastAPI endpoint return a 422 Unprocessable Entity error?
Examine this FastAPI endpoint and identify why the client receives a 422 error when calling it.
FastAPI
from fastapi import FastAPI from pydantic import BaseModel app = FastAPI() class User(BaseModel): username: str age: int @app.post('/users') async def create_user(user: User): return user
Attempts:
2 left
💡 Hint
422 errors often mean request data does not match expected model.
✗ Incorrect
If the client omits required fields like 'age', FastAPI returns 422 because it cannot validate the input.
🧠 Conceptual
expert2:00remaining
Why is using structured responses important in FastAPI APIs?
Select the best reason why structured responses matter in FastAPI applications.
Attempts:
2 left
💡 Hint
Think about how clients use API responses and why consistency matters.
✗ Incorrect
Structured responses enforce a clear contract between server and client, improving reliability and maintainability.