Skip to main content

A self-correcting natural language to SQL translator using Gemini 2.5 Pro

Project description

TranceSQL

TranceSQL is a lightweight, database-agnostic Python package that translates natural language questions into valid, executable SQL queries using Gemini 2.5 Pro. It contains a built-in self-correcting retry loop that catches database engine errors, feeds them back to Gemini, and corrects them in real-time.

By using your own database execution callbacks, TranceSQL does not require database-specific drivers or expensive Premium capacities (like Microsoft Fabric F64+ for Genie/Copilot). It runs anywhere you can execute Python.


Installation

Install from PyPI (Production)

pip install TranceSQL

Install directly from GitHub (R&D / Test)

pip install git+https://github.com/navakanth1984/TranceSQL.git

How it Connects to Databases (The Callback Pattern)

TranceSQL does not manage database connections or credentials. Instead, you supply an execution callback function: exec_callback(sql_query) -> (columns, rows).

If this callback raises an exception during database execution (such as a syntax error or a missing column name), TranceSQL automatically intercepts it and requests a correction from Gemini.

1. Connecting to Local SQLite

import sqlite3
import os
from trancesql import TranceSQLTranslator

# 1. Define schema grounding
schema = """
Table: jobs
Columns:
  - job_id (TEXT, PRIMARY KEY)
  - status (TEXT): 'failed', 'completed', 'pending'
  - prompt (TEXT)
  - created_at (TIMESTAMP)
"""

# 2. Initialize TranceSQL
os.environ["GEMINI_API_KEY"] = "your-api-key"
translator = TranceSQLTranslator(
    db_schema=schema, 
    db_type="SQLite",
    telemetry_log_path="logs/trancesql_telemetry.jsonl"
)

# 3. Create a read-only database execution callback
def run_sqlite(query):
    # Connect in read-only mode to prevent injection attacks
    conn = sqlite3.connect("file:jobs.db?mode=ro", uri=True)
    try:
        cursor = conn.cursor()
        cursor.execute(query)
        results = cursor.fetchall()
        columns = [desc[0] for desc in cursor.description]
        return columns, results
    finally:
        conn.close()

# 4. Translate and Run!
result = translator.translate("List the prompts for all failed jobs", run_sqlite)
if result["success"]:
    print("Columns:", result["columns"])
    print("Rows:", result["results"])

2. Connecting to Azure Databricks (Spark SQL)

To run this in a Databricks Notebook, install the package using %pip install and leverage the built-in PySpark spark session:

# Cell 1
%pip install git+https://github.com/navakanth1984/TranceSQL.git
# Cell 2
import os
from trancesql import TranceSQLTranslator

# GROUNDING: Define schemas for your Delta tables
lakehouse_schema = """
Table: hive_metastore.sales.orders
Columns:
  - order_id (STRING)
  - customer_id (STRING)
  - amount (DOUBLE)
  - order_date (DATE)
"""

translator = TranceSQLTranslator(db_schema=lakehouse_schema, db_type="SparkSQL")

# Databricks Spark SQL callback
def run_databricks_sql(query):
    # spark is globally available in Databricks notebooks
    df = spark.sql(query)
    return df.columns, df.collect()

# Translate natural language to Spark SQL
result = translator.translate("Show order ids and amounts for orders over 5000", run_databricks_sql)

3. Connecting to Microsoft Fabric (Lakehouse / Warehouse)

No Premium capacity (F64+) is needed! Fabric Spark notebooks can execute it using the standard spark instance in any workspace:

# Cell 1
%pip install git+https://github.com/navakanth1984/TranceSQL.git
# Cell 2
import os
from trancesql import TranceSQLTranslator

fabric_schema = """
Table: LH_Sales.customers
Columns:
  - CustomerKey (INT)
  - CustomerName (VARCHAR)
  - Region (VARCHAR)
"""

translator = TranceSQLTranslator(db_schema=fabric_schema, db_type="SparkSQL")

def run_fabric_sql(query):
    # spark runs on your Lakehouse Spark compute
    df = spark.sql(query)
    return df.columns, df.collect()

result = translator.translate("Find customers in the Europe region", run_fabric_sql)

Telemetry and R&D Feedback Loops

To continuously improve the accuracy of translations, configure the telemetry_log_path argument. TranceSQL will write telemetry objects for each run, which you can load and analyze.

Recording Manual Feedback

You can capture direct user ratings and comments to fine-tune your grounding context:

# Translate the query
result = translator.translate("List recently added jobs", run_sqlite)

# If you are displaying this in a web UI or dashboard, save user feedback
translator.save_manual_feedback(
    session_id=result["session_id"],
    rating=5,
    comment="Query returned correct columns instantly."
)

These ratings are logged directly into the telemetry log file for off-line auditing.

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

trancesql-0.2.0.tar.gz (12.4 kB view details)

Uploaded Source

Built Distribution

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

trancesql-0.2.0-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file trancesql-0.2.0.tar.gz.

File metadata

  • Download URL: trancesql-0.2.0.tar.gz
  • Upload date:
  • Size: 12.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for trancesql-0.2.0.tar.gz
Algorithm Hash digest
SHA256 781414bb9acf748a7cdbbcfac2c5286a62443dbe86783b42c389bed966ee11bd
MD5 ccaa7b07bc2bfd35fc96d3549005f3eb
BLAKE2b-256 128605359b240beba76398e6a25cf255defef6a75ae5b5aa7978b92a22af0947

See more details on using hashes here.

File details

Details for the file trancesql-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: trancesql-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 8.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for trancesql-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8f3a24c6ef8b447e87e396873b33654cd57b933a578338fb855fc639dff9775d
MD5 b490489bf68605723064205b90a8ecd0
BLAKE2b-256 c1515ab1ce93e44f2b3dcf4e24245d0e6fe3c6c3033dabc9f7aeac8fc1601017

See more details on using hashes here.

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