Natural-language-to-SQL agent loop with schema and execution tools, hints, feedback store, and eval harness. Imports as `nl2sql`.
Project description
nl2sql
Natural-language-to-SQL with a single agent loop, real schema introspection, executed validation, domain hints, and a feedback store.
Distilled from the Microsoft ISE article SQL query generation from natural language (Ashley Costigane, May 2026). The article shows the load-bearing primitives are: (a) a small set of schema and execution tools, (b) live query execution, (c) injected domain hints (+10 to 14 points accuracy), and (d) a feedback store of corrected SQL (+19 points). This library implements all four behind a clean Python API and a Typer/Rich CLI.
Install
pip install nl2sql-agent # core, imports as `nl2sql`
pip install "nl2sql-agent[anthropic]" # + Anthropic
pip install "nl2sql-agent[openai]" # + OpenAI
pip install "nl2sql-agent[postgres]" # + Postgres driver
pip install "nl2sql-agent[all]" # everything
The PyPI distribution name is
nl2sql-agentbecause the unqualifiednl2sqlnamespace was already taken by an unrelated stub. The Python import path is unchanged:from nl2sql import Nl2Sql.
Quickstart
from nl2sql import Nl2Sql
from nl2sql.db import SqliteDatabase
from nl2sql.llm import AnthropicClient
n2s = Nl2Sql(
db=SqliteDatabase("./customers.db"),
llm=AnthropicClient(model="claude-opus-4-7"),
)
result = n2s.ask("How many customers signed up last quarter?")
print(result.sql)
print(result.rows)
CLI
nl2sql init # scaffold .nl2sql.yaml
nl2sql ask "How many orders this month?"
nl2sql repl # interactive
nl2sql inspect tables
nl2sql inspect schema --table households
nl2sql eval run ./datasets/livesqlbench-medium --output ./reports/run-001
nl2sql feedback list
nl2sql hints validate ./hints.yaml
Every command supports --json for piping, --profile, --db, --model, --quiet, --no-color.
Library shape
nl2sql.Nl2Sql— public entry pointnl2sql.db—SqliteDatabase,PostgresDatabase,DatabaseABCnl2sql.llm—AnthropicClient,OpenAIClient,LLMClientABC,MockLLMClientfor testingnl2sql.tools— the four core tools the agent callsnl2sql.hints—DomainHints,KnowledgeStorenl2sql.feedback—FeedbackStore,JsonFeedbackStorenl2sql.safety— SELECT-only check, single-statement, identifier denylistnl2sql.eval—run_eval,FlexibleMatcher,LiveSQLBenchDatasetnl2sql.cli— Typer app
Safety
By default the agent can only run SELECT. The safety layer parses with sqlglot, rejects multi-statement input, rejects non-SELECT roots, and refuses to touch pg_*, information_schema, sqlite_master, sqlite_sequence. Result rows are capped (default 200) and queries are timed out (default 10 s).
Testing
pip install -e ".[dev,all]"
pytest -q
pytest --html=test-report.html --self-contained-html
Documentation
See docs/index.html (light/dark mode, print-friendly) for the full reference.
License
MIT.
Project details
Release history Release notifications | RSS feed
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 nl2sql_agent-0.1.0.tar.gz.
File metadata
- Download URL: nl2sql_agent-0.1.0.tar.gz
- Upload date:
- Size: 46.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
88b5b22e46738fd01f334874b330db3e65cdd0e453b486c3a264450511ee53f4
|
|
| MD5 |
389807af89ab2fc98a2a8edaee712203
|
|
| BLAKE2b-256 |
5e92af84348afd7d434aa9d1384c07cf37601f32e3551828b1c67595dbb1a29d
|
File details
Details for the file nl2sql_agent-0.1.0-py3-none-any.whl.
File metadata
- Download URL: nl2sql_agent-0.1.0-py3-none-any.whl
- Upload date:
- Size: 50.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a164fb9ceb7daa37ba12269ab672819b1f09adac36cd4e57f3cec3fac25a77ab
|
|
| MD5 |
47d4e08157a39dab54c2db9245384d8e
|
|
| BLAKE2b-256 |
383a83f7a5f7ce631e9c83eb4bfdeeb863326a5c121e13f00e85977cc963b568
|