Natural Language to SQL - Database First
Project description
SmartQL
Natural Language to SQL — Database First.
SmartQL is a Python library that converts natural language questions into SQL queries using a YAML-based semantic layer. It connects directly to databases, understands your schema, and generates safe, optimized queries.
Architecture
┌─────────────────────────────────────────────────────────────────┐
│ Natural Language Query │
│ "Show me all users who spent over $1000" │
└─────────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────┐
│ SmartQL Core │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────────────────┐ │
│ │ LiteLLM │ │ sqlglot │ │ Semantic Layer │ │
│ │ (100+ LLMs)│ │ (SQL Parser)│ │ (YAML Config) │ │
│ └─────────────┘ └─────────────┘ └─────────────────────────┘ │
└─────────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────┐
│ Generated SQL │
│ SELECT u.* FROM users u JOIN orders o ON o.user_id = u.id │
│ WHERE o.total >= 1000 GROUP BY u.id │
└─────────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────┐
│ Database │
│ MySQL, PostgreSQL, SQLite, SQL Server │
└─────────────────────────────────────────────────────────────────┘
Features
- 100+ LLM Providers — OpenAI, Claude, Gemini, Groq, Ollama, and more via LiteLLM
- SQL Security — AST-based validation with sqlglot (not regex)
- Semantic Layer — YAML config with business context, aliases, and rules
- Auto-Introspection — Discover schema from existing databases
- Interactive CLI — Shell mode for testing queries
- REST API — Optional HTTP server for language-agnostic access
Installation
pip install smartql
Or with uv:
uv pip install smartql
Quick Start
1. Create a configuration file (smartql.yml)
version: "1.0"
database:
type: mysql
connection:
host: localhost
database: myapp
user: ${DB_USER}
password: ${DB_PASSWORD}
llm:
provider: groq
groq:
api_key: ${GROQ_API_KEY}
model: llama-3.1-8b-instant
security:
mode: read_only
max_rows: 1000
2. Use the CLI
# Interactive shell
smartql shell -c smartql.yml
# Single query
smartql ask "How many users signed up this month?" -c smartql.yml
# With execution
smartql ask "Show me the top 5 customers" -c smartql.yml --execute
3. Use in Python
from smartql import SmartQL
sql = SmartQL.from_yaml("smartql.yml")
# Generate SQL
result = sql.ask("Show me all active users with orders")
print(result.sql)
print(result.explanation)
# Generate and execute
result = sql.ask("Top 10 customers by revenue", execute=True)
for row in result.rows:
print(row)
Semantic Layer
The semantic layer adds business context to your database schema:
semantic_layer:
entities:
customers:
table: users
description: "Registered customers"
aliases: ["users", "members"]
columns:
id:
type: integer
primary: true
email:
type: string
description: "Customer email"
status:
type: enum
values: ["active", "inactive", "suspended"]
relationships:
- name: customer_orders
type: one_to_many
from: customers
to: orders
foreign_key: user_id
business_rules:
- name: active
applies_to: [customers]
definition: "status = 'active'"
description: "Active customers"
- name: high_value
applies_to: [orders]
definition: "total >= 1000"
description: "Orders over $1000"
Without a semantic layer, SmartQL auto-introspects your database schema.
Security
SmartQL uses sqlglot for AST-based SQL parsing and validation:
security:
mode: read_only # Only SELECT queries allowed
allowed_tables: # Whitelist tables
- users
- orders
- products
blocked_columns: # Hide sensitive data
- users.password
- users.api_token
max_rows: 1000 # Prevent large result sets
timeout_seconds: 30 # Query timeout
max_join_depth: 4 # Limit JOIN complexity
LLM Providers
SmartQL uses LiteLLM for unified access to 100+ models:
# OpenAI
llm:
provider: openai
openai:
api_key: ${OPENAI_API_KEY}
model: gpt-4o
# Anthropic Claude
llm:
provider: anthropic
anthropic:
api_key: ${ANTHROPIC_API_KEY}
model: claude-sonnet-4-20250514
# Google Gemini
llm:
provider: google
google:
api_key: ${GEMINI_API_KEY}
model: gemini-2.0-flash
# Groq (fast & free)
llm:
provider: groq
groq:
api_key: ${GROQ_API_KEY}
model: llama-3.1-8b-instant
# Local with Ollama
llm:
provider: ollama
ollama:
model: llama3:8b
api_base: http://localhost:11434
CLI Commands
# Interactive shell
smartql shell -c config.yml
# Ask a question
smartql ask "your question" -c config.yml
# Ask and execute
smartql ask "your question" -c config.yml --execute
# Validate SQL
smartql check "SELECT * FROM users" -c config.yml
# Validate config
smartql validate -c config.yml
# Introspect database
smartql introspect -c "mysql://user:pass@localhost/db" -o schema.yml
# Start HTTP API server
smartql serve -c config.yml --port 8000
Shell Commands
In interactive shell mode:
/help - Show help
/schema - Display database schema
/execute - Toggle auto-execute mode
/clear - Clear the screen
/quit - Exit shell
Examples
See the examples/ directory:
ecommerce/— E-commerce database with products, orders, customerstrakli/— Personal finance tracker with wallets and transactions
License
MIT License
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
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 smartql-0.1.2.tar.gz.
File metadata
- Download URL: smartql-0.1.2.tar.gz
- Upload date:
- Size: 49.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c7e7aaadc1527f209afbacfbfb07d6f51a1b69c54b1da660cac5f22b4c6deca3
|
|
| MD5 |
9f53cca455b2b7467e35e1ba820bc57a
|
|
| BLAKE2b-256 |
02fd3c136f71659b5241aac4ad4a8557a46aa39484670bb046df03dbaad5fc39
|
Provenance
The following attestation bundles were made for smartql-0.1.2.tar.gz:
Publisher:
publish.yml on SmartQL/SmartQL
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
smartql-0.1.2.tar.gz -
Subject digest:
c7e7aaadc1527f209afbacfbfb07d6f51a1b69c54b1da660cac5f22b4c6deca3 - Sigstore transparency entry: 1765277936
- Sigstore integration time:
-
Permalink:
SmartQL/SmartQL@c8d2bdb6871fd827e0bdbcafdd4e0485db7ca9eb -
Branch / Tag:
refs/tags/v0.1.2 - Owner: https://github.com/SmartQL
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@c8d2bdb6871fd827e0bdbcafdd4e0485db7ca9eb -
Trigger Event:
release
-
Statement type:
File details
Details for the file smartql-0.1.2-py3-none-any.whl.
File metadata
- Download URL: smartql-0.1.2-py3-none-any.whl
- Upload date:
- Size: 46.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8006c7be73a364922e21dc0ecb8751342f7610fff4f5a6a19c0254ad8f2588e9
|
|
| MD5 |
a532581fc26982b9ee5d5d587b0a97d1
|
|
| BLAKE2b-256 |
ccedd8b025e0e3ec059d4a6649b20b9acd11cd196ce57f88567897760616db48
|
Provenance
The following attestation bundles were made for smartql-0.1.2-py3-none-any.whl:
Publisher:
publish.yml on SmartQL/SmartQL
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
smartql-0.1.2-py3-none-any.whl -
Subject digest:
8006c7be73a364922e21dc0ecb8751342f7610fff4f5a6a19c0254ad8f2588e9 - Sigstore transparency entry: 1765278032
- Sigstore integration time:
-
Permalink:
SmartQL/SmartQL@c8d2bdb6871fd827e0bdbcafdd4e0485db7ca9eb -
Branch / Tag:
refs/tags/v0.1.2 - Owner: https://github.com/SmartQL
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@c8d2bdb6871fd827e0bdbcafdd4e0485db7ca9eb -
Trigger Event:
release
-
Statement type: