Add your description here
Project description
SQLAlchemy LLM Agent
sqlalchemy-llm-agent packages a small, batteries-included LangChain agent that can inspect
SQLAlchemy schemas and run ad-hoc SQL queries generated by an LLM. It is helpful when you want
to expose a controlled subset of your database to an LLM-powered assistant without wiring up
all of the boilerplate yourself.
Features
- Table-aware prompts – the agent automatically enumerates the tables available to it and bakes them into its system prompt so the LLM understands the data surface area it can use.
- Schema inspection tool – provides a LangChain tool that reads table metadata via
sqlalchemy.inspectso the LLM can learn column names and types before writing SQL. - Query execution tool – safely executes generated SQL statements through your SQLAlchemy
Engineand returns rows as dictionaries for further processing. - Config-driven access control – restrict the agent to a whitelist of tables by name or give
it access to every table in the database via the special
"*"entry.
Installation
pip install sqlalchemy-llm-agent
Quickstart
Below is a minimal end-to-end example that shows how to configure and call the agent. This
example assumes you already have a SQLAlchemy Engine and have run inspect(engine) to
produce an inspector object.
from sqlalchemy import create_engine, inspect
from sqlalchemy_llm_agent import SqlalchemyAgent, SqlalchemyAgentConfig
engine = create_engine("postgresql+psycopg://user:pass@localhost:5432/mydb")
inspector = inspect(engine)
config = SqlalchemyAgentConfig(
api_key="sk-openai-123", # Replace with your actual OpenAI API key
model="gpt-5", # Optional, defaults to gpt-5
tables=["users", "orders"], # Limit the agent to just these tables
row_limit=100,
inspector=inspector,
engine=engine,
)
agent = SqlalchemyAgent(config)
result_rows = agent.query("List the five most recent orders including user email")
for row in result_rows:
print(row)
The agent will automatically:
- Inspect the
usersandorderstables to learn their columns. - Generate a SQL query using the configured LLM (
gpt-5by default). - Execute the query through your SQLAlchemy engine and return the results as a list of dictionaries.
You now have a reusable component that can power chatbots, dashboards, or internal tools that need natural-language access to relational data.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sqlalchemy_llm_agent-0.1.0.tar.gz.
File metadata
- Download URL: sqlalchemy_llm_agent-0.1.0.tar.gz
- Upload date:
- Size: 6.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e03b757091b697660e0421b4863165738c78f98dc6e875f4a0c3fbd60430adbf
|
|
| MD5 |
e9bfd5b7ae7ca364418f7d4bd406cdd0
|
|
| BLAKE2b-256 |
d3634fb634030045f98079a0bf91a719f2c4592a5d4bf851c147c7b74c52b1e6
|
File details
Details for the file sqlalchemy_llm_agent-0.1.0-py3-none-any.whl.
File metadata
- Download URL: sqlalchemy_llm_agent-0.1.0-py3-none-any.whl
- Upload date:
- Size: 7.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2135e0a0992a901fb0f5894d5ab86685c3d1d5b36131b5cd65ca5d2f1b7e1b17
|
|
| MD5 |
b29f734704d203d07b1aa3a9af2ee2e0
|
|
| BLAKE2b-256 |
8b014149f50195c1e763a9a629e05eca68be1ecb40826a26fb56ae1995fa4772
|