0

I try to create a simple FastAPI app:

from pydantic import BaseModel

class RephrasingRequest(BaseModel):
    content: str
    """The content to rephrase"""

    n_outputs: int = 1
    """How many examples to generate?"""




app_debug = FastAPI(
    title="debug", description="View project's README.md for details", docs_url="/"
)


@app_debug.post("/rephrase")
async def rephrase_question(request: RephrasingRequest) -> str:
    msg = "got the request " + str(request)
    return msg

When I go to the app SwaggerUI page, I see the endpoint documentation, but it says "No parameters", as if the function does not accept parameters.

screenshot of the original doc page

Also, and I think this is related, the UI doesn't format the documentation properly. For example, if I add the following docstring:


@app_debug.post("/rephrase")
async def rephrase_question(request: RephrasingRequest) -> str:
    """
    This is a sample doc.

    :param request:
        The request

    :return:
        What we return
    """

    msg = "got the request " + str(request)
    return msg

the result is malformatted screenshot of the modififed doc page

(note that the formatting strings are there)

I expected to see propely formatted documentation, including a proper list of parameters

  • The function does not accept parameters (which would be `?foo=123`). It _does show_ the expected _request body_, which matches what you've defined. The comment format is Markdown, and markdown does not consider a new line as anything other than plain text (i.e. not a break) if you've just indented the next line. You can test it in the editor here on SO. If you want to have a newline, you can usually do that by using the expected Mardown format: https://stackoverflow.com/questions/33191744/how-to-add-new-line-in-markdown-presentation (end the line with two spaces) – MatsLindh Mar 08 '23 at 15:11
  • > the function does not accept parameters But it does (`request`), unless FastAPI calls this something else? – BBorchik Pomidorchik Mar 08 '23 at 15:18
  • You're confusing _arguments to your HTTP endpoint_ and _arguments to the function_. You have a Pydantic model as the argument, this is mapped as a _request body_ to HTTP endpoint (which you can see in your screenshot below the part about parameters). When you're talking about parameters _to the endpoint_, those would be what you find in a query string (`?foo=bar`). The model you've told your function to expect has to be mapped to a JSON _body_, and not URL parameters (since it can be a complex model). If you want query parameters, use `def foo(bar: int)` directly or `bar: int = Query(...)`. – MatsLindh Mar 08 '23 at 19:21
  • @MatsLindh, thank you very much. If you could write your comments as an answer, I will be able to mark it "accepted" – BBorchik Pomidorchik Mar 09 '23 at 11:12

1 Answers1

0

You're confusing arguments to your HTTP endpoint and arguments to your python function.

You have a Pydantic model as the argument, this is mapped as a request body to HTTP endpoint (which you can see in your screenshot below the part about parameters).

When you're talking about parameters to the endpoint, those would be what you find in a query string (?foo=bar). The model you've told your function to expect has to be mapped to a JSON body, and not URL parameters (since it can be a complex model).

If you want query parameters, use:

def foo(bar: int):
# or
def foo(bar: int = Query(...)):

The comment format is Markdown, and markdown does not consider a new line as anything other than plain text (i.e. not a break) if you've just indented the next line. If you want to have a newline, you can usually do that by using the expected Markdown format (end the line with two spaces)

MatsLindh
  • 49,529
  • 4
  • 53
  • 84