Overview
The@deepagents/text2sql package converts natural language questions into validated, executable SQL queries. It supports multiple databases, domain knowledge injection, conversational context, and is safe by default.
Installation
- PostgreSQL
- SQLite
- SQL Server
Quick Example
Core API
new Text2Sql(config)
Creates a text-to-SQL instance.
toSql(question)
Convert a question to SQL:
chat(messages, options)
Conversational multi-turn interactions:
explain(sql)
Convert SQL back to natural language:
Database Adapters
PostgreSQL
SQLite
SQL Server
Grounding Functions
Control what schema metadata the AI receives:| Function | Description |
|---|---|
tables() | Tables, columns, primary keys |
views() | Database views |
info() | DB version and information |
indexes() | Index hints for performance |
constraints() | Foreign keys and constraints |
rowCount() | Table sizes (tiny/small/medium/large/huge) |
columnStats() | Min/max/null distribution |
lowCardinality() | Enum-like columns with distinct values |
Domain Knowledge
Inject business context using fragments:term- Business vocabularyhint- Behavioral rulesguardrail- Safety boundariesexample- Q&A pairsexplain- Concept explanationsclarification- When to ask for more infoworkflow- Multi-step processesquirk- Data edge casesstyleGuide- Query style preferencesanalogy- Concept comparisonsglossary- Term-to-expression mapping
Conversations
Build multi-turn conversational interfaces:- Conversation history
- Generated queries
- User preferences
- Clarifications
Safety Features
Read-Only Queries
By default, only SELECT queries are allowed:Validation
Queries are validated before execution:Custom Guardrails
Model Selection
Works with any AI SDK provider:- Groq (Recommended)
- OpenAI
- Anthropic
Advanced Usage
Custom Context Store
Version Management
Use versioning for schema changes:Package Info
- Version: 0.23.0
- License: MIT
- Repository: github.com/JanuaryLabs/deepagents
- Package: @deepagents/text2sql
Related Packages
@deepagents/context
Fragment builders for domain knowledge
@deepagents/evals
Evaluate SQL accuracy