Skip to main content

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.

Metadata

Release files for TranceSQL 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for TranceSQL 0.2.0
File Size Uploaded Provenance Actions
trancesql-0.2.0.tar.gz 12.4 kB Without provenance Details for trancesql-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for TranceSQL 0.2.0
File Interpreter ABI Platform Actions
trancesql-0.2.0-py3-none-any.whl Python 3 none any Details for trancesql-0.2.0-py3-none-any.whl

Total release size: 21.2 kB

Release files / trancesql-0.2.0.tar.gz

Download URL trancesql-0.2.0.tar.gz
Size 12.4 kB
Tags Source
SHA-256 checksum
How to use checksums
781414bb9acf748a7cdbbcfac2c5286a62443dbe86783b42c389bed966ee11bd
BLAKE2b-256 checksum
How to use checksums
128605359b240beba76398e6a25cf255defef6a75ae5b5aa7978b92a22af0947
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.10

Release files / trancesql-0.2.0-py3-none-any.whl

Download URL trancesql-0.2.0-py3-none-any.whl
Size 8.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8f3a24c6ef8b447e87e396873b33654cd57b933a578338fb855fc639dff9775d
BLAKE2b-256 checksum
How to use checksums
c1515ab1ce93e44f2b3dcf4e24245d0e6fe3c6c3033dabc9f7aeac8fc1601017
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.10

Security for TranceSQL 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page