Skip to main content

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.inspect so the LLM can learn column names and types before writing SQL.
  • Query execution tool – safely executes generated SQL statements through your SQLAlchemy Engine and 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:

  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.

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

sqlalchemy_llm_agent-0.1.0.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sqlalchemy_llm_agent-0.1.0-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file sqlalchemy_llm_agent-0.1.0.tar.gz.

File metadata

File hashes

Hashes for sqlalchemy_llm_agent-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e03b757091b697660e0421b4863165738c78f98dc6e875f4a0c3fbd60430adbf
MD5 e9bfd5b7ae7ca364418f7d4bd406cdd0
BLAKE2b-256 d3634fb634030045f98079a0bf91a719f2c4592a5d4bf851c147c7b74c52b1e6

See more details on using hashes here.

File details

Details for the file sqlalchemy_llm_agent-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for sqlalchemy_llm_agent-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2135e0a0992a901fb0f5894d5ab86685c3d1d5b36131b5cd65ca5d2f1b7e1b17
MD5 b29f734704d203d07b1aa3a9af2ee2e0
BLAKE2b-256 8b014149f50195c1e763a9a629e05eca68be1ecb40826a26fb56ae1995fa4772

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page