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}")