Ability to define response_model with native typing
First Check
- I added a very descriptive title to this issue.
- I used the GitHub search to find a similar issue and didn't find it.
- I searched the FastAPI documentation, with the integrated search.
- I already searched in Google "How to X in FastAPI" and didn't find any information.
- I already read and followed all the tutorial in the docs and didn't find an answer.
- I already checked if it is not related to FastAPI but to Pydantic.
- I already checked if it is not related to FastAPI but to Swagger UI.
- I already checked if it is not related to FastAPI but to ReDoc.
Commit to Help
- I commit to help with one of those options 👆
Example Code
from fastapi import FastAPI from pydantic import BaseModel app = FastAPI() class TypicalResponse(BaseModel): msg: str data: int @app.get("/add/{x}/{y}") async def add(x: int, y: int) -> TypicalResponse: return TypicalResponse(msg="Done!", data=x + y)
Description
Open the automatic doc on this handler
See no output schema defined
I would like to use typing "-> Type" for annotate response model.
This is good for simple handlers.
Wanted Solution
I would like to autodoc parse this typing same as "response_model=TypicalResponse"
Wanted Code
from fastapi import FastAPI from pydantic import BaseModel app = FastAPI() # Same class TypicalResponse(BaseModel): msg: str data: int @app.get("/add/{x}/{y}") async def add(x: int, y: int) -> TypicalResponse: return TypicalResponse(msg="Done!", data=x + y)
Alternatives
I use kwarg "response_model" as single alternative
But typing already defines the output model, necessary duplicate as result.
Operating System
Linux
Operating System Details
No response
FastAPI Version
0.79.0
Python Version
Python 3.9.0
Additional Context
I think second is better by avoid information duplication
Meanwhile, solution is quite simple.
One of the ways to implement (don't judge too much, that's first idea):
May be dirty, so welcome to search another ways.


