Skip to main content

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 whether work within cli or build quick administrative panel.

Features

  • Only read operations - Gives agent only to use safe sqlqueries.
  • 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 or ["*"]
    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:

  1. Inspect the users and orders tables to learn their columns.
  2. Generate a SQL query using the configured LLM (gpt-5 by default).
  3. 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.

Command-line usage

You can also run ad-hoc queries directly from a terminal with the bundled CLI. Create a Python config file that exposes a sqlalchemy_llm_agent_config variable:

# config.py
from sqlalchemy import create_engine, inspect
from sqlalchemy_llm_agent import SqlalchemyAgentConfig

engine = create_engine("postgresql+psycopg://user:pass@localhost:5432/mydb")
inspector = inspect(engine)

sqlalchemy_llm_agent_config = SqlalchemyAgentConfig(
    api_key="sk-openai-123",
    tables=["payments"],
    inspector=inspector,
    engine=engine,
)

Then invoke the CLI by pointing it to the config file and passing the natural-language query:

sqlalchemy_llm_agent --config ./config.py "Give me success payments"

The command prints the resulting rows as formatted JSON, making it easy to script around or inspect responses interactively.

Metadata

Release files for sqlalchemy-llm-agent 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sqlalchemy-llm-agent 0.1.3
File Size Uploaded
sqlalchemy_llm_agent-0.1.3.tar.gz 8.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sqlalchemy-llm-agent 0.1.3
File Interpreter ABI Platform
sqlalchemy_llm_agent-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 17.0 kB

Release files / sqlalchemy_llm_agent-0.1.3.tar.gz

Download URL sqlalchemy_llm_agent-0.1.3.tar.gz
Size 8.4 kB
Tags Source
SHA-256 checksum
How to use checksums
26308fec6f1ca8f5e2a4fb7855101b332f0ff75057efbef079da5625032a7186
BLAKE2b-256 checksum
How to use checksums
dea16eaeb3d477d1ebb1adef7872d9edf38c763d59a68caa4025f1c19224bf04
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.14

Release files / sqlalchemy_llm_agent-0.1.3-py3-none-any.whl

Download URL sqlalchemy_llm_agent-0.1.3-py3-none-any.whl
Size 8.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7558eaa389ebcb5233611453d7f5dd483565eb521468f7a5588814140bc17fab
BLAKE2b-256 checksum
How to use checksums
288ef0ba5314d2580d71b1ec4817cf0dc2362da855c3068ae109fc9c44ea24d2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.14

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page