Setup · 5:11
Intro and environment setup
What Pydantic AI is and how to install it with your API keys.
- Pydantic AI is an agentic Python framework like LangChain or LlamaIndex.
- Install the package and set API keys in the environment.
- A notebook works for experimenting.
| Framework | Style |
|---|---|
| Pydantic AI | Type-safe, Pythonic |
| LangChain | Broad ecosystem |
| LlamaIndex | Data/RAG focus |
Do this in your own tools, step by step. Ticks are saved on this device.
What is Pydantic AI?
A type-safe agent framework for Python.
Where do API keys go?
Environment variables.
0:00 So LangChain is the more generalistic, end-to-end framework that is a little bit like Django, you could say, a lot of batteries included, opinionated. Then Llama Index is more focused on retrieval, and Pydantic, which we're going to cover today, is more focused on validation, type safety, structured output, and in general, it's more minimalistic and flexible, a little bit like FastAPI. We're going to cover quite a bit today, so if you like this video, let me know by hitting the like button and subscribing.
0:20 But now, let us get right into it. All right, so Pydantic AI is what this crash course is about. It's another one of those GenAI agent framework, similar to LangChain and Llama Index, as I already mentioned in the introduction. And you can see they say here, it's the Pydantic way. So for those of you who don't know what Pydantic is, it's basically a Python package used for validation, in a lot of different contexts. One of them is FastAPI, but also Pydantic is being used by many of the other agent frameworks.
1:00 It's used by LangChain and it's also used by Llama Index. And this is also one of their arguments here when you go down to why use Pydantic AI, it basically says here, why use the derivative when you can go straight to the source? But on a more serious note, the actual reason to use Pydantic AI is to some degree a matter of choice. So some people just like the FastAPI style of building these agentic applications. A lot of people in the community think that Pydantic AI is just much more, uh, much cleaner than LangChain, in terms of how the code is written and in terms of how you write code using it.
1:20 Uh, but also it has a different focus. It's very focused on being production ready and type safe and validating stuff and doing structured output. So that is the focus here, similar to how Llama Index is focused on retrieval. You can use all of these frameworks for basically everything, but they have certain strengths to them. And this is the focus or the key selling point here of Pydantic AI, I would say. So enough of the theory, let us get started with the setup.
1:40 With this, open up a terminal, navigate to the directory that you want to be working in. In my case, the tutorial directory, and here you can set up a virtual environment. You can also just use your system installation if you want to. So the simplest way to do that is to just say pip or pip three install, the packages that we're going to use today. I personally always prefer to use UV, so I'm going to initialize a UV project with UV init and I'm going to say UV add instead of pip install.
2:00 So whatever I say here after UV add, you can just say after pip install and it has the same effect, just on your system installation. So what we're going to need here is Pydantic-AI, but we're also going to need Python.env. And one more thing that we're going to need is also Jupiter Lab. This is the environment that we're going to be working in today. This is optional, you can also just work in ordinary Python files. I just prefer Jupiter notebooks when we learn something, because we can interactively run, uh, cells separately.
2:20 We don't have to run full Python scripts. So install these dependencies and then we can do Jupiter Lab in order to start Jupiter Lab. And if you're using UV like me, you have to do UV run Jupiter Lab. So this is just going to open up the development environment. Um, as always, when I do tutorials in Jupiter Lab, I briefly explain what the environment is about. So here, we don't work with Python files, we work with interactive Python notebooks.
2:40 Uh, it sounds more fancy than it is. We just have individual cells of code that we can run. So I can say A equals 10 and I can say print A down below. And then I can also do multiple print A's, and I don't have to run the entire code from top to bottom. I can run individual cells and I can also run them in different orders. That just makes it easier to interactively work with stuff, um, as you're exploring a package or a dataset or whatever you're doing.
3:00 Now, before we start with the coding, one more thing needs to be set up. You need to have a dot env file containing your API key of the provider that you want to use. You need to use some large language model, obviously, to power your agent with intelligence. So you want to go into your directory that you're working in. And in my case, now I'm going to copy this from my prepared code. So I have a YouTube preparation, uh, library here.
3:20 I have a Pydantic directory, and I have a dot env file in here. Now, I'm not going to show my dot env file because this dot env file contains the API key. But what you want to be doing is you want to create a file called dot env. So just a dot symbol, env. And in this file, you want to have, uh, an API key. I'm going to say test here to show you what this looks like.
3:40 That's just going to be the OpenAI API underscore key, for example, if it's the OpenAI key, otherwise, Anthropic or whatever you're using. And then whatever the key is. So you just put the key here, and then you save the file like this. That's basically it. That is the dot env file, and it's going to be relevant. that is going to automatically be recognized by Pythantic if you try to use an OpenAI model and it's going to automatically authenticate yourself with the OpenAI API.
Get the other 13 chapters, the exam and your certificate for LinkedIn, plus all 700+ AI courses.