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

194
Views
Pydantic: Specify value as required yet input example value

I am using Pydantic in FastAPI, to define in an OpenAPI doc. Realised that to define a value as required, I need to keep the values empty, like below.

class Item(BaseModel):
    name: str
    description: str
    price: float
    tax: float

However, I wanted to give an the JSON with example values, which I can create with the below syntax.

class Item(BaseModel):
    name: str = "my name"
    description: str = "my description"
    price: float = 0.234
    tax: float = 0.20

But this will render them as not required in OpenAPI docs. Is there any way where I can specify them as required, and yet give an example value?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Based on docs Schema Extra - Example:

You can declare extra Config.schema_extra for your BaseModel as such:

class Item(BaseModel):
    name: str
    description: str
    price: float
    tax: float

    class Config:
        schema_extra = {
            "example": {
                "name": "my name",
                "description": "my description",
                "price": 0.234,
                "tax": 0.20
            }
        }

Or declare explicitly with pydantic.Field:

from pydantic import BaseModel, Field

class Item(BaseModel):
    name: str = Field(..., example="my name")
    description: str = Field(..., example="my description")
    price: float = Field(..., example=0.234)
    tax: float = Field(..., example=0.20)
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!