Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

127
Views
How to add docs to post body model on fast api view?

For example imagine that we have two endpoints:

class FooRequest(BaseModel):
    data: str


@router.post("/foo/", response_model=FooRequest)
async def foo_view(data: FooRequest) -> FooRequest:
    ...


@router.get("/bar/", response_model=FooRequest)
async def bar_view(data: str = Query(..., description="Data param")) -> FooRequest:
    ...

In swagger UI /bar/ endpoint will have properly documented query param and /foo/ will have some abstract example of post body without any description.

So how can I document post body model?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can declare an example for a Pydantic model using Config and schema_extra.

class FooRequest(BaseModel):
    data: str

    class Config:
        schema_extra = {
            "FooRequest": {
                "name": "Foo Request",
                "description": "Data param",
            }
        }

Also with Field you declare extra info for your JSON Schema.

from pydantic import Field

...

class FooRequest(BaseModel):
    data: str = Field(..., example="Data param for Foo Request")
    description: Optional[str] = Field(None, example="Description for Foo")

The same way you can pass extra info to Field, you can do the same with Path, Query, Body, etc.

For example, you can pass an example for a body request to Body:

from fastapi import Body

...

class FooRequest(BaseModel):
    data: str


@router.post("/foo/", response_model=FooRequest)
async def foo_view(data: FooRequest = Body(
            ...,
            example={
                "name": "Foo Request",
                "description": "data param",
            },
        ),
    ) -> FooRequest:
    
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!