Tool

Query Databases with AI

LlamaIndex tool that lets an agent query a SQLAlchemy-connected database and explore its schema.

Works with sqlalchemypostgresql

73
Spark score
out of 100
Updated 2 days ago
Version 0.14.23
Models

Add to Favorites

Why it matters

Empower AI agents to directly interact with and query relational databases. Extract structured data and gain insights from your database tables.

Outcomes

What it gets done

01

List tables within a database schema.

02

Describe the schema of specific tables.

03

Execute SQL queries and retrieve results.

04

Integrate database access into AI agent workflows.

Install

Add it to your toolbox

Run in your project directory:

curl -fsSL https://spark.entire.vc/get/li-tool-tools-database | bash

Overview

Database Tool

The Database Tool wires a SQLAlchemy-backed database connection into a LlamaIndex agent as three callable functions: list tables, describe a table's schema, and run an SQL query. Use it when an agent needs to explore a database's schema or query it in natural language. It requires working connection credentials for an accessible database.

What it does

The Database Tool connects to a database using SQLAlchemy under the hood and gives an agent the ability to query the database and get information about its tables. It exposes three functions: list_tables (list the tables in the database schema), describe_tables (describe the schema of a table), and load_data (accepts an SQL query and returns the result). It is designed to be used as a way to load data as a Tool in an Agent.

When to use - and when NOT to

Use it when an agent needs to explore an existing database's schema or run SQL queries against it -- for example answering "What tables does this database contain," "Describe the first table," and "Retrieve the first row of that table" in sequence. Because SQLAlchemy is doing the connecting, it works with any SQLAlchemy-supported database (the example uses PostgreSQL), but it needs working connection credentials, so it is not usable without an accessible database instance.

Inputs and outputs

Setup takes standard database connection parameters -- scheme, host, port, user, password, and database name -- passed to DatabaseToolSpec, then attached to an agent via to_tool_list():

from llama_index.tools.database import DatabaseToolSpec
from llama_index.core.agent.workflow import FunctionAgent
from llama_index.llms.openai import OpenAI

db_tools = DatabaseToolSpec(
    scheme="postgresql",  # Database Scheme
    host="localhost",  # Database Host
    port="5432",  # Database Port
    user="postgres",  # Database User
    [REDACTED],  # Database Password
    dbname="postgres",  # Database Name
)
agent = FunctionAgent(
    tools=db_tools.to_tool_list(),
    llm=OpenAI(model="gpt-4.1"),
)

print(await agent.run("What tables does this database contain"))
print(await agent.run("Describe the first table"))
print(await agent.run("Retrieve the first row of that table"))

Who it's for

Developers building LlamaIndex agents that need natural-language access to a SQL database's schema and contents without writing SQL by hand for every request.

Source README

Database Tool

This tool connects to a database (using SQLAlchemy under the hood) and allows an Agent to query the database and get information about the tables.

Usage

This tool has more extensive example usage documented in a Jupyter notebook here.

Here's an example usage of the DatabaseToolSpec.

from llama_index.tools.database import DatabaseToolSpec
from llama_index.core.agent.workflow import FunctionAgent
from llama_index.llms.openai import OpenAI

db_tools = DatabaseToolSpec(
    scheme="postgresql",  # Database Scheme
    host="localhost",  # Database Host
    port="5432",  # Database Port
    user="postgres",  # Database User
    [REDACTED],  # Database Password
    dbname="postgres",  # Database Name
)
agent = FunctionAgent(
    tools=db_tools.to_tool_list(),
    llm=OpenAI(model="gpt-4.1"),
)

print(await agent.run("What tables does this database contain"))
print(await agent.run("Describe the first table"))
print(await agent.run("Retrieve the first row of that table"))

The tools available are:

list_tables: A tool to list the tables in the database schema
describe_tables: A tool to describe the schema of a table
load_data: A tool that accepts an SQL query and returns the result

This loader is designed to be used as a way to load data as a Tool in a Agent.

FAQ

Common questions

Discussion

Questions & comments · 0

Sign In Sign in to leave a comment.