Skip to main content

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-agent because the unqualified nl2sql namespace 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 point
  • nl2sql.db — SqliteDatabase, PostgresDatabase, Database ABC
  • nl2sql.llm — AnthropicClient, OpenAIClient, LLMClient ABC, MockLLMClient for testing
  • nl2sql.tools — the four core tools the agent calls
  • nl2sql.hints — DomainHints, KnowledgeStore
  • nl2sql.feedback — FeedbackStore, JsonFeedbackStore
  • nl2sql.safety — SELECT-only check, single-statement, identifier denylist
  • nl2sql.eval — run_eval, FlexibleMatcher, LiveSQLBenchDataset
  • nl2sql.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)

Source distribution for nl2sql-agent 0.1.0
File Size Uploaded
nl2sql_agent-0.1.0.tar.gz 46.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for nl2sql-agent 0.1.0
File Interpreter ABI Platform
nl2sql_agent-0.1.0-py3-none-any.whl Python 3 none any Details

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

Release history Release notifications | RSS feed

This release

0.1.0 This release

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