Modern APIs with FastAPI and Python Transcripts
Chapter: Modern language foundations
Lecture: Model validation, the Pydantic way

Login or purchase this course to watch this video and the rest of the course contents.
0:00 That was rough, wasn't it? Okay, let's take a step back towards this. So we're gonna go and use this library called pydantic.
0:08 Now pydantic is really, really neat. And what it lets us do is it lets us create simple classes. Here's a class. It has an ID
0:16 which is an integer, a name which is a string and has a default value. A signup which is an optional datetime defaulting to not set and so
0:23 on, and some external data like this, and then we create it exactly the same way. But this is all we write,
0:30 right? Well, technically, we already have the external data. This is what we write. And it behaves very, very similar to what we already saw,
0:37 which is killer. So let's go make that happen. In order to do that, we're going to need to use pydantic here, Yep. That's still not misspelled.
0:48 Go run pip install again. Get pydantic. So what we're gonna do is I'll make a copy of this. I'll call this V2, it'll be the pydantic one.
0:56 And let's get rid of this big gnarly beast here, and I'll just leave that for a minute.
1:02 Let's go create our class order, and the way we use pydantic is we derive from base model, which comes from pydantic,
1:09 And then it's very, very similar to data classes. If you've seen this, we'll have item ID and then we just specify the type.
1:15 This is gonna be a required integer field, Okay? Say created date, which is, say this is an optional,
1:25 datetime dot datetime. We just need to import that. What else do we have? We had pages visited and that was going to be
1:33 a list of int. And then we had a price which was a float. Look how nice and clean that is. Even simpler than are simple version.
1:41 And guess what? Unless I made a quick mistake here, it should run and it should do the same thing. So let's go and run "order_v2".
1:48 Check it out. It did the same thing. Item is an ID. An integer got converted. 23 created date. The datetime got converted,
1:57 pages visited. Look, even the elements in the list got converted and the price just hung in there. And we said that the datetime was optional.
2:05 So what if we omit it. Well, it's just set to none. That's fine. No big deal. We did not say that the pages was optional.
2:13 What happens if we omit that? it hates it! A required field is missing. What field? Pages visited. Super, Super cool. All the validation,
2:22 all that conversion, everything, even a little bit better than we had. It will say you know what's required and what's not.
2:29 So this is what pydantic offers for us. This really clean model of writing this that takes this external data very much like you
2:36 might receive from an API from some client sending random junky, semi-correct data, like here's a sort of a three,
2:45 but it's not a three, but it could be a three type of data over to us and just turn it into exactly what we want or a nice error message.
2:52 Like what happens if I put ABC here? Pages visited, the second index, so third item, is not an integer. How super cool is that,
3:04 and we don't have to write it. That's why it's cool. So this is exactly we want, the sort of filter we
3:09 want to go through with submitting data to our API and, a little bit of a sneak peek here,
3:16 If we have some order API call, instead of saying it takes in item ID, which is an int, created date, blah blah blah, we can just say this:
3:25 It takes an order, which is an order, and that's it. All that automatic validation and all that cool stuff I just showed
3:32 you happens before our function even gets called. It's done automatically by FastAPI, so that's super cool.
3:39 This really works by default for JSON post, and can be done for others with a slight modification. You have to like,
3:48 give it a certain, a certain statement here that says, Please go find this somewhere else,
3:53 okay? So by default, this works when some API is being called and data is being posted to it,
3:58 if you want something else as like our calculator example, we're gonna need to do something slightly different, but nonetheless, it's still super,
4:05 super cool, and I guess we could even just leave this here like that. Alright, this is pydantic. Look, let's just do a quick compare.
4:13 Here's what we do by ourselves, and this is actually less validation than what pydantic is doing because it doesn't say like,
4:19 Oh, the created date is required and stuff like that. It'll crash if it's not there, but it doesn't say that it's required explicitly.
4:26 So all of this, place down to that, that's doing even more. Pydantic derived from the base model to create those things,
4:35 kind of like you would with data class. We could even come over here and say that if you don't specify
4:40 these, we'll just do an empty list as the default. Super cool. So that's pydantic and you'll see it appear as we work with our API's at
4:48 the exchange layer, when data is being passed to us or when we return objects, we can actually return Pydantic models right out of our API methods
4:56 and they'll get converted to JSON as well.


Talk Python's Mastodon Michael Kennedy's Mastodon