Skip to main content

Sqlalchemy nl2sql agent

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.1.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.1-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for sqlalchemy_llm_agent-0.1.1.tar.gz
Algorithm Hash digest
SHA256 b00fd39d6654d57fd3c92978286207ab9bd40fd85c8b97b89dd06d3ac5c9e527
MD5 7fb2ba9844b9dabdd596aebe5931216c
BLAKE2b-256 10ad0b9496a1c3760ac1ba4fe3f97aa20881e6dcde09b2cb4abad2d2b636608e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sqlalchemy_llm_agent-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f51a95e70991ff6e41d2e13e37e53ad268902ad4ca0f5387c4f4d023ea3c2508
MD5 49924eafbc746ecde76bfecbc9ac7c9b
BLAKE2b-256 dfc4600b339ef0d36e7ff25f35c2971950bb3681c38d736a2bfd7b198f6ba574

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