Body - Fields¶
The same way you can declare additional validation and metadata inpath operation function parameters withQuery,Path andBody, you can declare validation and metadata inside of Pydantic models using Pydantic'sField.
ImportField¶
First, you have to import it:
fromtypingimportAnnotatedfromfastapiimportBody,FastAPIfrompydanticimportBaseModel,Fieldapp=FastAPI()classItem(BaseModel):name:strdescription:str|None=Field(default=None,title="The description of the item",max_length=300)price:float=Field(gt=0,description="The price must be greater than zero")tax:float|None=None@app.put("/items/{item_id}")asyncdefupdate_item(item_id:int,item:Annotated[Item,Body(embed=True)]):results={"item_id":item_id,"item":item}returnresults🤓 Other versions and variants
Tip
Prefer to use theAnnotated version if possible.
fromfastapiimportBody,FastAPIfrompydanticimportBaseModel,Fieldapp=FastAPI()classItem(BaseModel):name:strdescription:str|None=Field(default=None,title="The description of the item",max_length=300)price:float=Field(gt=0,description="The price must be greater than zero")tax:float|None=None@app.put("/items/{item_id}")asyncdefupdate_item(item_id:int,item:Item=Body(embed=True)):results={"item_id":item_id,"item":item}returnresultsWarning
Notice thatField is imported directly frompydantic, not fromfastapi as are all the rest (Query,Path,Body, etc).
Declare model attributes¶
You can then useField with model attributes:
fromtypingimportAnnotatedfromfastapiimportBody,FastAPIfrompydanticimportBaseModel,Fieldapp=FastAPI()classItem(BaseModel):name:strdescription:str|None=Field(default=None,title="The description of the item",max_length=300)price:float=Field(gt=0,description="The price must be greater than zero")tax:float|None=None@app.put("/items/{item_id}")asyncdefupdate_item(item_id:int,item:Annotated[Item,Body(embed=True)]):results={"item_id":item_id,"item":item}returnresults🤓 Other versions and variants
Tip
Prefer to use theAnnotated version if possible.
fromfastapiimportBody,FastAPIfrompydanticimportBaseModel,Fieldapp=FastAPI()classItem(BaseModel):name:strdescription:str|None=Field(default=None,title="The description of the item",max_length=300)price:float=Field(gt=0,description="The price must be greater than zero")tax:float|None=None@app.put("/items/{item_id}")asyncdefupdate_item(item_id:int,item:Item=Body(embed=True)):results={"item_id":item_id,"item":item}returnresultsField works the same way asQuery,Path andBody, it has all the same parameters, etc.
Technical Details
Actually,Query,Path and others you'll see next create objects of subclasses of a commonParam class, which is itself a subclass of Pydantic'sFieldInfo class.
And Pydantic'sField returns an instance ofFieldInfo as well.
Body also returns objects of a subclass ofFieldInfo directly. And there are others you will see later that are subclasses of theBody class.
Remember that when you importQuery,Path, and others fromfastapi, those are actually functions that return special classes.
Tip
Notice how each model's attribute with a type, default value andField has the same structure as apath operation function's parameter, withField instead ofPath,Query andBody.
Add extra information¶
You can declare extra information inField,Query,Body, etc. And it will be included in the generated JSON Schema.
You will learn more about adding extra information later in the docs, when learning to declare examples.
Warning
Extra keys passed toField will also be present in the resulting OpenAPI schema for your application.As these keys may not necessarily be part of the OpenAPI specification, some OpenAPI tools, for examplethe OpenAPI validator, may not work with your generated schema.
Recap¶
You can use Pydantic'sField to declare extra validations and metadata for model attributes.
You can also use the extra keyword arguments to pass additional JSON Schema metadata.







