0
0
FastAPIframework~10 mins

Response model exclude and include in FastAPI - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Response model exclude and include
Define Pydantic Model
Create FastAPI Endpoint
Return Model Instance
Apply include/exclude
Filter fields in response
Send filtered JSON to client
The flow shows how FastAPI uses Pydantic models and filters fields with include/exclude before sending the response.
Execution Sample
FastAPI
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

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

@app.get("/user")
async def get_user():
    return User(id=1, name="Alice", email="alice@example.com").model_dump(include={"id", "name"})
This code returns a User but only includes 'id' and 'name' fields in the response.
Execution Table
StepActionModel InstanceInclude/ExcludeFiltered Output
1Create User instanceUser(id=1, name='Alice', email='alice@example.com')NoneFull model data
2Apply includeUser(id=1, name='Alice', email='alice@example.com')Include: {'id', 'name'}{'id': 1, 'name': 'Alice'}
3Send response to client{'id': 1, 'name': 'Alice'}N/AJSON with only id and name
💡 Response sent with only included fields as per include
Variable Tracker
VariableStartAfter Step 1After Step 2Final
user_instanceNoneUser(id=1, name='Alice', email='alice@example.com')Same instanceSame instance
response_dataNoneFull model dict{'id': 1, 'name': 'Alice'}{'id': 1, 'name': 'Alice'}
Key Moments - 2 Insights
Why does the 'email' field not appear in the response even though it exists in the model?
Because include specifies only 'id' and 'name' to be included, so 'email' is filtered out before sending (see execution_table step 2).
What happens if both include and exclude are used together?
FastAPI applies include first to select fields, then exclude removes fields from that selection. This combined filtering controls exactly which fields appear.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the filtered output at step 2?
A{'id': 1, 'name': 'Alice'}
B{'id': 1, 'name': 'Alice', 'email': 'alice@example.com'}
C{'email': 'alice@example.com'}
D{}
💡 Hint
Check the 'Filtered Output' column at step 2 in the execution_table.
At which step is the response data filtered to include only certain fields?
AStep 3
BStep 1
CStep 2
DFiltering does not happen
💡 Hint
Look at the 'Action' column in execution_table where include/exclude is applied.
If exclude={'email'} was used instead of include, what would the filtered output be?
A{}
B{'id': 1, 'name': 'Alice'}
C{'id': 1, 'name': 'Alice', 'email': 'alice@example.com'}
D{'email': 'alice@example.com'}
💡 Hint
response_model_exclude removes specified fields from full model data.
Concept Snapshot
FastAPI response_model can filter fields using include or exclude.
Use include to specify fields to keep.
Use exclude to specify fields to remove.
Filtering happens after model instance creation, before sending response.
This controls what JSON data client receives.
Full Transcript
This visual execution shows how FastAPI uses Pydantic models to define response data. When an endpoint returns a model instance, FastAPI can filter which fields to send using include or exclude. The flow starts with creating a model instance with all fields. Then, include or exclude filters are applied to select or remove fields. Finally, the filtered JSON is sent to the client. The execution table traces each step: creating the instance, applying filters, and sending the response. Variable tracking shows how the user instance stays the same but the response data changes after filtering. Key moments clarify why some fields are missing and how include and exclude work together. The quiz tests understanding of filtering steps and results. This helps beginners see exactly how FastAPI controls response content using these options.