The Validation Tax You Didn't Know You Were Paying
Adding Pydantic to a FastAPI endpoint slowed request handling by 7x in my tests. Not on some contrived benchmark — on a real API that was humming along with standard library dataclasses until I decided "proper validation" was worth the migration.
The promise was simple: swap @dataclass for Pydantic's BaseModel, get runtime type checking, automatic docs, and JSON serialization out of the box. What I got was a 120ms response time jumping to 840ms under load. That's the kind of regression that makes you question every "best practice" tutorial you've ever read.
But here's the nuance most comparisons miss: for 80% of internal services, you don't need Pydantic's validation overhead. For the other 20% — user-facing APIs, third-party integrations, anything touching untrusted input — the slowdown is the entire point. You're trading CPU cycles for safety. The question is knowing which bucket your code falls into.
Why Standard Dataclasses Are Fast (And Dumb)
Continue reading the full article on TildAlice

Top comments (0)