Explainability
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.SQL Validation
Before executing any query, Text2SQL validates it using the database’sEXPLAIN command:
Validation Process
- Syntax Check - Ensures query is syntactically valid
- Schema Check - Verifies tables and columns exist
- Type Check - Validates data type compatibility
- Performance Check - Uses EXPLAIN to estimate query cost
Error Explanations
When queries fail, Text2SQL provides helpful error messages with recovery suggestions:Error Types
MISSING_TABLE
INVALID_COLUMN
SYNTAX_ERROR
INVALID_FUNCTION
Automatic Error Recovery
The conversational interface automatically retries failed queries with corrections:Retry Strategy
- Classify Error - Determine error type (syntax, missing table, invalid column)
- Gather Context - Re-check schema, review query structure
- Apply Fix - Targeted correction based on error type
- Validate - Re-validate before executing
- Execute - Retry with corrected query
Query Explanations
The conversational interface provides plain English summaries of query results:Explanation Features
- Result Summary - High-level overview of what the query returned
- Key Comparisons - Highlights interesting patterns in the data
- Follow-Up Suggestions - Recommends related queries
Reasoning Framework
Text2SQL uses a meta-cognitive reasoning framework to make decisions transparent:1. Logical Dependencies
Before generating SQL:2. Risk Assessment
3. Abductive Reasoning
When queries fail:4. Precision and Grounding
SQL to English
Convert existing SQL to natural language explanations:Query Formatting
Text2SQL automatically formats SQL according to the database dialect:Formatting Features
- Indentation - Consistent 2-space indentation
- Keyword Casing - SQL keywords in UPPERCASE
- Line Breaks - Logical breaks between clauses
- Alignment - Aligned column lists and conditions
Debugging Queries
Inspect Schema Context
See what schema information the AI has:Inspect Conversation Context
See the full conversation history:Enable Verbose Logging
Token Usage Tracking
Monitor AI token consumption:Usage Metadata
Performance Metrics
Track query execution time:Confidence Scores
The system can indicate confidence in its query generation:Confidence Indicators
- High - Clear schema match, unambiguous query
- Medium - Some ambiguity resolved via reasoning
- Low - Multiple assumptions made, clarification recommended
Query Plan Visualization
Visualize the query execution plan:Best Practices
1. Review Generated Queries
Always review SQL before executing in production:2. Validate Assumptions
Check the AI’s assumptions:3. Use Teachables for Clarity
4. Monitor Error Patterns
Track common errors to improve teachables:5. Provide Feedback Loops
Allow users to correct the AI:Complete Example
explainability-demo.ts
Next Steps
Database Adapters
Learn about adapter-specific features
Core API
Full Text2SQL API reference