Replies: 3 comments 1 reply
-
|
Hello @AlberLC, Response model
Response class
Typical flow In FastAPI, a functionβs return type (also used for docs) fills the response model, not the response class. By default FastAPI uses JSONResponse as the response class, which is why youβre seeing the wrong MIME type. app = FastAPI(default_response_class=HTMLResponse)Hope this clarifies it! |
Beta Was this translation helpful? Give feedback.
-
|
Thanks for your reply. I totally understand what youβre saying, everything you explained is true: thereβs a clear difference between However, in my opinion, that doesnβt mean it couldnβt be leveraged to make things more powerful and consistent. Just look at how clean and intuitive the code looks, it feels natural and in line with the elegance FastAPI already provides. Iβm not sure how difficult it would be to implement having the response type information derived from the same place as the model (the return type annotations), but I think it would be a really nice addition. |
Beta Was this translation helpful? Give feedback.
-
|
Hi again, Iβve revisited this topic over the past months and spent time analyzing FastAPIβs internal routing and OpenAPI generation logic more carefully. My initial goal was to explore whether return type annotations could be leveraged more consistently, similar to how Pydantic models are handled, but without affecting runtime behavior or introducing fragile heuristics. After reviewing related discussions and implementation details (including the new JSONL streaming and SSE support), I implemented a prototype that:
The change does not alter response rendering logic and preserves existing runtime semantics, while expanding support for unions of Iβve opened a PR with full implementation details and comprehensive test coverage: #15040 Iβd really appreciate feedback. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
First Check
Commit to Help
Example Code
Description
Hi!
First of all, I want to say I really love FastAPI and the incredible amount of work it saves when building APIs. I'm also a strong believer in leveraging the language's own features whenever possible, like using Python's return type annotations instead of always relying on
response_modelorresponse_class.One thing that has been bothering me is an possible inconsistency I've noticed: when I use a Pydantic model as a return type annotation, FastAPI automatically generates documentation with the correct schema, status code, and content type. However, when I annotate a function to return
HTMLResponseorRedirectResponse, the automatic documentation does not pick up the MIME type or the status code, and I am forced to specifyresponse_classorresponsesmanually.Since my methods always have return type annotations, it feels natural and cleaner to rely on them for documentation whenever possible. It would be great if FastAPI could support automatic OpenAPI documentation for
Responsesubclasses directly from the return type annotation, just like it does for Pydantic models. Or maybe Iβm missing something or lacking experience here π€. Iβd love to be convinced that the current approach is the right way.Thanks for your insights!
Operating System
Windows
Operating System Details
No response
FastAPI Version
0.115.12
Pydantic Version
2.11.3
Python Version
Python 3.13.3
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions