Introduction to Text2SQL
@deepagents/text2sql transforms natural language questions into validated, executable SQL queries. It’s designed for production use with comprehensive safety guardrails, domain knowledge injection, and multi-turn conversational support.
What is Text2SQL?
Text2SQL is an AI-powered system that:- Converts natural language to SQL - Ask questions in plain English, get executable queries
- Understands your database - Automatic introspection of tables, relationships, indexes, and constraints
- Learns your domain - Inject business terminology, rules, and patterns
- Supports conversations - Multi-turn dialogs with context persistence
- Stays safe - Read-only queries, validation, and configurable guardrails
Key Features
Natural Language to SQL
Convert questions to validated, executable queries:Multi-Database Support
Supports major database systems with native adapters:- PostgreSQL - Full support including schemas, indexes, and constraints
- SQLite - Lightweight embedded database support
- SQL Server - Enterprise database with T-SQL dialect
- MySQL/MariaDB - Popular open-source databases
- BigQuery - Google Cloud data warehouse
Schema-Aware
Automatic introspection discovers:- Tables, columns, and data types
- Primary keys and foreign key relationships
- Indexes for performance hints
- Constraints (UNIQUE, CHECK, NOT NULL)
- Column statistics (min/max, null fraction, cardinality)
- Row counts and table sizes
Teachables
Inject domain knowledge to improve accuracy:term, hint, guardrail, example, explain, clarification, workflow, quirk, styleGuide, analogy.
Conversational
Build multi-turn conversations with context:Explainable
The system provides:- SQL validation before execution
- Error explanations with recovery suggestions
- Query result summaries in plain English
- Reasoning transparency
Safe by Default
- Read-only queries - Only SELECT and WITH statements allowed
- Validation - Queries validated before execution
- Guardrails - Configurable safety rules
- Error recovery - Automatic retry with intelligent fixes
Architecture
Text2SQL is built on three layers:- Adapters - Database-specific implementations for introspection and execution
- Context Engine - Manages schema fragments, teachables, and conversation history
- AI Agent - Coordinates SQL generation, validation, execution, and explanation
How It Works
- Introspection - Adapter scans database schema (cached)
- Context Building - Combines schema + teachables + conversation history
- SQL Generation - AI agent generates query based on context
- Validation - Query validated using EXPLAIN
- Execution - Query executed and results returned
- Explanation - Results summarized in natural language
Use Cases
- Business Intelligence - Natural language dashboards and reports
- Data Exploration - Ad-hoc queries without SQL knowledge
- Customer Support - Self-service data access
- Documentation - Explain complex queries in plain English
- Training Data Generation - Create question-SQL pairs from existing queries
Next Steps
Installation
Install the package and database adapters
Getting Started
Write your first natural language query
Database Adapters
Choose and configure your database adapter
Teachables
Inject domain knowledge to improve accuracy