You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/en/docs/tutorial/response-model.md
+70Lines changed: 70 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -89,6 +89,8 @@ If you declare both a return type and a `response_model`, the `response_model` w
89
89
90
90
This way you can add correct type annotations to your functions even when you are returning a type different than the response model, to be used by the editor and tools like mypy. And still you can have FastAPI do the data validation, documentation, etc. using the `response_model`.
91
91
92
+
You can also use `response_model=None` to disable creating a response model for that *path operation*, you might need to do it if you are adding type annotations for things that are not valid Pydantic fields, you will see an example of that in one of the sections below.
93
+
92
94
## Return the same input data
93
95
94
96
Here we are declaring a `UserIn` model, it will contain a plaintext password:
@@ -244,6 +246,74 @@ And both models will be used for the interactive API documentation:
There might be cases where you return something that is not a valid Pydantic field and you annotate it in the function, only to get the support provided by tooling (the editor, mypy, etc).
252
+
253
+
### Return a Response Directly
254
+
255
+
The most common case would be [returning a Response directly as explained later in the advanced docs](../advanced/response-directly.md){.internal-link target=_blank}.
This will also work because `RedirectResponse` is a subclass of `Response`, and FastAPI will automatically handle this simple case.
274
+
275
+
### Invalid Return Type Annotations
276
+
277
+
But when you return some other arbitrary object that is not a valid Pydantic type (e.g. a database object) and you annotate it like that in the function, FastAPI will try to create a Pydantic response model from that type annotation, and will fail.
278
+
279
+
The same would happen if you had something like a <abbrtitle='A union between multiple types means "any of these types".'>union</abbr> between different types where one or more of them are not valid Pydantic types, for example this would fail 💥:
...this fails because the type annotation is not a Pydantic type and is not just a single `Response` class or subclass, it's a union (any of the two) between a `Response` and a `dict`.
294
+
295
+
### Disable Response Model
296
+
297
+
Continuing from the example above, you might not want to have the default data validation, documentation, filtering, etc. that is performed by FastAPI.
298
+
299
+
But you might want to still keep the return type annotation in the function to get the support from tools like editors and type checkers (e.g. mypy).
300
+
301
+
In this case, you can disable the response model generation by setting `response_model=None`:
This will make FastAPI skip the response model generation and that way you can have any return type annotations you need without it affecting your FastAPI application. 🤓
316
+
247
317
## Response Model encoding parameters
248
318
249
319
Your response model could have default values, like:
0 commit comments