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, and responds with the SQL, reasoning, and results.
Features
- Natural language → SQL → JSON rows
- MySQL/MariaDB via
mysql-connector-python - Gemini models (default:
gemini-2.5-flash) - Built-in guardrails against destructive SQL
- Schema caching for speed
- Python API and
humesqlCLI - Debug mode to see raw model output
Requirements
- Python 3.9+
- MySQL/MariaDB reachable from where you run HumeSQL
- Google Gemini API key with access to the chosen model
Install
From PyPI:
pip install humesql
From source (editable):
pip install -e .
Configuration
HumeSQL looks for an API key in this order:
- Explicit
api_keyargument HUMESQL_AI_KEY(preferred)HUMANSQL_AI_KEY(legacy)GEMINI_API_KEY
Set an environment variable, for example:
export HUMESQL_AI_KEY="your-gemini-key"
Database credentials are passed as a dict:
db = {
"host": "localhost",
"user": "root",
"password": "",
"database": "classicmodels",
"port": 3306,
}
Quick start (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"])
Returned structure:
{
"ok": true,
"error": null,
"sql": "SELECT ...",
"limit_applied": true,
"reasoning": "short explanation",
"rows": [ { "col": "value" }, ... ],
"time_ms": 123.45,
"raw_ai_response": "model text" // only when debug=True
}
CLI usage
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
CLI arguments:
query(positional): natural language input-H/--host,-u/--user,-p/--password,-d/--database,--port--api-key: overrides env vars--model: Gemini model name (defaultgemini-2.5-flash)--no-cache-schema: disable schema caching--debug: include raw AI response in output
Exit code is 0 on success, 1 on error.
API reference (core)
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: main call.refresh_schema_cache(): reload schema immediately
Schema caching
- Enabled by default (
cache_schema=True) - Turn off by passing
cache_schema=False - Call
refresh_schema_cache()after DB changes (new tables/columns)
Safety guardrails
- Blocks SQL containing
DROP,TRUNCATE,ALTER,;, orinformation_schema - Adds
LIMIT 100if the model returns a SELECT without LIMIT - Rejects non-JSON or malformed model responses
Models
- Default:
gemini-2.5-flash - Override via constructor
model="gemini-1.5-pro-latest"or CLI--model ... - Ensure your API key has access to the chosen model
Prompting tips
- Be specific about filters: “last 5 orders from customers in Nepal”
- Mention columns or table hints when you know them
- Include aggregation expectations: “average order value per month”
- For date ranges, state them explicitly
Troubleshooting
- “API key not valid”: check
HUMESQL_AI_KEYor pass--api-key; confirm Gemini access to the model. - 404 model not found: choose a supported model (
--model gemini-2.5-flash). - DB connection errors: verify host/port/user/password and network access to MySQL.
- Malformed JSON from model: rerun with
debug=Trueto inspectraw_ai_response; the JSON sanitizer is lenient but will fail on non-JSON outputs. - Schema changes not reflected: call
refresh_schema_cache()or start withcache_schema=False.
Development
- Install:
pip install -e . - Run example:
python example.py(after setting env vars and DB creds) - CLI smoke test without DB (will error for lack of key):
humesql "test" -H localhost -u root -d testdb --api-key dummy
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.1.tar.gz.
File metadata
- Download URL: humesql-0.3.1.tar.gz
- Upload date:
- Size: 11.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b055f041b88b9edd50ddfa74fda82507880a3746098d2b01373aaa612024d5ea
|
|
| MD5 |
c873448c84ed646708a0fbe2040b0731
|
|
| BLAKE2b-256 |
e0993099e994cd7958a936505bccde623835d5f843f0142914d51d5fc628eda2
|
File details
Details for the file humesql-0.3.1-py3-none-any.whl.
File metadata
- Download URL: humesql-0.3.1-py3-none-any.whl
- Upload date:
- Size: 11.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0f49b0b112020514c41f20cb49c9f9ccdd2b8932b7951f08794a4dd4db635b77
|
|
| MD5 |
8796d4a62695791d434e43e8c5ec45e6
|
|
| BLAKE2b-256 |
8f3da0936de922de6c8a0c74586692393a36b142085c5b0fd364930bbae472d3
|