Setting Up Pydantic AI
00:00 In order to be able to set up Pydantic AI, you’ll want to do four things. You’ll want to create a virtual environment, the environment in which you install Pydantic AI.
00:11 You’ll want to install Pydantic AI in that environment. And then you’ll want to make sure that everything is working just fine. So you’re going to set the API key you got from the prerequisites lesson, the past lesson. You’ll set it as an environment variable, and then you’ll try to run a basic prompt from the REPL, just to double-check that everything is working smoothly.
00:33 So first, you’re going to create your virtual environment and install Pydantic AI. To create a virtual environment, feel free to use whatever tool you prefer.
00:42
If you have no preference, usually you’ll just want to do something like python -m venv and then .venv. That’s the typical way if you have no preference for any tool whatsoever.
00:53
After creating the virtual environment, you’ll want to activate it with source .venv/bin/activate. And you’ll want to run the command python -m pip install pydantic-ai, all together but with a hyphen separating pydantic and ai.
01:12
And then this command will install pydantic-ai and all of its dependencies so that you can use this package.
01:20 You’re going to give it a couple of seconds. And once you’re done, you’re going to set up the API key.
01:27
Alright, so after pydantic-ai is installed, if you run Python, you could already import pydantic_ai, but you still have to set up your API key in order to be able to actually run something. So you want to make sure you’re at the root of your directory.
01:43
And if you look at the contents of this folder, you’ll see a file called .env. Now, this is a text file that contains the definition of one environment variable.
01:56
In this particular case, if you cat the file, you’ll see that it contains a single line, GOOGLE_API_KEY= and then inside quotes, the API key that’s being used for this course.
02:11
If you’re using Gemini, you’ll want your .env file to have this structure as well. GOOGLE_API_KEY= and then the key. If you’re using an API provider that’s not Google, you’ll need to check the Pydantic AI documentation to see what’s the correct name for the environment variable.
02:30 If you open the documentation page from the previous lesson that shows all of the providers that Pydantic AI supports, you can click on your provider. For example, let’s say it’s Anthropic.
02:42
And very close to the top, you will see under “Configuration”, “Environment variable”, it’s going to tell you the name, the correct name you should use for your environment variable. Now, feel free to manage the environment variable in your preferred way. If you don’t have any preference, our recommendation is that you save it in a .env file.
03:02
Make sure it’s added to your gitignore. If you’re doing this inside the GitHub repo, make sure you add it to a gitignore. And then in order to assign the environment variable in your terminal, you’ll want to run the commands set -a, source .env, and then set +a.
03:21 And the reason you put the API key inside a file is so that it’s easier to retrieve. You can just run these three commands whenever you need to activate the API key.
03:35
Once you go through these steps, you can run Python. You can, from pydantic_ai, you’ll want to import the class Agent with capital A.
03:46 And now you’re going to instantiate an agent. You want to pass in the name of the provider and the model to the agent. If you’re using Gemini, feel free to copy what’s on the screen. If you’re using a different provider, the same page that tells you the name of the environment variable, if you scroll down a little bit, you’ll see the correct way of identifying the provider. And then you need to check the provider’s documentation for the models that are available.
04:15 For Gemini, that’s going to be Gemini 2.5 Flash, a reasonable model that’s going to be fairly fast for our examples. And then you’re going to set some basic instructions. You’re going to say you’re a Python expert,
04:28
reply to all questions with a single sentence. You instantiate the agent. And now to get a single interaction with the agent, you’re going to run the method run_sync.
04:41
So you say that the result is going to be whatever you get from the agent if you pass in the question, "What is Pydantic AI?"
04:50
And you give it a second, Pydantic AI is connecting to the Gemini LLM. And once you have the result, you can print its output to read the one sentence reply from the LLM.
05:02 If this ran smoothly, it means that you’re good to go for the next lesson. We’re going to learn how to get structured output out of an LLM. If you had any issues, make sure that you correctly define the API key, double-check the name of the environment variable, double-check the value, and double-check the commands.
05:23
The order of the commands is important. You start with set -a, then you source the file, and then you set +a again to counteract the -a you started with.
05:34 Alright, once everything is good to go, I’ll see you in the next lesson.
Become a Member to join the conversation.
