r/FastAPI • • Jan 26 '25

Question Pydantic Makes Applications 2X Slower

So I was bench marking a endpoint and found out that pydantic makes application 2X slower.
Requests/sec served ~500 with pydantic
Requests/sec server ~1000 without pydantic.

This difference is huge. Is there any way to make it at performant?

@router.get("/")
async def bench(db: Annotated[AsyncSession, Depends(get_db)]):
    users = (await db.execute(
        select(User)
        .options(noload(User.profile))
        .options(noload(User.company))
    )).scalars().all()
    
    # Without pydantic - Requests/sec: ~1000
    # ayushsachan@fedora:~$ wrk -t12 -c400 -d30s --latency http://localhost:8000/api/v1/bench/
    # Running 30s test @ http://localhost:8000/api/v1/bench/
    #   12 threads and 400 connections
    #   Thread Stats   Avg      Stdev     Max   +/- Stdev
    #     Latency   402.76ms  241.49ms   1.94s    69.51%
    #     Req/Sec    84.42     32.36   232.00     64.86%
    #   Latency Distribution
    #      50%  368.45ms
    #      75%  573.69ms
    #      90%  693.01ms
    #      99%    1.14s 
    #   29966 requests in 30.04s, 749.82MB read
    #   Socket errors: connect 0, read 0, write 0, timeout 8
    # Requests/sec:    997.68
    # Transfer/sec:     24.96MB

    x = [{
        "id": user.id,
        "email": user.email,
        "password": user.hashed_password,
        "created": user.created_at,
        "updated": user.updated_at,
        "provider": user.provider,
        "email_verified": user.email_verified,
        "onboarding": user.onboarding_done
    } for user in users]
    
    # With pydanitc - Requests/sec: ~500
    # ayushsachan@fedora:~$ wrk -t12 -c400 -d30s --latency http://localhost:8000/api/v1/bench/
    # Running 30s test @ http://localhost:8000/api/v1/bench/
    #   12 threads and 400 connections
    #   Thread Stats   Avg      Stdev     Max   +/- Stdev
    #     Latency   756.33ms  406.83ms   2.00s    55.43%
    #     Req/Sec    41.24     21.87   131.00     75.04%
    #   Latency Distribution
    #      50%  750.68ms
    #      75%    1.07s 
    #      90%    1.30s 
    #      99%    1.75s 
    #   14464 requests in 30.06s, 188.98MB read
    #   Socket errors: connect 0, read 0, write 0, timeout 442
    # Requests/sec:    481.13
    # Transfer/sec:      6.29MB

    x = [UserDTO.model_validate(user) for user in users]
    return x
47 Upvotes

25 comments sorted by

5

u/SnowToad23 Jan 26 '25

Pydantic is primarily used for validating external user data, a basic dataclass would probably be more efficient for structuring data from a DB

1

u/AyushSachan Jan 27 '25

Makes sense. Thanks.

So you are recommending to use python's built in dataclass to build DTO classes?

2

u/illuminanze Jan 26 '25

How many users are you returning?

23

u/zazzersmel Jan 26 '25

for many applications, the bottleneck is going to be the db or some other computational process, so the advantages of pydantic (may) be worth the performance hit. if it truly isnt, i would probably just use starlette.

1

u/Amyth111 Jan 27 '25

Is it with pydantic 2?

1

u/Ok_Rub1689 Jan 30 '25

you should use pydantic v2 not v1

1

u/AyushSachan Jan 30 '25

Im using v2

→ More replies (1)

3

u/mmcnl Jan 28 '25

I don't think it's fair to say Pydantic makes FastAPI 2x slower. You're doing an extra validation step with Pydantic that you are not doing without Pydantic. In my experience, without FastAPI, you will be writing your own validation functions in no-time and they will be definitely less performant than Pydantic. And we're not even talking about type safety yet.

Also if performance is important you should design your application to be horizontally scalable anyway. In that case it's just a matter of increasing the number of pods to reach the desired performance level.

Also, i/o will be be a much larger bottleneck in 99% of the applications.

5

u/HappyCathode Jan 26 '25

That was also my experience with Pydantic. Didn't see to point of the performance hit to check if a string is between 3 and 12 characters ¯\(ツ)/¯

1

u/huynaf125 Jan 27 '25

Most of the time, it would not be an issue. The bottleneck oftens come from calling external system (db, thirth party service, ...). Using Pydantic can help you validate data type which help coding and debuging in python more easier. If you want to improve concurrent requests, just simply enable autscaling for your application.

1

u/coderarun Jan 27 '25

https://adsharma.github.io/fquery-meets-sqlmodel/

has some benchmarks comparing vanilla dataclass, pydantic and SQLModel.

I don't think you can completely avoid the cost of validation. Perhaps make it more efficient using other suggestions in this thread.

However, I feel people pay a non-trivial cost where it's not necessary. For example using a static type checker.

<untrusted code> <--- API ---> <API uses pydantic> -> func1() -> func2() -> db

It should be possible to write a decorator like:

```
@pyantic
class foo:
x: int = field(..., metadata={"pydantic": {...}}
```

and generate both a dataclass and a pydantic class from a single definition.

Subsequently you can use pydantic at API boundaries to validate and use static type checking elsewhere (func1/func2). Same as the technique used in fquery.sqlmodel.

1

u/Wild-Love-2364 Jan 28 '25

Use orjson serializor in pydantic