Skip to content
Pydantic AI

RAG Pipeline with Pydantic AI

Build a Retrieval-Augmented Generation pipeline that searches documents with vector embeddings and answers questions with citations — using Pydantic AI.

RAGvector storeembeddingsdocuments

Working Code

Pydantic AI
from pydantic_ai import Agent, RunContext
agent = Agent(
"openai:gpt-4o",
system_prompt="You are a document Q&A assistant. Search documents before answering. Cite sources using [1], [2] format.",
)
@agent.tool
async def search_documents(ctx: RunContext, query: str, top_k: int = 5) -> str:
"""Search uploaded documents for relevant content."""
results = await vector_store.asimilarity_search(query, k=top_k)
return "\n\n".join(
f"[{i+1}] {r.metadata.get('source', 'unknown')}: {r.page_content}"
for i, r in enumerate(results)
)
result = await agent.run("What does the refund policy say about digital products?")
print(result.output)

Step by Step

1

Install dependencies

Install Pydantic AI and the required tools for this use case.

2

Define your tools

Create the domain-specific tool functions your agent will use to interact with external services.

3

Create the agent and run

Initialize the Pydantic AI agent with your tools, set the system prompt, and execute a query.

Ready to build with Pydantic AI?

Generate a production-ready project with Pydantic AI pre-configured — FastAPI + Next.js, auth, streaming, and more.

Get Started

Ready to build your first production AI agent?

Open-source tools, battle-tested patterns, zero boilerplate. Configure your stack and ship in minutes — not months.