r/FastAPI • • Oct 15 '21

Question Pydantics relation with FastAPI?

I was just wondering what the deal is with Pydantic. Is it built into FastAPI or is it just like a closely tied partner library?

Mentioned everywhere in the docs and at first I thought these were FastAPI features, but realized they were actually from this other library.

For example could you run FastAPI and all it's features without using any Pydantic? (Not that I want to, just curious). I feel like the dependency injector system might depend on it but not sure.

1 Upvotes

10 comments sorted by

3

u/[deleted] Oct 15 '21

This is covered on the home page for FastAPI. FastAPI is built on top of starlette and pydantic.

1

u/AlabamaSky967 Oct 15 '21

So is it fair to say they are required included dependencies with FastAPI and without them FastAPI would break ?

However they aren't built directly into FastAPI

1

u/tommytwoeyes Oct 15 '21

This is not strictly true — it depends on what you mean by “run FastAPI” and all its features without Pydantic.

Pydantic is required in the sense that it is listed amongst FastAPI’s required dependencies.

But, you can certainly use FastAPI without using Pydantic. You could roll your own user input validation, for instance.

Or, you could use Dataclasses, which aren’t part of Pydantic of course, though Pydantic can use them as substitutes for the Pydantic models.

1

u/AlabamaSky967 Oct 15 '21

Riiight but it just seems to me like Pydantic is a totally seperate library than FastAPI. The only thing tying them together is the fact that the FastApi guide includes so much Pydantic material and features.

I'm just trying to seperate in my head, which is FastAPI, which is Pydantic, and which (if any) is a combination of both.

3

u/hexarobi Oct 15 '21

Pydantic is a library for parsing and validating data. FastAPI uses it for the common problems of parsing and validating input/output for a web app.

1

u/Ivana_Twinkle Oct 16 '21

Data classes gets converted to pydantic models anyway so you are still dependent and need a specific use case for data classes even being worth it. https://fastapi.tiangolo.com/advanced/dataclasses/

1

u/[deleted] Oct 15 '21

Yes. FastAPI framework is built on top of starlette and pydantic, which means they are dependent. Look at the dependencies when you install it.

fastapi==0.68.1
  - pydantic [required: >=1.6.2,<2.0.0,!=1.8.1,!=1.8,!=1.7.3,!=1.7.2,!=1.7.1,!=1.7, installed: 1.8.2]
  - typing-extensions [required: >=3.7.4.3, installed: 3.10.0.0]

Edit: Formatting and dumbassery.

0

u/[deleted] Oct 16 '21

[deleted]

1

u/AlabamaSky967 Oct 16 '21

That's what I found when using it. It seems like all the features in fastapi are actually Pydantic 😅

0

u/[deleted] Oct 16 '21

[deleted]

1

u/AlabamaSky967 Oct 16 '21

Thanks for the clarification!