From 775e4b6540c231d62a7fdf4d601def0e1175164f Mon Sep 17 00:00:00 2001 From: Leodanis Pozo Ramos Date: Fri, 13 Feb 2026 15:41:53 +0100 Subject: [PATCH 1/2] Sample code for the article on Pydantic AI --- pydantic-ai/README.md | 3 +++ pydantic-ai/cats.py | 23 +++++++++++++++++ pydantic-ai/city.py | 20 +++++++++++++++ pydantic-ai/first_agent.py | 9 +++++++ pydantic-ai/users.py | 52 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 107 insertions(+) create mode 100644 pydantic-ai/README.md create mode 100644 pydantic-ai/cats.py create mode 100644 pydantic-ai/city.py create mode 100644 pydantic-ai/first_agent.py create mode 100644 pydantic-ai/users.py diff --git a/pydantic-ai/README.md b/pydantic-ai/README.md new file mode 100644 index 0000000000..1813059787 --- /dev/null +++ b/pydantic-ai/README.md @@ -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/). diff --git a/pydantic-ai/cats.py b/pydantic-ai/cats.py new file mode 100644 index 0000000000..9d0ee7d4c5 --- /dev/null +++ b/pydantic-ai/cats.py @@ -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) diff --git a/pydantic-ai/city.py b/pydantic-ai/city.py new file mode 100644 index 0000000000..1dadf3aa31 --- /dev/null +++ b/pydantic-ai/city.py @@ -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) + + +print(f"{result.output.name}, {result.output.country}") +print(f"Population: {result.output.population:,}") +print(f"Fun fact: {result.output.fun_fact}") diff --git a/pydantic-ai/first_agent.py b/pydantic-ai/first_agent.py new file mode 100644 index 0000000000..728a2a57fb --- /dev/null +++ b/pydantic-ai/first_agent.py @@ -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) diff --git a/pydantic-ai/users.py b/pydantic-ai/users.py new file mode 100644 index 0000000000..7a0efcb90a --- /dev/null +++ b/pydantic-ai/users.py @@ -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}") From 9a76310f84ca4918280a7c2ce12db5384afac99d Mon Sep 17 00:00:00 2001 From: Leodanis Pozo Ramos Date: Wed, 18 Feb 2026 13:46:35 +0100 Subject: [PATCH 2/2] TR updates, first round --- pydantic-ai/cats.py | 2 +- pydantic-ai/city.py | 2 -- pydantic-ai/first_agent.py | 2 +- pydantic-ai/users.py | 2 +- 4 files changed, 3 insertions(+), 5 deletions(-) diff --git a/pydantic-ai/cats.py b/pydantic-ai/cats.py index 9d0ee7d4c5..f19de978be 100644 --- a/pydantic-ai/cats.py +++ b/pydantic-ai/cats.py @@ -3,7 +3,7 @@ agent = Agent( "google-gla:gemini-2.5-flash", - system_prompt="Help users with cat breeds. Be concise.", + instructions="Help users with cat breeds. Be concise.", ) diff --git a/pydantic-ai/city.py b/pydantic-ai/city.py index 1dadf3aa31..7b343cb275 100644 --- a/pydantic-ai/city.py +++ b/pydantic-ai/city.py @@ -13,8 +13,6 @@ class CityInfo(BaseModel): result = agent.run_sync("Tell me about Tokyo") print(result.output) - - print(f"{result.output.name}, {result.output.country}") print(f"Population: {result.output.population:,}") print(f"Fun fact: {result.output.fun_fact}") diff --git a/pydantic-ai/first_agent.py b/pydantic-ai/first_agent.py index 728a2a57fb..32018fa766 100644 --- a/pydantic-ai/first_agent.py +++ b/pydantic-ai/first_agent.py @@ -2,7 +2,7 @@ agent = Agent( "google-gla:gemini-2.5-flash", - system_prompt="You're a Python Expert. Reply in one sentence.", + instructions="You're a Python Expert. Reply in one sentence.", ) result = agent.run_sync("What is Pydantic AI?") diff --git a/pydantic-ai/users.py b/pydantic-ai/users.py index 7a0efcb90a..81efb67c5a 100644 --- a/pydantic-ai/users.py +++ b/pydantic-ai/users.py @@ -24,7 +24,7 @@ class UserSummary(BaseModel): "google-gla:gemini-2.5-flash", output_type=UserSummary, deps_type=UserDatabase, - system_prompt=( + instructions=( "You retrieve user information from an external database. " "Use the available tools to gather user info, " "then return a structured summary."