Member-only story
Write your own Search Agent with Pydantic AI — Part 2
Hello everyone, this is a follow-up of my recent blog about Writing Your Own Search Agent with Pydantic AI. In the earlier post, I demonstrated how to use Pydantic AI, a new LLM Agent framework developed by the Pydantic team, to build an agent capable of searching the internet to answer questions. We used the SERP API to expose search functionality to the agent, enabling it to query Google and augment the search results with the LLM to generate final answers.
In this post, I will take it a step further by enabling the agent to visit websites from the search results and extract detailed information to answer the query. This enhancement allows the agent to handle more complex questions, as the search result snippets alone may not provide all the necessary details. If you haven’t read the first post, i would highly recommend reading it first before continue with this one.
Updating search tool
Continue from the previous implementation, the agent only received the page title and snippet from search results. So the first thing we need to ensure that the agent also gets the URL of each search result. Here’s how to define a structured type for the search results and update the search tool:
from pydantic import BaseModel
class SearchResult(BaseModel):
title: str
url: str
snippet: str
@research_agent.tool_plain
async def search_google(query: str) -> List[SearchResult]:
"""
Search the web for the given query and return…