Ability to define response_model with native typing
See original GitHub issueFirst 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
Without patch:
With patch:
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):
- Open APIRouter class
- Modify
self.add_api_route
by adding
May be dirty, so welcome to search another ways.
Issue Analytics
- State:
- Created a year ago
- Reactions:7
- Comments:20 (6 by maintainers)
Top Results From Across the Web
Typehints instead of response_model · Issue #2296 - GitHub
Ability to define response_model with native typing #5215 ... https://fastapi.tiangolo.com/tutorial/response-model/#add-an-output-model.
Read more >typing — Support for type hints — Python 3.11.1 documentation
Source code: Lib/typing.py This module provides runtime support for type hints. The most fundamental support consists of the types Any, Union, Callable, ...
Read more >Response Model - FastAPI
FastAPI will use this response_model to: Convert the output data to its type declaration. Validate the data. Add a JSON Schema for the...
Read more >What is Natural Language Processing? An Introduction to NLP
Natural language processing (NLP ) is the ability of a computer program to understand human language as it is spoken and written --...
Read more >Knowledge, Skills, Abilities and Other Characteristics (KSAOs)
Skills are the capabilities require to perform tasks accurately, such as psychomotor activities like typing speed or driving ability.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This idea has been rejected previously: https://github.com/tiangolo/fastapi/issues/101
My patch is very soft.It checks response_model to be None before grabbing model from annotations.I can agree with problems, mypy, everything, but for newbies it’s great, meanwhile everyone can directly set response_model and my patch will not do a thing.
Welcome to test