docs: clarify purpose and behavior of response_model

This commit is contained in:
NEHRA 2026-01-06 19:59:50 +05:30
parent f2687dc1bb
commit c0db4cc744
1 changed files with 36 additions and 0 deletions

View File

@ -47,6 +47,42 @@ Notice that `response_model` is a parameter of the "decorator" method (`get`, `p
`response_model` receives the same type you would declare for a Pydantic model field, so, it can be a Pydantic model, but it can also be, e.g. a `list` of Pydantic models, like `List[Item]`.
### Why use `response_model`
The `response_model` parameter is used to **control and validate the data
returned in responses**, independently from the request body model.
This is useful for several reasons:
- To **filter out sensitive fields** (e.g. passwords, internal IDs)
- To **guarantee a stable response shape**
- To avoid accidentally returning extra data
For example:
```python
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class UserInDB(BaseModel):
username: str
email: str
hashed_password: str
class UserPublic(BaseModel):
username: str
email: str
@app.get("/user/", response_model=UserPublic)
async def get_user():
return UserInDB(
username="alice",
email="alice@example.com",
hashed_password="secret"
)
FastAPI will use this `response_model` to do all the data documentation, validation, etc. and also to **convert and filter the output data** to its type declaration.
/// tip