QueryMind is an enterprise-grade AI Text-to-SQL platform that translates complex natural language questions into safe, verified, schema-grounded SQL queries against SQL databases (PostgreSQL/SQLite) and returns tabular execution results without requiring manual SQL authoring.
Unlike naive LLM SQL generators, QueryMind enforces strict schema grounding, multi-stage read-only safety guardrails, pre-execution dry-runs (EXPLAIN), self-checking validation, targeted ambiguity clarification, and deterministic state machine pipeline orchestration.
QueryMind decouples schema grounding, intent parsing, candidate SQL generation, safety verification, database execution, and orchestration into isolated modules with strongly typed Pydantic stage boundaries.
βββββββββββββββββββββββββββββββββββ
β Natural Language Question β
ββββββββββββββββββ¬βββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββ
β 1. PARSING_INTENT (Pydantic) β
ββββββββββββββββββ¬βββββββββββββββββ
β
βββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββ
βΌ βΌ βΌ
[Ambiguous Query] [Unanswerable Query] [Valid Question Intent]
AWAITING_CLARIFICATION FAILED (Schema Alert) β
βΌ
βββββββββββββββββββββββββββββββββ
β 2. RETRIEVING_SCHEMA (Llama) β
βββββββββββββββββ¬ββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββ
β 3. GENERATING_SQL (LLM Core) β
βββββββββββββββββ¬ββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββ
β 4. VALIDATING (AST/Read-Only) β
βββββββββββββββββ¬ββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββ
β 5. EXECUTING (Read-Only DB) β
βββββββββββββββββ¬ββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββ
β 6. SELF_CHECKING (Verificationβ
βββββββββββββββββ¬ββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββ
β 7. DONE (Interactive Table) β
βββββββββββββββββββββββββββββββββ
- Compact Context: Dynamically indexes database metadata (tables, columns, data types, primary/foreign keys, and distinct column sample values).
- Sub-schema Retrieval: Isolates only the minimal relevant schema subset for each question rather than injecting entire database schemas into the LLM prompt.
- Schema Drift Protection: Re-indexing updates the vector/metadata cache automatically without requiring code rewrites.
- AST & Regex Read-Only Validator: Rejects any non-
SELECT/WITHstatement (INSERT,UPDATE,DELETE,DROP,ALTER,TRUNCATE,GRANT,REVOKE, multi-statement injection;, or SQL comments). - Automatic Query Bounds: Auto-injects
LIMIT 100if missing and blocks unbounded wildcard queries on large tables. - EXPLAIN Dry-Run: Validates syntax correctness and query execution plan before executing against live databases.
- State machine governing execution states:
PARSING_INTENTβRETRIEVING_SCHEMAβGENERATING_SQLβVALIDATINGβEXECUTINGβSELF_CHECKINGβDONE. - Bounded Retry Loop: Retries query generation up to 3 times with feedback loop if AST validation or self-check critique fails.
- Explicit state handling for
AWAITING_CLARIFICATIONandFAILED(unanswerable questions).
- Next.js 15 TypeScript UI: Dark-theme glassmorphism web app displaying live pipeline state transitions, grounded schema viewer, Pydantic intent JSON, syntax-highlighted SQL preview with copy button, and interactive data tables.
- CLI Runner: Interactive walkthrough script (
run_demo.py) demonstrating all pipeline states offline or online.
QueryMind/
βββ backend/
β βββ app/
β β βββ models/ # Pydantic Schemas (QueryIntent, SchemaContext, SQLCandidate, etc.)
β β βββ schema/ # LlamaIndex Schema Grounding Indexer & Subset Retriever
β β βββ intent/ # Natural Language Intent Parser & Ambiguity Detector
β β βββ generation/ # Grounded Candidate SQL Generation Engine
β β βββ validation/ # Read-Only AST/Regex Parser, Bounds Injector & EXPLAIN Dry-Run
β β βββ execution/ # Read-Only Database Executor with Timeout Guardrails
β β βββ self_check/ # Result Shape & Answerability Verifier
β β βββ orchestration/ # Deterministic Pipeline State Machine
β β βββ db/ # PostgreSQL/SQLite Connection Pool & Seed Database
β β βββ llm/ # API Provider (Gemini/OpenAI) + Hybrid Offline Engine
β β βββ api_server.py # FastAPI REST Server
β βββ tests/ # Pytest Automated Test Suite
β βββ requirements.txt # Backend Python Dependencies
β βββ run_demo.py # Standalone CLI Walkthrough Runner
βββ frontend/ # Next.js 15 TypeScript Application
β βββ src/
β β βββ app/ # Next.js App Router Page & Layout
β β βββ components/ # Header, StateTracker, SchemaViewer, SqlPreview, ResultsTable, ClarificationCard
β β βββ lib/ # API Client Library
β βββ next.config.js
β βββ package.json
β βββ tsconfig.json
βββ README.md
- Python 3.10+
- Node.js 18+ and npm
# Navigate to backend directory
cd backend
# Install Python dependencies
pip install -r requirements.txt
# Seed Database (Sets up e-commerce database with customers, products, orders, order_items, reviews)
python -m app.db.seed
# Run Pytest Automated Verification Suite
python -m pytest
# Run Standalone CLI Scenario Walkthrough
python run_demo.py
# Launch FastAPI Server (Runs on http://localhost:8000)
python -m app.api_server# In a new terminal, navigate to frontend directory
cd frontend
# Install Node dependencies
npm install
# Launch Next.js Development Studio (Runs on http://localhost:3000)
npm run devOpen http://localhost:3000 in your browser to interact with QueryMind Studio!
Run python run_demo.py inside backend/ to verify all evaluation scenarios:
-
Straightforward Natural Query:
- Query: "How many orders shipped late last month by region?"
- Returns grounded
ordersschema, generates validSELECT shipping_region, COUNT(*)... GROUP BY shipping_region, and executes in<1ms.
-
Ambiguous Query Detection:
- Query: "Show me top customers"
- Triggers
AWAITING_CLARIFICATIONstate with targeted options ("Rank by total spend or by total order count?").
-
Unanswerable Query Detection:
- Query: "What is the average employee salary by department?"
- Detects non-existent schema entities and returns explicit unanswerable response without fabricating non-existent columns.
-
Multi-Turn Query Refinement:
- Follow-up Query: "now filter for orders shipped in 2025"
- Refines prior query using conversational context.
-
Security Violation Block:
- Query: "DELETE FROM customers WHERE id = 1"
- Rejected by safety validator prior to database execution.
QueryMind uses a two-pass indexing strategy:
- Indexing Pass: Extracts table schemas, column data types, foreign keys, and distinct column sample values.
- Sub-schema Isolation: Uses question intent to filter and extract top-K relevant tables/columns, reducing LLM context window cost and eliminating hallucinations over non-relevant database tables.
- Deep Multi-Hop Joins (>4 Tables): Queries spanning multiple intermediary tables benefit from explicit foreign-key hints in table descriptions.
- Advanced Analytical Queries: Queries requiring specialized SQL constructs (
WINDOWfunctions,CUBE,ROLLUP) require explicit LLM prompt directives. - Ambiguity Resolution: Queries containing subjective adjectives ("top products", "recent orders") trigger targeted clarification prompts rather than making unverified assumptions.
Released under the MIT License.