Text2SQL provides transparency into how queries are generated, validated, and executed. The system can explain SQL queries in plain English and provide reasoning for its decisions.
When queries fail, Text2SQL provides helpful error messages with recovery suggestions:
try { const sql = await text2sql.toSql('Show me data from invalid_table');} catch (error) { console.error(error);}
Error format:
{ "error": "no such table: invalid_table", "error_type": "MISSING_TABLE", "suggestion": "Check the database schema for the correct table name. The table you referenced does not exist.", "sql_attempted": "SELECT * FROM invalid_table"}
The conversational interface provides plain English summaries of query results:
const stream = await text2sql.chat([ { role: 'user', content: 'Show me top 10 customers by revenue' }]);for await (const chunk of stream) { if (chunk.type === 'text') { console.log(chunk.content); // "Here are the top 10 customers by total revenue: // 1. Acme Corp - $125,000 // 2. Global Industries - $98,500 // ..." }}
Applying Principle 1 (Logical dependencies):- Need: schema to know which table has sales data- Need: clarify "last month" = calendar month or rolling 30 days?Action: Ask user for date range clarification BEFORE generating SQL.
Applying Principle 2 (Risk assessment):- Consequence: Unbounded query may return millions of rows- Risk: High - could impact database performanceAction: Add LIMIT clause or ask user to specify date range.
Applying Principle 3 (Abductive reasoning):- Hypothesis 1 (most likely): Filter too restrictive- Hypothesis 2: Data doesn't exist for that period- Hypothesis 3: JOIN eliminated matching rowsTesting hypotheses:1. Remove filters one by one to isolate the issue2. Check date range actually has data3. Run subqueries separately to verify each table
Applying Principle 6 (Precision and Grounding):- User asked for "active customers"- Schema shows status column with values: 'active', 'inactive', 'pending'- Sample data confirms lowercase valuesAction: Use status = 'active' (lowercase, exact match).
Convert existing SQL to natural language explanations:
import { toExplanation } from '@deepagents/text2sql';const sql = ` SELECT customers.name, COUNT(orders.id) AS order_count, SUM(orders.amount) AS total_revenue FROM customers LEFT JOIN orders ON customers.id = orders.customer_id WHERE orders.created_at >= '2024-01-01' GROUP BY customers.id HAVING total_revenue > 1000 ORDER BY total_revenue DESC LIMIT 10`;const explanation = await toExplanation(sql, { adapter });console.log(explanation);
Output:
This query shows the top 10 customers by revenue in 2024:1. Retrieves customer names from the customers table2. Counts orders for each customer3. Sums the total revenue per customer4. Filters to orders created on or after January 1, 20245. Groups results by customer6. Includes only customers with revenue exceeding $1,0007. Sorts by revenue (highest first)8. Limits to top 10 results
const stream = await text2sql.chat([ { role: 'user', content: 'Show me active users' }]);// If AI assumes status = 'active', verify that's correct:console.log('Assumption: status = "active" means active users');