question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Ability to define response_model with native typing

See original GitHub issue

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

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):

  1. Open APIRouter class
  2. Modify self.add_api_route by adding изображение

May be dirty, so welcome to search another ways.

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:7
  • Comments:20 (6 by maintainers)

github_iconTop GitHub Comments

5reactions
chbndrhnnscommented, Jul 31, 2022

This idea has been rejected previously: https://github.com/tiangolo/fastapi/issues/101

2reactions
AbstractiveNordcommented, Oct 3, 2022

Essential… Well, i mean it’s because this functionality expected to be as basic (by any newbie at least). FastAPI have Pydantic native intergration, very pythonic style, you may think “wow, nice framework!” And boom! - This typing isn’t working. Not critical, but still.

Exactly - I literally went through this! Would be great if we can somehow make this work. If this indeed breaks something, can this configuration be used as an input for those who wants to go use the return types instead of duplicating it in reponse_model?

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found