0
0
FastAPIframework~20 mins

Response model exclude and include 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 when using response_model_exclude in FastAPI?
Consider this FastAPI endpoint using a Pydantic model with response_model_exclude to hide some fields. What will the JSON response include?
FastAPI
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class User(BaseModel):
    id: int
    username: str
    email: str

@app.get("/user", response_model=User, response_model_exclude={"email"})
def get_user():
    return User(id=1, username="alice", email="alice@example.com")
A{"id": 1, "username": "alice"}
B{"id": 1, "username": "alice", "email": "alice@example.com"}
C{"username": "alice", "email": "alice@example.com"}
D{"id": 1, "email": "alice@example.com"}
Attempts:
2 left
💡 Hint
Think about which fields are excluded from the response model.
component_behavior
intermediate
2:00remaining
How does response_model_include affect the FastAPI response?
Given this FastAPI endpoint, what fields will appear in the JSON response when using response_model_include?
FastAPI
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class Product(BaseModel):
    id: int
    name: str
    price: float
    description: str

@app.get("/product", response_model=Product, response_model_include={"id", "name"})
def get_product():
    return Product(id=10, name="Book", price=12.99, description="A nice book")
A{"id": 10, "price": 12.99, "description": "A nice book"}
B{"id": 10, "name": "Book"}
C{"id": 10, "name": "Book", "price": 12.99}
D{"name": "Book", "price": 12.99, "description": "A nice book"}
Attempts:
2 left
💡 Hint
Only the fields listed in response_model_include will be shown.
📝 Syntax
advanced
2:00remaining
What error occurs with incorrect response_model_exclude syntax?
Which option shows the error raised by this FastAPI endpoint with wrong response_model_exclude syntax?
FastAPI
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class Item(BaseModel):
    id: int
    name: str
    price: float

@app.get("/item", response_model=Item, response_model_exclude=["price"])
def get_item():
    return Item(id=5, name="Pen", price=1.5)
ANo error, runs correctly
BSyntaxError: invalid syntax
CTypeError: unhashable type: 'list'
DValueError: field 'price' not found
Attempts:
2 left
💡 Hint
Check the type expected for response_model_exclude.
state_output
advanced
2:00remaining
What is the output when combining response_model_include and response_model_exclude?
Given this FastAPI endpoint, what fields will appear in the JSON response?
FastAPI
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class Profile(BaseModel):
    id: int
    username: str
    email: str
    bio: str

@app.get(
    "/profile",
    response_model=Profile,
    response_model_include={"id", "username", "email"},
    response_model_exclude={"email"}
)
def get_profile():
    return Profile(id=2, username="bob", email="bob@example.com", bio="Hello!")
A{"id": 2, "username": "bob", "email": "bob@example.com"}
B{"id": 2, "username": "bob", "bio": "Hello!"}
C{"username": "bob", "email": "bob@example.com", "bio": "Hello!"}
D{"id": 2, "username": "bob"}
Attempts:
2 left
💡 Hint
Remember that response_model_exclude removes fields even if included.
🔧 Debug
expert
3:00remaining
Why does this FastAPI endpoint return all fields despite response_model_exclude?
Analyze this code. Why does the response include all fields even though response_model_exclude is set?
FastAPI
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class Data(BaseModel):
    a: int
    b: int
    c: int

@app.get("/data", response_model=Data, response_model_exclude={"b"})
def get_data():
    return {"a": 1, "b": 2, "c": 3}
AThe response includes all fields because the returned value is a dict, not a Pydantic model instance.
BThe response includes all fields because <code>response_model_exclude</code> only works with lists, not sets.
CThe response includes all fields because <code>response_model_exclude</code> is ignored when returning dicts.
DThe response includes all fields because the endpoint is missing a <code>return</code> statement.
Attempts:
2 left
💡 Hint
Think about how FastAPI processes the returned data with response models.