Natural language to SQL to JSON results using Gemini.
Project description
HumeSQL
HumeSQL turns natural language into safe SQL, runs it against your MySQL/MariaDB database, and returns JSON rows. It introspects your schema, prompts Gemini to craft SQL, applies guardrails, executes the query, and responds with the SQL, reasoning, and results.
- Fast start: drop in DB creds + Gemini key, ask questions in English.
- Safe by default: blocks destructive SQL, adds LIMITs, only uses tables in your schema.
- Flexible: Python API + CLI, schema caching, debug output from the LLM.
Documentation: https://nirajang20.github.io/HumeSQL/
Installation
PyPI:
pip install humesql
From source:
pip install -e .
Quickstart (Python)
from humesql import HumeSQL
db = {
"host": "localhost",
"user": "root",
"password": "",
"database": "classicmodels",
"port": 3306,
}
h = HumeSQL(db, model="gemini-2.5-flash")
res = h.query("Show me the last 5 users who signed up from Nepal", debug=True)
if res["ok"]:
print("SQL:", res["sql"])
print("Reasoning:", res["reasoning"])
print("Rows:", res["rows"])
else:
print("Error:", res["error"])
if res.get("raw_ai_response"):
print("Raw:", res["raw_ai_response"])
Response structure (keys may be null on errors):
ok: boolsql: generated SQLreasoning: short explanationlimit_applied: bool ornullrows: list of result dicts (if SELECT)time_ms: total time in millisecondsraw_ai_response: only whendebug=True
Configuration
Environment variable priority for the Gemini key:
- Explicit
api_keyargument HUMESQL_AI_KEY(preferred)HUMANSQL_AI_KEY(legacy)GEMINI_API_KEY
Example:
export HUMESQL_AI_KEY="your-gemini-key"
CLI
Run one-off queries from the terminal:
humesql "last 5 customers from Nepal" \
-H localhost -u root -p "" -d classicmodels --port 3306 \
--api-key "$HUMESQL_AI_KEY" --model gemini-2.5-flash --debug
Key flags:
query(positional): natural language input-H/--host,-u/--user,-p/--password,-d/--database,--port--api-key: override env vars--model: Gemini model (defaultgemini-2.5-flash)--no-cache-schema: disable schema caching--debug: include raw AI response in output
Exit code 0 on success, 1 on error.
API highlights
HumeSQL(
db_config: dict,
model: str = "gemini-2.5-flash",
api_key: str | None = None,
cache_schema: bool = True,
)
.query(user_text: str, debug: bool = False) -> dict.refresh_schema_cache()
Safety guardrails
- Blocks SQL containing
DROP,TRUNCATE,ALTER,;, orinformation_schema - Adds
LIMIT 100if the model omits it on SELECT - Rejects non-JSON or malformed model responses
Schema caching
- Enabled by default; disable with
cache_schema=False - Call
refresh_schema_cache()after DB schema changes
Prompting tips
- Be explicit about filters: “last 5 orders from customers in Nepal”
- Name entities when possible: table/column hints help
- State aggregations/ranges: “average order value per month in 2023”
Troubleshooting
- API key not valid: check
HUMESQL_AI_KEYor pass--api-key; ensure the key has access to your chosen Gemini model. - Model not found (404): pick a supported model, e.g.,
--model gemini-2.5-flash. - DB connection errors: verify host/port/user/password and MySQL reachability.
- Malformed JSON from model: rerun with
debug=Trueto inspectraw_ai_response. - Schema changes not reflected: call
refresh_schema_cache()or disable caching.
Development
- Install deps:
pip install -e . - Example: set env vars + DB creds, then
python example.py - Type check / lint locally as needed
License
MIT — see LICENSE.
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 humesql-0.3.2.tar.gz.
File metadata
- Download URL: humesql-0.3.2.tar.gz
- Upload date:
- Size: 10.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1e17e46699a5b4c7456cd5b49c86bf5e411ff64b0d616629fbc9a5cfd8819786
|
|
| MD5 |
2198a749ba0def4b9cac804d4161aadc
|
|
| BLAKE2b-256 |
a900f3b7c897d08ce5d656b3575c9e020efd65338ae1f8e8c5771d92553a15d4
|
Provenance
The following attestation bundles were made for humesql-0.3.2.tar.gz:
Publisher:
publish.yml on nirajang20/HumeSQL
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
humesql-0.3.2.tar.gz -
Subject digest:
1e17e46699a5b4c7456cd5b49c86bf5e411ff64b0d616629fbc9a5cfd8819786 - Sigstore transparency entry: 702307963
- Sigstore integration time:
-
Permalink:
nirajang20/HumeSQL@8f48b100f6d2eb79e03ed6a6dc46375c74d162a7 -
Branch / Tag:
refs/tags/v0.3.3 - Owner: https://github.com/nirajang20
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@8f48b100f6d2eb79e03ed6a6dc46375c74d162a7 -
Trigger Event:
push
-
Statement type:
File details
Details for the file humesql-0.3.2-py3-none-any.whl.
File metadata
- Download URL: humesql-0.3.2-py3-none-any.whl
- Upload date:
- Size: 11.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
353e2e128b9a7c1ea80837ca68a2ecc2a7a8ca538f9762b74feed2a2031fc81a
|
|
| MD5 |
455af04681d38affa26208830be3fca6
|
|
| BLAKE2b-256 |
6e52862bf8cb42568d8ba21dfcce145d8c63580fbf61aabd29326cf8c1771454
|
Provenance
The following attestation bundles were made for humesql-0.3.2-py3-none-any.whl:
Publisher:
publish.yml on nirajang20/HumeSQL
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
humesql-0.3.2-py3-none-any.whl -
Subject digest:
353e2e128b9a7c1ea80837ca68a2ecc2a7a8ca538f9762b74feed2a2031fc81a - Sigstore transparency entry: 702307965
- Sigstore integration time:
-
Permalink:
nirajang20/HumeSQL@8f48b100f6d2eb79e03ed6a6dc46375c74d162a7 -
Branch / Tag:
refs/tags/v0.3.3 - Owner: https://github.com/nirajang20
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@8f48b100f6d2eb79e03ed6a6dc46375c74d162a7 -
Trigger Event:
push
-
Statement type: