Member-only story
FastAPI Request Validation: 10 Pydantic Tricks Most Tutorials Skip
Your API is only as safe as your validation layer.
We had an endpoint that accepted a JSON body with a price field. Type-hinted as float. Standard Pydantic model. Worked perfectly for months.
Then one day a frontend developer sent "price": "29.99" — a string, not a number. And Pydantic accepted it. Silently converted it to 29.99 and carried on. No error. No warning. The request went through.
That’s not a bug. That’s Pydantic’s default behavior. It coerces types. A string that looks like a number becomes a number. A string "true" becomes a boolean True. An integer 1 becomes a float 1.0. It's designed to be forgiving.
And for a lot of APIs, that’s fine. But for our payment endpoint, silently accepting strings where numbers should be was a ticking time bomb. We needed strict validation — and that was the day I learned that most Pydantic tutorials only scratch the surface of what’s possible.
Here are the 10 patterns I’ve collected from building production APIs with FastAPI and Pydantic V2. These are the tricks I wish someone had shown me on day one.