If you have a big application, you might end up accumulating several tags, and you would want to make sure you always use the same tag for related path operations.
In these cases, it could make sense to store the tags in an Enum.
FastAPI supports that the same way as with plain strings:
fromfastapiimportFastAPIfrompydanticimportBaseModelapp=FastAPI()classItem(BaseModel):name:strdescription:str|None=Noneprice:floattax:float|None=Nonetags:set[str]=set()@app.post("/items/",response_model=Item,summary="Create an item",description="Create an item with all the information, name, description, price, tax and a set of unique tags",)asyncdefcreate_item(item:Item):returnitem
fromtypingimportUnionfromfastapiimportFastAPIfrompydanticimportBaseModelapp=FastAPI()classItem(BaseModel):name:strdescription:Union[str,None]=Noneprice:floattax:Union[float,None]=Nonetags:set[str]=set()@app.post("/items/",response_model=Item,summary="Create an item",description="Create an item with all the information, name, description, price, tax and a set of unique tags",)asyncdefcreate_item(item:Item):returnitem
fromtypingimportSet,UnionfromfastapiimportFastAPIfrompydanticimportBaseModelapp=FastAPI()classItem(BaseModel):name:strdescription:Union[str,None]=Noneprice:floattax:Union[float,None]=Nonetags:Set[str]=set()@app.post("/items/",response_model=Item,summary="Create an item",description="Create an item with all the information, name, description, price, tax and a set of unique tags",)asyncdefcreate_item(item:Item):returnitem
As descriptions tend to be long and cover multiple lines, you can declare the path operation description in the function docstring and FastAPI will read it from there.
You can write Markdown in the docstring, it will be interpreted and displayed correctly (taking into account docstring indentation).
fromfastapiimportFastAPIfrompydanticimportBaseModelapp=FastAPI()classItem(BaseModel):name:strdescription:str|None=Noneprice:floattax:float|None=Nonetags:set[str]=set()@app.post("/items/",response_model=Item,summary="Create an item")asyncdefcreate_item(item:Item):""" Create an item with all the information: - **name**: each item must have a name - **description**: a long description - **price**: required - **tax**: if the item doesn't have tax, you can omit this - **tags**: a set of unique tag strings for this item """returnitem
fromtypingimportUnionfromfastapiimportFastAPIfrompydanticimportBaseModelapp=FastAPI()classItem(BaseModel):name:strdescription:Union[str,None]=Noneprice:floattax:Union[float,None]=Nonetags:set[str]=set()@app.post("/items/",response_model=Item,summary="Create an item")asyncdefcreate_item(item:Item):""" Create an item with all the information: - **name**: each item must have a name - **description**: a long description - **price**: required - **tax**: if the item doesn't have tax, you can omit this - **tags**: a set of unique tag strings for this item """returnitem
fromtypingimportSet,UnionfromfastapiimportFastAPIfrompydanticimportBaseModelapp=FastAPI()classItem(BaseModel):name:strdescription:Union[str,None]=Noneprice:floattax:Union[float,None]=Nonetags:Set[str]=set()@app.post("/items/",response_model=Item,summary="Create an item")asyncdefcreate_item(item:Item):""" Create an item with all the information: - **name**: each item must have a name - **description**: a long description - **price**: required - **tax**: if the item doesn't have tax, you can omit this - **tags**: a set of unique tag strings for this item """returnitem
You can specify the response description with the parameter response_description:
fromfastapiimportFastAPIfrompydanticimportBaseModelapp=FastAPI()classItem(BaseModel):name:strdescription:str|None=Noneprice:floattax:float|None=Nonetags:set[str]=set()@app.post("/items/",response_model=Item,summary="Create an item",response_description="The created item",)asyncdefcreate_item(item:Item):""" Create an item with all the information: - **name**: each item must have a name - **description**: a long description - **price**: required - **tax**: if the item doesn't have tax, you can omit this - **tags**: a set of unique tag strings for this item """returnitem
fromtypingimportUnionfromfastapiimportFastAPIfrompydanticimportBaseModelapp=FastAPI()classItem(BaseModel):name:strdescription:Union[str,None]=Noneprice:floattax:Union[float,None]=Nonetags:set[str]=set()@app.post("/items/",response_model=Item,summary="Create an item",response_description="The created item",)asyncdefcreate_item(item:Item):""" Create an item with all the information: - **name**: each item must have a name - **description**: a long description - **price**: required - **tax**: if the item doesn't have tax, you can omit this - **tags**: a set of unique tag strings for this item """returnitem
fromtypingimportSet,UnionfromfastapiimportFastAPIfrompydanticimportBaseModelapp=FastAPI()classItem(BaseModel):name:strdescription:Union[str,None]=Noneprice:floattax:Union[float,None]=Nonetags:Set[str]=set()@app.post("/items/",response_model=Item,summary="Create an item",response_description="The created item",)asyncdefcreate_item(item:Item):""" Create an item with all the information: - **name**: each item must have a name - **description**: a long description - **price**: required - **tax**: if the item doesn't have tax, you can omit this - **tags**: a set of unique tag strings for this item """returnitem
Info
Notice that response_description refers specifically to the response, the description refers to the path operation in general.
Check
OpenAPI specifies that each path operation requires a response description.
So, if you don't provide one, FastAPI will automatically generate one of "Successful response".