Skip to main content

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: bool
  • sql: generated SQL
  • reasoning: short explanation
  • limit_applied: bool or null
  • rows: list of result dicts (if SELECT)
  • time_ms: total time in milliseconds
  • raw_ai_response: only when debug=True

Configuration

Environment variable priority for the Gemini key:

  1. Explicit api_key argument
  2. HUMESQL_AI_KEY (preferred)
  3. HUMANSQL_AI_KEY (legacy)
  4. 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 (default gemini-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, ;, or information_schema
  • Adds LIMIT 100 if 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_KEY or 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=True to inspect raw_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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

humesql-0.3.2.tar.gz (10.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

humesql-0.3.2-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

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

Hashes for humesql-0.3.2.tar.gz
Algorithm Hash digest
SHA256 1e17e46699a5b4c7456cd5b49c86bf5e411ff64b0d616629fbc9a5cfd8819786
MD5 2198a749ba0def4b9cac804d4161aadc
BLAKE2b-256 a900f3b7c897d08ce5d656b3575c9e020efd65338ae1f8e8c5771d92553a15d4

See more details on using hashes here.

Provenance

The following attestation bundles were made for humesql-0.3.2.tar.gz:

Publisher: publish.yml on nirajang20/HumeSQL

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for humesql-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 353e2e128b9a7c1ea80837ca68a2ecc2a7a8ca538f9762b74feed2a2031fc81a
MD5 455af04681d38affa26208830be3fca6
BLAKE2b-256 6e52862bf8cb42568d8ba21dfcce145d8c63580fbf61aabd29326cf8c1771454

See more details on using hashes here.

Provenance

The following attestation bundles were made for humesql-0.3.2-py3-none-any.whl:

Publisher: publish.yml on nirajang20/HumeSQL

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page