API Reference¶
This section provides detailed API documentation for the database-pydantic-ai package.
Core Toolset¶
database_pydantic_ai.sql.toolset
¶
PydanticAI toolset for AI agents used to inference with database on given permission level
SQLDatabaseDeps
¶
Bases: BaseModel
Dependencies for the SQL database toolset.
Attributes:
| Name | Type | Description |
|---|---|---|
database |
Annotated[SQLDatabaseProtocol, SkipValidation]
|
Database backend instance. |
read_only |
bool
|
Enforce read-only mode (blocks INSERT, UPDATE, DELETE etc.) |
max_rows |
int
|
Maximum rows to return from queries. |
query_timeout |
float
|
Query timeout in seconds. |
id |
str | None
|
Optional dependency ID. |
Source code in src/database_pydantic_ai/sql/toolset.py
create_database_toolset(*, id=None)
¶
Create a database toolset for AI Agents.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
str | None
|
Optional toolset ID. |
None
|
Returns:
| Type | Description |
|---|---|
FunctionToolset[SQLDatabaseDeps]
|
FunctionalToolset with database tools |
...
Source code in src/database_pydantic_ai/sql/toolset.py
| Python | |
|---|---|
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | |
Default System Prompt¶
This is the prompt used to instruct the agent:
SQLITE_SYSTEM_PROMPT = """
## SQLite Database Toolset
### IMPORTANT
* Database may be running in READ-ONLY mode
* When in read-only mode, only SELECT queries are allowed
You have access to SQLite database tools for database operations and querying:
* `list_tables` - list all tables which are present in database
* `get_schema` - read the database schema
* `describe_table` - describe table's content
* `explain_query` - explains dependencies which given SQL query needs to run
* `query` - execute a SQL query on a database to retrieve data
### Best Practices
* Always try to perform sample data query before performing full query process
* Before running any query, validate it with current schema of the tables and database
* Be careful with querying the database, try to validate beforehand
* Use `LIMIT` clause when querying large tables to avoid retrieving too much data
Backends¶
SQLite¶
database_pydantic_ai.sql.backends.sqlite.SQLiteDatabase
¶
Bases: BaseSQLDatabase, SQLDatabaseProtocol
Source code in src/database_pydantic_ai/sql/backends/sqlite.py
| Python | |
|---|---|
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | |
__aenter__()
async
¶
__aexit__(exc_type, exc_val, exc_tb)
async
¶
Ensure the connection is closed when exiting the context.
Source code in src/database_pydantic_ai/sql/backends/sqlite.py
connect()
async
¶
Connect to the database
Source code in src/database_pydantic_ai/sql/backends/sqlite.py
close()
async
¶
execute(query, params=None)
async
¶
Execute a SQL query with optional parameters.
Source code in src/database_pydantic_ai/sql/backends/sqlite.py
get_tables()
async
¶
Get list of tables in the public schema.
Source code in src/database_pydantic_ai/sql/backends/sqlite.py
get_foreign_keys(table_name)
async
¶
Get information about foreign keys in given table
Source code in src/database_pydantic_ai/sql/backends/sqlite.py
get_table_info(table_name, return_md=True)
async
¶
Get detailed information about a specific table.
Source code in src/database_pydantic_ai/sql/backends/sqlite.py
get_schema(return_md=True)
async
¶
Get database schema information.
Source code in src/database_pydantic_ai/sql/backends/sqlite.py
PostgreSQL¶
database_pydantic_ai.sql.backends.postgres.PostgreSQLDatabase
¶
Bases: BaseSQLDatabase, SQLDatabaseProtocol
Source code in src/database_pydantic_ai/sql/backends/postgres.py
| Python | |
|---|---|
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 | |
__aenter__()
async
¶
__aexit__(exc_type, exc_val, exc_tb)
async
¶
Ensure the pool is closed when exiting the context.
Source code in src/database_pydantic_ai/sql/backends/postgres.py
connect(min_size=1, max_size=10, command_timeout=60.0, timeout=120.0)
async
¶
Connect to the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
min_size
|
int
|
Minimum size of the connection pool. |
1
|
max_size
|
int
|
Maximum size of the connection pool. |
10
|
command_timeout
|
float
|
Timeout for individual queries in seconds. |
60.0
|
timeout
|
float
|
Timeout for establishing the connection in seconds. |
120.0
|
Source code in src/database_pydantic_ai/sql/backends/postgres.py
close()
async
¶
execute(query, params=None)
async
¶
Execute a SQL query with optional parameters.
Source code in src/database_pydantic_ai/sql/backends/postgres.py
get_tables()
async
¶
Get list of tables in the public schema.
Source code in src/database_pydantic_ai/sql/backends/postgres.py
get_foreign_keys(table_name)
async
¶
Get information about foreign keys in given table
Source code in src/database_pydantic_ai/sql/backends/postgres.py
get_table_info(table_name, return_md=True)
async
¶
Get detailed information about a specific table.
Source code in src/database_pydantic_ai/sql/backends/postgres.py
get_schema(return_md=True)
async
¶
Get database schema information.
Source code in src/database_pydantic_ai/sql/backends/postgres.py
explain(query)
async
¶
Get query execution plan.
Source code in src/database_pydantic_ai/sql/backends/postgres.py
Protocol¶
database_pydantic_ai.sql.protocol.SQLDatabaseProtocol
¶
Bases: Protocol
Protocol for database backends.
Source code in src/database_pydantic_ai/sql/protocol.py
Types¶
database_pydantic_ai.types
¶
CustomTypes
¶
Bases: BaseModel
Base class for custom Pydantic models in the database-pydantic-ai library.
This class provides a common configuration for all custom models in the library, allowing arbitrary types to be used in Pydantic models.
Source code in src/database_pydantic_ai/types.py
QueryResult
¶
ColumnInfo
¶
Bases: CustomTypes
Information about a table column.
Source code in src/database_pydantic_ai/types.py
ForeignKeyInfo
¶
TableInfo
¶
Bases: CustomTypes
Information about a database table.