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.
Metadata
Release files for nl2sql-agent 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| nl2sql_agent-0.1.0.tar.gz | 46.8 kB | Without provenance | Details for nl2sql_agent-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| nl2sql_agent-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for nl2sql_agent-0.1.0-py3-none-any.whl |
Total release size: 96.9 kB
Release files / nl2sql_agent-0.1.0.tar.gz
| Download URL | nl2sql_agent-0.1.0.tar.gz |
|---|---|
| Size | 46.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
88b5b22e46738fd01f334874b330db3e65cdd0e453b486c3a264450511ee53f4
|
|
BLAKE2b-256 checksum How to use checksums |
5e92af84348afd7d434aa9d1384c07cf37601f32e3551828b1c67595dbb1a29d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.2
|
Release files / nl2sql_agent-0.1.0-py3-none-any.whl
| Download URL | nl2sql_agent-0.1.0-py3-none-any.whl |
|---|---|
| Size | 50.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a164fb9ceb7daa37ba12269ab672819b1f09adac36cd4e57f3cec3fac25a77ab
|
|
BLAKE2b-256 checksum How to use checksums |
383a83f7a5f7ce631e9c83eb4bfdeeb863326a5c121e13f00e85977cc963b568
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.2
|
Security for nl2sql-agent 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.