Python · pydantic-ai
Pydantic AI + SerpKite
Register a typed SerpKite search function with @agent.tool_plain, or attach the SerpKite MCP server as a toolset. Pydantic AI validates the arguments; SerpKite returns Markdown.
Overview
What Pydantic AI is, and where SerpKite fits
Pydantic AI is a type-safe agent framework from the Pydantic team. Tools are ordinary functions registered with @agent.tool_plain (or @agent.tool when you need run context); argument types and docstrings become the tool schema.
Pair it with structured output: search with SerpKite, then have the agent return a validated Pydantic model.
Setup
Set it up in 4 steps
-
1
Get an API key
Sign up (no card), create a key in the dashboard and export it as
SERPKITE_API_KEY. New accounts get 2,500 free credits, then 1,000 every month.export SERPKITE_API_KEY=skt_live_… -
2
Install
Set your model provider's key too (the example uses OpenAI).
pip install -U serpkite pydantic-ai -
3
Register the tool
Decorate a function with
@agent.tool_plain. -
4
Run with typed output
output_typemakes the agent return a validated model.
Code
Complete example
from serpkite import SerpKite, SerpKiteError
sk = SerpKite() # reads SERPKITE_API_KEY
def serpkite_search(query: str, country: str = "us", num: int = 10) -> str:
"""Google search via SerpKite. Returns the results page as compact Markdown."""
try:
# format="markdown" returns a str: results, People Also Ask, related searches
return sk.search(query, country=country, num=num, format="markdown")
except SerpKiteError as e:
return f"Search failed: {e.message}"
from pydantic import BaseModel
from pydantic_ai import Agent
class Answer(BaseModel):
answer: str
sources: list[str]
agent = Agent(
"openai:gpt-5-mini",
output_type=Answer,
instructions="Search the web before answering. Put the URLs you used in sources.",
)
@agent.tool_plain
def google_search(query: str, country: str = "us") -> str:
"""Search Google for current information. Returns the top results with links, People Also Ask and related searches as Markdown. Use for recent events, facts and anything after your training cutoff."""
return serpkite_search(query, country)
result = agent.run_sync("When is the next total solar eclipse visible from Europe?")
print(result.output.answer)
print(result.output.sources)AI assistants
Ask ChatGPT, Gemini and other AI assistants
The serpkite SDK asks ChatGPT, Gemini, Copilot Search, Google AI Mode and Brave's AI answer as a logged-out visitor (sk.chatgpt, sk.gemini, sk.copilot, sk.ai_mode, sk.brave_ai) and returns the answer as Markdown with the sources it cited. Register one with @agent.tool_plain. Give the agent a second opinion, or have it check what assistants say about your brand: track adds mentions of the brands and domains you list. See AI assistants.
Through the MCP server the same assistants are the chatgpt, gemini, copilot, ai_mode and brave_ai tools.
ChatGPT 4, Gemini 3, Copilot Search 3, Google AI Mode 2 and Brave AI 2 credits per answer; failed, refused and empty answers are free. Brave answers in 1–10 s, ChatGPT in 10–30 s and Gemini in 5–30 s, so allow at least 120 seconds per call.
from pydantic_ai import Agent
from serpkite import SerpKite, SerpKiteError
sk = SerpKite() # reads SERPKITE_API_KEY
agent = Agent("openai:gpt-5-mini", instructions="Compare what AI assistants answer. Cite sources.")
@agent.tool_plain
def ask_brave(prompt: str) -> str:
"""Ask Brave Search's AI answer and return it as Markdown with its cited sources
and the searches it ran. Takes 1–10 s."""
try:
return sk.brave_ai(prompt=prompt, format="markdown")
except SerpKiteError as e:
return f"Brave AI failed: {e.message}"FAQ
Pydantic AI and SerpKite: common questions
Keep exploring
Related APIs and integrations
Start building
Give your Pydantic AI project Google search
2,500 free credits, then 1,000 every month. No credit card.