Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pydantic-ai/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Pydantic AI: Build Type-Safe LLM Agents in Python

This folder provides the code examples for the Real Python tutorial [Pydantic AI: Build Type-Safe LLM Agents in Python](https://realpython.com/pydantic-ai/).
23 changes: 23 additions & 0 deletions pydantic-ai/cats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import requests
from pydantic_ai import Agent

agent = Agent(
"google-gla:gemini-2.5-flash",
system_prompt="Help users with cat breeds. Be concise.",
)


@agent.tool_plain
def find_breed_info(breed_name: str) -> dict:
"""Find information about a cat breed."""
response = requests.get("https://api.thecatapi.com/v1/breeds")
response.raise_for_status()
json_response = response.json()
for breed in json_response:
if breed["name"] == breed_name:
return breed
return {"error": "Breed not found"}


result = agent.run_sync("Tell me about the Siamese cats.")
print(result.output)
20 changes: 20 additions & 0 deletions pydantic-ai/city.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from pydantic import BaseModel
from pydantic_ai import Agent


class CityInfo(BaseModel):
name: str
country: str
population: int
fun_fact: str


agent = Agent("google-gla:gemini-2.5-flash", output_type=CityInfo)

result = agent.run_sync("Tell me about Tokyo")
print(result.output)


Copy link
Contributor

@martin-martin martin-martin Feb 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these newlines don't really make sense here between the print() calls:

Suggested change

print(f"{result.output.name}, {result.output.country}")
print(f"Population: {result.output.population:,}")
print(f"Fun fact: {result.output.fun_fact}")
9 changes: 9 additions & 0 deletions pydantic-ai/first_agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from pydantic_ai import Agent

agent = Agent(
"google-gla:gemini-2.5-flash",
system_prompt="You're a Python Expert. Reply in one sentence.",
)

result = agent.run_sync("What is Pydantic AI?")
print(result.output)
52 changes: 52 additions & 0 deletions pydantic-ai/users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import requests
from pydantic import BaseModel
from pydantic_ai import Agent, RunContext


class UserDatabase:
"""Simulate a user database using the JSONPlaceholder users API."""

_base_url = "https://jsonplaceholder.typicode.com"

def get_user_info(self, user_id: int) -> dict:
response = requests.get(f"{self._base_url}/users/{user_id}")
response.raise_for_status()
return response.json()


class UserSummary(BaseModel):
name: str
email: str
company: str


agent = Agent(
"google-gla:gemini-2.5-flash",
output_type=UserSummary,
deps_type=UserDatabase,
system_prompt=(
"You retrieve user information from an external database. "
"Use the available tools to gather user info, "
"then return a structured summary."
),
)


@agent.tool
def fetch_user(ctx: RunContext[UserDatabase], user_id: int) -> str:
"""Fetch user profile from the service."""
try:
user = ctx.deps.get_user_info(user_id)
return str(user)
except requests.HTTPError:
return f"User with ID {user_id} not found"


db = UserDatabase()
result = agent.run_sync(
"Get a summary for user 7",
deps=db,
) # Inject the database
print(f"Name: {result.output.name}")
print(f"Email: {result.output.email}")
print(f"Company: {result.output.company}")