PydanticAI: the AI Agent Framework Winner
By ArjanCodes
Summary
Topics Covered
- Pydantic AI Builds Context-Aware Agents
- Define Dependencies and Structured Outputs
- Inject Patient Data via Dynamic Prompts
- Tools Enable Domain-Aware Decisions
- Scale from Single Agents to Graphs
Full Transcript
This video is brought to you by Squarespace. What if you could integrate
Squarespace. What if you could integrate GPT4 right into your Python app and make it aware of your business logic, your data, and your models? Today, I'll show
you exactly how to do this with Pantic AI, a really nice toolkit that connects AI with real Python code. And don't
worry, it's not just theoretical. I'll
walk you through an actual example.
Let's dive in. First, what is Pyantic AI? It's basically an extension of
AI? It's basically an extension of paidantic that helps you build AI powered agents. These agents can use
powered agents. These agents can use your domain data models. They can access dependencies like databases or APIs and they can return structured outputs with
validation. Thus, the Pantic extension.
validation. Thus, the Pantic extension.
So, an agent in that sense is an LLM that's aware of your business context.
And Pentic AI integrates with popular models including GPT4. Why is this helpful? Well, if you're just using a
helpful? Well, if you're just using a chatbot directly, then you're going to get unstructured text back, which is going to be hard to deal with in production systems. And what Pedantici
adds is that you have type safe outputs that are validated by Pantic. You have a clean way to inject uh contextual dependencies like a database for example, and you also have the ability
to define tools, custom functions that the agent can call. And this helps you build AI systems that go beyond just being simple chat bots. So let's take a look at a concrete example in
healthcare. That being said, I'm not a
healthcare. That being said, I'm not a doctor. I don't recommend you actually
doctor. I don't recommend you actually start using this in your hospital, but theoretically this is possible. Let's
say you're building a triage system that helps nurses respond to patient questions. So here is the starting point
questions. So here is the starting point of this example. It doesn't use any AI.
So as you can see, I'm using async.io because well why not it's helpful if we're sending requests to an API I'm using a data class to uh represent a mock database I'm importing environment
variables from aend file in this case this contains my open AAI API key I'm not going to show you that file but then I have a simple mock database very basic
it has a patient class using a data class for that one a patient has an ID a name and vital which is a dictionary of strings to whatever. I'm not saying that
this is how you should model patients but it's a simple example. Then I have my database, which is nothing more than a dictionary that has two patients with
uh each an ID, a name, and some vitals.
Then I have a helper class that helps me access some of that data. So here, given an ID, this is going to give me the name of the patient, and this method gives
the latest vital of a patient by ID.
Extremely basic, of course. If you have an actual database, you would fill this with way more information. You'd have
way more capabilities here. It's just a basic example. And the only thing I have
basic example. And the only thing I have now is a main function that creates a database connection and gets a patient name and vitals and then prints that.
So when I run this, then this is what we get. Very basic, right? So now as the
get. Very basic, right? So now as the first step, let's set up a bit of scaffolding for our agent. One thing
that we're going to need is some class that represents the things that the the agent depends on. Let's call that triage dependencies.
And for simplicity, I'm going to use a data platform.
And what we're going to need is a patient ID. And we're also going to need
patient ID. And we're also going to need a database connection, right? So that
for any patient, we can access then the database and do something with it. So
that's what the agent is going to be dependent on. Next, we're also going to
dependent on. Next, we're also going to need to decide what the output of the agent should look like. So that's going to be triage output. Not sure why it's
indented here. And since we're using
indented here. And since we're using Pyantic AI, this is where we should rely on Pyantics base model.
And of course, we're going to need to import that. So what the output is going
import that. So what the output is going to be actually AI already sort of indicates that here is that we're going to have a response text from the chat
agent. We also need a boolean indicating
agent. We also need a boolean indicating whether or not this should be escalated and we're going to need to indicate the urgency as a number between 1 and 10. So
these are the three things that we are going to need and we're going to use a field to give a bit more information about this. So in this case, the
about this. So in this case, the response text is going to be a field with a description and that's the response text for the triage. And maybe we can actually write
triage. And maybe we can actually write that a bit clearer by saying this is a message to the actual patient like so. And then we can do the same
like so. And then we can do the same thing for escalate and for urgency. So
whether we should escalate this case and what the urgency level of this case is to a human nurse. Don't forget about the humans. AI. So now we have created our
humans. AI. So now we have created our sort of context for the agent. So this
is what the agent depends on which is the patient ID and a reference to a database connection and we defined what the agent should output response text whether to escalate and what the urgency
is. Now before I show you how to create
is. Now before I show you how to create an agent, first want to take a minute to talk about today's sponsor, Squarespace.
Squarespace is an all-in-one website platform designed to help you stand out and succeed online. Whether you're just starting out or scaling your business it gives you everything you need to claim your domain, build a professional
site, grow your brand, and get paid all in one place. I've used Squarespace myself to launch websites for my businesses. And as someone who builds
businesses. And as someone who builds software and teaches software design, I really appreciate tools that are thoughtfully designed and just work.
It's really easy to get started by using Blueprint AI, which generates a fully custom website based on just a few prompts. There are a ton of templates
prompts. There are a ton of templates that all look really good. After that
it's trivial to add sections or change the appearance to your liking and boom you have a full professional website.
Squarespace comes with integrated SEO tools, so you don't need to worry about optimizing meta descriptions or generating site maps. It handles all of that automatically. If you ever try to
that automatically. If you ever try to get a new dev blog or documentation site index properly, you know how frustrating that can be. With Squarespace, it's
baked in. And with the built-in
baked in. And with the built-in analytics dashboard, you can track traffic, engagement, and even revenue if you're selling something, or you can use to figure out what landing pages are
working and what needs to be improved.
Whether you're building a site to showcase your portfolio or launch a software as a service that you just vioded, Squarespace makes it incredibly easy. Head over to
easy. Head over to squarespace.com/ircodes
squarespace.com/ircodes for a free trial. And when you're ready to launch, use offer code iron codes to save 10% off your first purchase of a
website or domain. Now, back to the video. Now, next thing that we're going
video. Now, next thing that we're going to do is define an agent that uses GPT4 that takes our dependencies and then returns a structured output. And we also
need to supply it with a system prompt that explains the role of this particular agent, which is a triage assistant. So I'm going to create a
assistant. So I'm going to create a triage agent which is an agent and that is actually something that we're going to need to
import from paidantic AI.
going to import the agent and then the agent is going to use the open AI GPT 40 model.
We're going to need to supply the dependencies which is triage dependencies. We're going to need to
dependencies. We're going to need to specify the output type which is triage output and we're going to supply a system prompt.
You are a triage assistant helping patients.
I still can't type there.
provide clear advice and assess urgency. There
we go. That's our system prompts.
I'm missing closing parenthesis. Oh
yeah. Wow. Copilot even corrects my spelling. Very helpful. Okay. So, this
spelling. Very helpful. Okay. So, this
is my agent. And now what you can do let's change this main function here, is we can create the dependencies. So
we're going to do that for patient 42.
And we have a database connection. And
then we're going to get a result from the triage agent.
And we can then specify a prompt supply dependencies. There we go.
dependencies. There we go.
And then we can print the result.put.
Something went wrong here with the parenthesis. I know if you also have
parenthesis. I know if you also have that problem if you have these kind of editing AI kind of tools that they always screw up my parenthesis for some reason. Not sure why that happens. Okay
reason. Not sure why that happens. Okay
so now we're actually running the agent.
And let's see what happens if we run the file again. So now you see we get back
file again. So now you see we get back an object with a response text. We have
escalate is false, urgency is two, which is probably right in this particular case. Seems like it's not very urgent
case. Seems like it's not very urgent right? But the thing is, of course
right? But the thing is, of course that's it's not really using any of the patient information. Right? Now, before
patient information. Right? Now, before
I show you how to make this more patient dependent, if you're enjoying this video, give it a like and subscribe to the channel. This is a small thing, but
the channel. This is a small thing, but really helps and lets me keep making these types of videos. Now let's say we want to personalize this more to the
patient instead of this very generic uh thing that we get here as a response. So
what you can do then with the pyantic AI agents is you can add things like for example you can add a system prompt
triage agent dot system prompt. So I'm
going to use the system prompt decorator and then we're going to add the patient name and prompts in pantic AI they're
going to get a context. So in this case that is a run context which well we need to import that from a pyantic AI
like so. And run context is a generic
like so. And run context is a generic type that gets dependencies like so.
And this is going to return a name because we want the patient name of course.
So then we get the patient name from the database providing the patient ID and then we can do is simply write the patient name.
Patient name is like so. Now this is going to add this particular system prompt to our agent which is nice. And
then what we can do in the agent for example is to also indicate always mention the patient name when available like so.
So it's more personalized. So we're
going to get generic advice but at least it's going to be uh personalized to you.
So when I run this let's see what the response text is.
There you go. John for your symptoms etc etc. So now it's personalized to the patient name because it uses the uh system prompt and adds it to my agent.
So that's a really helpful tool. You can
of course add more of these prompts if that's helpful and get uh useful information from the dependencies. Now
instead of just getting data, you can also add tools. For example, calling a function that does something. So in this case, I'm going to keep it really simple
and I'm just going to add a tool to get the latest vitals for a patient. Now
this is simply a database request, but you can also imagine that the tool is actually, I, don't know, augmenting, some patient information in a database or or do basically whatever you want. But here
I'm just going to get the latest vitals.
So now when I run this again it's going to take the latest vitals into account when creating the response.
So as you can see, it now mentions the vital signs and the blood pressure.
And if I change these things, so in this case, we're talking about John. Let's
say I'm going to give John a heart rate of 30. It's not very high. and also like
of 30. It's not very high. and also like a very low blood pressure like so not sure if this is even realistic but just
to see how it influences the result. So
now you see that whereas in the previous responses we got uh escalate is false right we got that here and we got a pretty low urgency now even though the
patient is saying the same thing it uses the information to actually change these values so now escalate is true so a nurse should take a look at this and the urgency is also higher so what's really
cool about this is that you can combine LM reasoning with your domain knowledge the outputs you get are validated by Pyantic and they're structured so you
can trust them and use them in other parts of your application. You can
extend this agent by adding more tools or prompts or anything you like. And in
my opinion, this is a big step in incorporating AI in your Python applications in a more useful way. Now
predentic AI's other possibilities as well. on particular graphs are kind of
well. on particular graphs are kind of interesting. Although they say in the
interesting. Although they say in the documentation themselves that well don't use it if you don't really need it because of course graphs can potentially complicate things. But they do mention
complicate things. But they do mention different ways of organizing your agent workflows. So the one that I showed you
workflows. So the one that I showed you today was a single agent workflow.
That's also what most of the documentation shows. But you can also
documentation shows. But you can also combine agents. For example, you could
combine agents. For example, you could have one agent that then defines a tool that uses another agent. You can have an agent run something and then the
application code could do something with the data or call another agent. Or you
could go full on graph uh based control and then you can define these graphs with nodes and edges and make like a really complicated setup. But only do
that if it actually uh adds something to your application. If you can use one of
your application. If you can use one of the simpler methods I mentioned just now, I think that overall is better. But
I'd like to know what you think. Do you
use pideantic AI? Could you see yourself using it in your own projects? Maybe in
finance, healthcare, or something completely different. Let me know in the
completely different. Let me know in the comments. Now, there are, of course
comments. Now, there are, of course next to Pyantic AI a bunch of other libraries that are incredibly helpful to building stuff in Python. You probably
already know quite a few of them like pandas or fast API. Now, if you want to learn about more libraries that are really helpful, but only few developers know about, check out this video next.
Loading video analysis...