Skip to main content
Wrap Moss as a 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’s load_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

1

Installation

2

Environment setup

.env
3

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.
4

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.
5

Customize retrieval behaviour

Adjust top_k and alpha to tune the retrieval:

How it works

The index is loaded once before the agent starts. All 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.