Skip to content
All Projects

Database Toolset for Pydantic AI

Empower AI agents with SQL database capabilities

SQL database toolset that lets AI agents query, analyze, and interact with databases. Supports schema introspection, safe query execution, and result formatting.

Installation

Terminal
pip install database-pydantic-ai

Five tools give agents full database interaction: list_tables, get_schema, describe_table, explain_query, and query. Supports both SQLite (via aiosqlite) and PostgreSQL (via asyncpg) with a unified interface. Security-first: read-only mode blocks 15+ dangerous SQL keywords including DROP, DELETE, and ALTER. Resource control with configurable query timeouts (30s default) and row limits (100 default). Comment-aware parsing detects dangerous keywords hidden behind -- and /* */ comments.

Features

SQL Query Execution
Schema Introspection
Result Formatting
Safe by Default

Quick Start

database_example.py
from pydantic_ai import Agent
from database_pydantic_ai import (
SQLiteDatabase,
SQLDatabaseDeps,
SQLITE_SYSTEM_PROMPT,
create_database_toolset,
)
async with SQLiteDatabase("data.db") as db:
deps = SQLDatabaseDeps(database=db, read_only=True)
agent = Agent(
"openai:gpt-4o",
deps_type=SQLDatabaseDeps,
toolsets=[create_database_toolset()],
system_prompt=SQLITE_SYSTEM_PROMPT,
)
result = await agent.run("What are the top 5 most expensive products?", deps=deps)

Use Cases

Data Analysis Agents

Let agents explore schemas, write queries, and analyze results from SQLite or PostgreSQL databases using natural language.

Report Generation

Build agents that query production databases, format results, and generate human-readable reports automatically.

Database Administration

Create assistants that help with schema discovery, query optimization via EXPLAIN, and database documentation.

Business Intelligence

Give non-technical users natural language access to their data with read-only safety guarantees and query timeouts.

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.