pydantic_ai.Tool and pass it directly to any Pydantic AI agent. The agent will call moss_search automatically whenever it needs to look up facts from your knowledge base, keeping all answers grounded in indexed content.
Full example — see the Pydantic AI cookbook for the complete MossSearchTool class, demo script, and unit tests.
Why use Moss with Pydantic AI?
Pydantic AI’s tool system infers the input schema directly from the function signature, so there’s no schema boilerplate to maintain. Moss’sload_index() pulls the index into local memory before the agent starts, meaning every tool call during the agent’s run hits the ~1–10ms in-memory path rather than a remote API.
Required tools
- Moss account with project credentials
- OpenAI API key (or any Pydantic AI-supported LLM)
- Python 3.11+
Integration guide
Create MossSearchTool
MossSearchTool wraps a MossClient and exposes a .tool property that returns a ready-to-use pydantic_ai.Tool. Pydantic AI derives the input schema from the inner function’s type annotations automatically.Load the index and run the agent
Call
load_index() before agent.run(). On the first run this may download the local query model cache — subsequent runs start immediately.How it works
moss_search calls during the agent’s execution hit the in-memory path, so retrieval doesn’t add meaningful latency to tool-call round trips.