So, even with the code above that doesn't use Pydantic explicitly, FastAPI is using Pydantic to convert those standard dataclasses to Pydantic's own flavor of dataclasses.
And of course, it supports the same:
data validation
data serialization
data documentation, etc.
This works the same way as with Pydantic models. And it is actually achieved in the same way underneath, using Pydantic.
Info
Keep in mind that dataclasses can't do everything Pydantic models can do.
So, you might still need to use Pydantic models.
But if you have a bunch of dataclasses laying around, this is a nice trick to use them to power a web API using FastAPI. 🤓
You can also use dataclasses in the response_model parameter:
fromdataclassesimportdataclass,fieldfromtypingimportList,UnionfromfastapiimportFastAPI@dataclassclassItem:name:strprice:floattags:List[str]=field(default_factory=list)description:Union[str,None]=Nonetax:Union[float,None]=Noneapp=FastAPI()@app.get("/items/next",response_model=Item)asyncdefread_next_item():return{"name":"Island In The Moon","price":12.99,"description":"A place to be be playin' and havin' fun","tags":["breater"],}
The dataclass will be automatically converted to a Pydantic dataclass.
This way, its schema will show up in the API docs user interface:
You can also combine dataclasses with other type annotations to make nested data structures.
In some cases, you might still have to use Pydantic's version of dataclasses. For example, if you have errors with the automatically generated API documentation.
In that case, you can simply swap the standard dataclasses with pydantic.dataclasses, which is a drop-in replacement:
fromdataclassesimportfield# fromtypingimportList,UnionfromfastapiimportFastAPIfrompydantic.dataclassesimportdataclass# @dataclassclassItem:name:strdescription:Union[str,None]=None@dataclassclassAuthor:name:stritems:List[Item]=field(default_factory=list)# app=FastAPI()@app.post("/authors/{author_id}/items/",response_model=Author)# asyncdefcreate_author_items(author_id:str,items:List[Item]):# return{"name":author_id,"items":items}# @app.get("/authors/",response_model=List[Author])# defget_authors():# return[# {"name":"Breaters","items":[{"name":"Island In The Moon","description":"A place to be be playin' and havin' fun",},{"name":"Holy Buddies"},],},{"name":"System of an Up","items":[{"name":"Salt","description":"The kombucha mushroom people's favorite",},{"name":"Pad Thai"},{"name":"Lonely Night","description":"The mostests lonliest nightiest of allest",},],},]
You can combine dataclasses with other type annotations in many different combinations to form complex data structures.
Check the in-code annotation tips above to see more specific details.