Foundations · 8:35
Introduction and type annotations
TypedDict, Union, Optional, Annotated.
- LangGraph builds stateful AI workflows.
- TypedDict defines state.
- Union, Optional, Any, Annotated, Sequence.
| Annotation | Use |
|---|---|
| TypedDict | State schema |
| Optional | May be None |
| Annotated | Extra metadata |
Do this in your own tools, step by step. Ticks are saved on this device.
State schema?
TypedDict.
Metadata?
Annotated.
0:00 By the end, you'll be equipped to build robust, scalable, conversational applications that leverage the full potential of large language models. Hey guys, my name is Vava and I'm a robotics and AI student. In this course, we're going to be learning all about the fundamentals of Langraph. Now, I assume you've heard of Langraph before, hence why you clicked on this course. But I'm also going to assume you have never coded in Langraph before. Now, because of this assumption, I have explained every single thing in as much detail as I possibly can.
0:46 Now, also this might mean that I might be going slow at times. So if you want you can always speed me up. Now what are we going to be learning in this course? Well to start we're going to be building a lot of graphs, a lot of AI agents. We're going to be learning a lot about the theory and I've also provided exercises throughout the course in which all of the answers will be provided on the GitHub.
1:09 With that being said, if you're ready to start on this journey with me, let's go to our first section then. All right people. So welcome to the first section of this course. Now in this section we'll be covering something called as type annotations. Now admittedly this is going to be a completely theoretical section but it will be short and brief. I promise. The reason I've kept this specific section in the course is because when we do eventually go on to code uh our AI agents, our graphs and langraph, these will start popping up everywhere.
1:32 And I don't really want you to look start coding without ever having seen these before or really not knowing what these actually are. So that's why I've kept it here. But I promise this will be short and brief. Cool. Okay. Let's begin with dictionaries. Now dictionaries are a data structure. Yes, but there's a reason I've kept it here. So let's see how a dictionary is described in Python. You should already know this. So in this case, I've described a very simple dictionary called uh movie.
1:55 and it has two keys, the name and the year. And it has two values, Avengers Endgame and 2019. Now, dictionaries are awesome, don't get me wrong. They're they allow for efficient data retrieval based on their unique keys. They're flexible and easy to implement, but there's a potential problem with them. See, it's a challenge to ensure that the data is a particular structure. And this could be a huge problem in larger projects. So to put things in simple words, it doesn't really check if the data is the correct type data type or structure and that could be the source of a lot of logical errors in your project.
2:43 And if your project is really really large, then this could be quite a headache to identify, right? Cuz it's quite a small detail. So what is the solution for this? Well, it's something called a type dictionary. Now, here is an example on how you create a type dictionary in Python. And I just want to uh emphasize that this type annotation is used extensively in langraph. This will be used to define states. Now don't worry you we haven't covered states yet.
3:06 We will cover that in the next section. But just be mindful that this is quite important. So a type dictionary is quite easy to implement. You implement it as a class. In this case, I've implemented the same um example I uh uh showed you in the previous section where I described the movie is the same exact keys and values. So, it still has the name and the year. But notice in this class, I have defined the actual uh data type of what that key should be.
3:28 So, for example, the name is a string and the year is an integer, right? And to initialize a dictionary uh I have done the exact same thing. to have engineet game in 2019. So now there are two main uh benefits of using a type dictionary it's type safety because we've explicitly defined what should be in this data structure and so this will really reduce the runtime errors and obviously the readability is enhanced as well and this will make debugging easier if something goes wrong within this type dictionary.
4:16 Cool. So we've covered type dictionary now. Now we move on to another type of annotation which is union. Now you might have seen these future these later uh types annotations before if you coded in Python but again I'm just giving you a highle overview what these are. So union take a look at this example. So I've created a very simple function which takes in a value and it squares it. Now in this case the uh input x could be either an integer or float and union basically says that whatever value you have can be these data types only.
4:40 So in this case x can only be integer or float. So if I pass in five or 1.23 4 this would be completely fine. It would square the number and everything. But if I passed in a string like I am a string it would completely fail. Now admittedly yes this function is quite easy. If I passed in I'm a string, it would have failed anyway. But in more complicated applications, hopefully you can see how this actually is useful.
5:02 In fact, the makers of Lang Chain and Langraph used Union quite extensively throughout u making the actual library. So again, it's flexible and it's easy to code and it allows for type safety. So because it can provide hints to uh help catch incorrect usage. Now something similar to union is another type annotation which is optional. Now optional is quite similar and in this case I've described another function nice message. So you pass in a name. If you pass in a name it will say hi there name.
5:51 So for example let the name be Bob. If I pass in Bob to this uh function it would say hi there Bob. But what if I don't pass in anything? Now if I don't pass anything optional because I've used optional says that the name parameter could either be a string or a none value. Now if I pass in nothing it will go in this if statement and say hey random person. But this is also important to emphasize that it cannot be anything else.
6:13 It can't be an integer or or a boolean or a float or anything like that. It has to be either a string or a none value because that's what I've defined here. Cool. Now comes another type annotation called any. And any is really the easiest one to understand. It literally means this value could be anything. It could be any data structure. So in this case I've created a simple um function called print value where it takes in something and it prints that.
6:38 And for example I passed in this string and it prints it and anything and everything is allowed. Cool. one last type annotation I promise and it's the lambda function. So lambda functions are quite useful. For example, in this I'll give you two examples now. So the first example is this really simple uh example. Now we've already I already created a square function before, right? Where it takes in a value, it takes in a number and it squares it.
7:03 So for example, if I passed in square 10, it would give me 100. Quite an easy example. Now let me give you a second example. this. So if you've come from a leaf code background, then you've probably seen you've either used lambda before and you've definitely used math before cuz it's quite efficient. So for example, if I pass in 1 2 3 4, what this piece of code is saying is that it squares each number in nums.
7:25 So this map function maps each value uh and performs this function to it. So x * x. So 1 4 9 16 and then converts that back into a list. Now lambda functions really are just a shortcut to writing small functions and they make everything quite efficient. Now obviously this could have been done in one line as well but for example this a beginner programmer could have might have used a for loop but a more advanced programmer could have used this and this is obviously much more efficient.
8:12 Right? So hopefully you can start to see what how powerful these type annotations are and these will be coming up. So again, no need to memorize this. Just need to have a highle overview what they are. Okay, cool. So now I'll see you in the next section. See you there. All right, perfect. So let's
Get the other 14 chapters, the exam and your certificate for LinkedIn, plus all 700+ AI courses.