A Python-based GenAI project that converts natural-language questions into SQL queries using Google Gemini and executes them against a SQLite database.
Instead of writing SQL manually, users can ask questions such as:
Which customer has placed the most orders?
The application:
- Reads the database schema
- Sends the schema and question to Gemini
- Generates a SQL query
- Validates the generated query
- Executes it against SQLite
- Displays the result
Example:
Question:
Which customer has placed the most orders?
Generated SQL:
SELECT T1.name
FROM customers AS T1
INNER JOIN orders AS T2
ON T1.id = T2.customer_id
GROUP BY T1.id
ORDER BY COUNT(T2.id) DESC
LIMIT 1;
Result:
Suresh
- Python
- Google Gemini 2.5 Flash
- Google Cloud Vertex AI
- SQLite
- google-genai
text-to-sql-ai/
│
├── app/
│ ├── main.py
│ ├── gemini_client.py
│ ├── sql_validator.py
│ ├── database.py
│ └── __init__.py
│
├── database/
│ ├── create_database.py
│ ├── schema.py
│ └── test_database.py
│
├── data/
│ └── sales.db
│
├── requirements.txt
├── .gitignore
└── README.md
The sample SQLite database contains:
customers— customer informationproducts— product informationorders— order information
The application reads the schema dynamically rather than hard-coding it into the main program.
Since the SQL is generated by an LLM, the application validates the query before execution.
Currently, only read-oriented SELECT queries are allowed, while operations such as INSERT, UPDATE, DELETE, DROP, and ALTER are rejected.
Clone the repository and create a virtual environment:
python -m venv venvActivate it on Windows:
venv\Scripts\activateInstall dependencies:
pip install -r requirements.txtAuthenticate with Google Cloud:
gcloud auth application-default loginRun the application from the project root:
python -m app.mainThe basic Text-to-SQL pipeline is working:
Question
↓
Gemini
↓
SQL
↓
Validation
↓
SQLite
↓
Result
- Improve SQL validation and database safety
- Add SQL error correction/retry
- Test the system with a larger set of questions
- Add Text-to-SQL evaluation metrics
- Add a simple web interface
- Deploy the application