v1.0

v1.0

/

/

Text-to-SQL with LLMs: architecture, techniques, evaluation and production best practices

Text-to-SQL with LLMs: architecture, techniques, evaluation and production best practices

Text-to-SQL with LLMs: architecture, techniques, evaluation and production best practices

Production Text-to-SQL is not question → LLM → SQL. Reliable systems retrieve the right schema, add business semantics, generate dialect-aware SQL, validate it deterministically, enforce permissions and evaluate the result. This guide explains the complete architecture and how to benchmark it.

Aucun heading trouvé dans #cible

Adil Harcha

Text-to-SQL with LLMs is easy to demo and difficult to trust.

A language model can often produce syntactically valid SQL from a natural-language question. Production systems fail for harder reasons: the wrong table was selected, a business metric was interpreted incorrectly, a join duplicated rows, a user asked an ambiguous question, a restricted column was exposed or a plausible query answered a different question than the one intended.

That changes the engineering problem.

The production architecture is not simply:

question → LLM → SQL

It is closer to:



This guide explains that full pipeline, the techniques that improve accuracy, how to evaluate a text-to-SQL system, the security controls required before execution and how LLM-based SQL generation fits inside a data agent.

What is text-to-SQL with LLMs?

Text-to-SQL with LLMs is the use of a large language model to translate a natural-language analytical request into a SQL query against a known database schema.

A user might ask:

“Which product categories lost the most gross margin in France last quarter compared with the previous quarter?”

The system must identify the relevant business concepts, locate the appropriate tables and columns, understand the SQL dialect, generate the query and execute it safely.

That is one implementation of natural language querying, but NLQ is broader: it can target documents, APIs, semantic models or other analytical tools, not only SQL databases.

Why LLMs changed text-to-SQL

Earlier text-to-SQL systems depended heavily on task-specific models and tightly scoped schemas. General-purpose LLMs improved the ability to reason over schemas, instructions, examples and business language without retraining a model for every database.

But the model still does not know your organization’s data semantics by default.

It may know what “revenue” usually means. It does not know whether your company defines net revenue before or after rebates, which table is authoritative or whether cancelled orders must be excluded.

That gap between linguistic competence and business semantics is where most production architecture lives.


Let business users query governed data without exposing raw schema complexity

thaink² Data Analyst Agent combines natural-language questions, governed data access and analytical workflows rather than treating SQL generation as a standalone prompt.


A production text-to-SQL architecture

1. User intent and ambiguity detection

Before touching the schema, determine whether the question is answerable as written. “Top customers last month” is ambiguous if “top” could mean revenue, margin, order count or retention.

A good system asks a clarification question when ambiguity changes the query materially.

2. Schema retrieval and linking

Large databases can contain thousands of tables and columns. Sending the whole schema to the model is expensive and often reduces accuracy.

Instead, retrieve the most relevant datasets, tables, columns, relationships and examples for the specific question.

Google Cloud explicitly describes intelligent retrieval and ranking of datasets, tables and columns as a core technique for production text-to-SQL.

3. Semantic context

The model needs business meaning, not just SQL metadata.

Useful context includes:

  • metric definitions;

  • approved dimensions;

  • join paths;

  • synonyms;

  • time semantics;

  • row-level restrictions;

  • examples of verified questions and queries.

Snowflake’s semantic views illustrate this pattern: logical tables, dimensions, facts, metrics and relationships give the model a business-friendly layer above raw database structure.

4. SQL generation

The LLM receives the user question, relevant schema, business rules, dialect instructions and examples, then proposes SQL.

Generation can use a single model call, multiple candidates, self-consistency or a planner/generator split for complex questions.

5. Deterministic validation

Do not execute generated SQL because the model says it looks correct.

Validate with deterministic tools where possible:

  • SQL parser or AST;

  • read-only statement allowlist;

  • table and column allowlists;

  • dialect validation;

  • dry run or EXPLAIN;

  • query-cost or scan limits;

  • timeout limits.

Google Cloud specifically notes that parsing and dry runs complement LLM validation by giving deterministic signals when generation fails.

6. Permission enforcement

Authorization belongs below the model. The SQL execution identity should see only the tables, rows and columns the user is permitted to access.

Do not ask the LLM to remember that a salary column is sensitive and hope it complies.

7. Safe execution

Execute against a read-only path with time, row and resource limits appropriate to the workload.

8. Result validation

A query can execute successfully and still answer the wrong question. Inspect row counts, null patterns, unexpected magnitudes and whether the result actually matches the requested grain.

9. Repair loop

If validation or execution fails, return the error and relevant context to the model for a bounded retry rather than starting the entire workflow from scratch.


Production Text-to-SQL pipeline connecting a natural-language question, schema context, SQL generation, validation and a verified result.

Schema linking is usually the first hard problem

A model cannot write the right query if it is reasoning over the wrong part of the schema.

Schema linking maps words in the question to database entities. “Customers who renewed” might map to an account table, a contract table, a subscription status field and a renewal event table.

Useful retrieval signals include table and column descriptions, embeddings, lexical matching, query history, foreign keys, data samples and business glossary terms.

Use a semantic layer when business logic is not obvious from SQL

The semantic layer is often the difference between technically valid SQL and an answer the business recognizes.

It can encode:

  • the official formula for gross margin;

  • which date represents recognized revenue;

  • how active customers are defined;

  • valid join relationships;

  • which filters are implicit;

  • which metrics should never be summed.

For broader analytical workflows, this same semantic layer also supports agentic analytics.

Prompting techniques that still matter

Dialect instructions

PostgreSQL, BigQuery, Snowflake and SQL Server differ. Tell the model which dialect it must produce.

Verified examples

A small set of representative question/SQL pairs can teach organization-specific conventions more effectively than generic demonstrations.

Business rules

Include only the rules relevant to the current query instead of growing one giant system prompt.

Output contracts

Ask for structured fields such as assumptions, SQL, referenced tables and confidence rather than free-form prose when the next step is machine validation.

Should you generate one SQL query or several?

For difficult questions, generating multiple candidates can improve robustness.

A self-consistency approach can produce several candidate queries, validate them, compare execution behavior and select the strongest one.

The trade-off is latency and cost. Use it selectively for high-value or complex queries rather than every request.

Validation: syntax is the easy part

A parser can tell you whether SQL is valid. It cannot tell you whether “revenue” was interpreted correctly.

Production validation should cover several layers:

Layer

Question

Syntax

Can the database parse the SQL?

Security

Is the statement read-only and within allowed objects?

Cost

Could the query scan or consume too much?

Schema

Are tables, columns and joins valid?

Semantic

Does the query implement the intended business meaning?

Result

Does the returned data make sense for the question?

Text-to-SQL security: treat generated SQL as untrusted code

The safest default is simple: the model proposes a query; policy decides whether it can run.

Use:

  • read-only database credentials;

  • row- and column-level security;

  • statement allowlists;

  • query timeouts;

  • scan / compute budgets;

  • blocked system schemas;

  • auditable execution logs.

Prompt injection is also relevant when schema descriptions, examples or retrieved metadata can contain untrusted text. Treat retrieved context as data, not instructions.

How to evaluate a Text-to-SQL system

A benchmark score is useful, but production evaluation needs your own questions, schema and business rules.

Execution accuracy

Does the generated query return the expected result?

Semantic correctness

Does the query answer the intended business question, not merely produce matching rows by accident?

Schema-linking accuracy

Did the system retrieve the right tables and columns?

Safety pass rate

How often does the proposed query satisfy execution policy without manual repair?

Clarification quality

Does the system ask when ambiguity matters?

Repair success rate

When a query fails, can the system correct it within a bounded number of retries?

Latency and cost

Measure the entire workflow, including schema retrieval, model calls, validation and database execution.

What Spider and BIRD can and cannot tell you

Academic benchmarks remain valuable for measuring progress, but they do not reproduce your warehouse, naming conventions, permissions or metric definitions.

BIRD’s 2026 leaderboard still shows a meaningful gap between human performance and leading systems on execution accuracy, even as model performance improves. That is a useful reminder that benchmark leadership is not the same as production reliability.

Use public benchmarks for model screening. Use an internal evaluation set for deployment decisions.


Text-to-SQL evaluation comparing generated queries for execution accuracy, semantic correctness and safety.

Build your evaluation set from real analyst questions

Collect questions that analysts and business users actually ask, then include:

  • simple single-table questions;

  • multi-table joins;

  • time comparisons;

  • business metrics;

  • ambiguous questions that require clarification;

  • unanswerable requests;

  • permission-sensitive questions;

  • queries that are expensive if generated badly.

Freeze a representative test set before changing prompts or models so you can measure regressions rather than rely on memorable demos.

Model choice matters less than system design after a point

Stronger models generally help with difficult schemas and reasoning, but architecture can produce larger gains than simply upgrading the model.

Schema retrieval, verified examples, semantic context, deterministic validation and a repair loop can make a medium-cost model outperform a stronger model used with a weak prompt.

That also creates room for model routing: easy queries can use a cheaper model while complex or low-confidence queries escalate.

Multi-turn Text-to-SQL

Real analysis is conversational.

A user asks “Show revenue by region,” then “Only enterprise customers,” then “Compare that with last year.”

The system must preserve analytical context without blindly copying every previous assumption. Track explicit filters, metric choices and time ranges as structured state where possible.

Text-to-SQL inside an agentic analytics workflow

SQL generation becomes more powerful when it is one tool inside a broader analytical agent.

A Data Analyst Agent can decide that one question needs SQL, another needs document retrieval and a third needs a calculation or external API. It can then inspect the intermediate result and choose the next analytical action.

That is the difference between a text-to-SQL feature and an analytical workflow.


Use SQL generation as a governed analytical tool

Connect natural-language questions to databases through an agent that can retrieve schema, validate queries and choose when SQL is the right tool.


Common production failure modes

Wrong table, right-looking answer. The query executes but uses a stale or non-authoritative dataset.

Join explosion. Many-to-many joins inflate totals without causing a SQL error.

Metric drift. The model reconstructs a KPI instead of using the governed definition.

Ambiguity ignored. The system guesses instead of asking one clarification question.

Permission leakage. Generated SQL reaches a column the user should not see.

Expensive query. A valid query scans far more data than expected.

Silent repair loops. The system retries indefinitely and hides cost or latency.

A 10-step production checklist

  1. Define the analytical workflows and users.

  2. Identify authoritative schemas and metrics.

  3. Build schema retrieval instead of sending the entire warehouse.

  4. Add semantic definitions and verified examples.

  5. Generate SQL for the correct dialect.

  6. Parse and validate before execution.

  7. Execute with read-only, permission-aware credentials.

  8. Validate results and support bounded repair.

  9. Create a representative evaluation harness.

  10. Monitor failures, latency, cost and model changes in production.

Where thaink² and aPowerB fit

thaink² uses Text-to-SQL as one capability inside broader data-agent workflows. The Data Analyst Agent can work with structured enterprise data while preserving the surrounding analytical context, and the Text-to-SQL solution focuses on natural-language access to SQL systems.

aPowerB provides an open-source runtime for developers who want to compose model calls, database tools, retrieval and validation steps into their own agent workflows.

In both cases, the principle is the same: generated SQL should be treated as a proposed action that must pass deterministic policy before execution.


Build natural-language analytics around governed execution

The practical takeaway

The best Text-to-SQL systems do not ask the LLM to solve the entire problem.

They use the model where language and reasoning help, then surround it with retrieval, semantic definitions, deterministic validation, permissions, safe execution and evaluation.


Frequently asked questions

What is Text-to-SQL with LLMs?

It is the use of a large language model to translate a natural-language analytical request into SQL against a known database schema, typically with schema context, validation and execution controls around the model.

What is the best LLM for Text-to-SQL?

The best model depends on schema complexity, latency, cost and evaluation results. In production, schema retrieval, semantic context and validation can matter as much as the model itself.

How do you make Text-to-SQL accurate?

Retrieve the relevant schema, provide business semantics and verified examples, generate dialect-specific SQL, validate deterministically, execute safely and evaluate on real organization-specific questions.

Is Text-to-SQL safe for production databases?

It can be when generated SQL is treated as untrusted code: use read-only credentials, row and column security, allowlists, dry runs, timeouts, cost limits and auditable execution.

How should Text-to-SQL be evaluated?

Measure execution accuracy, semantic correctness, schema linking, safety pass rate, clarification quality, repair success, latency and cost on a representative internal test set.

An open-source foundation for building your own agents.

ApowerB is the open-source agentic framework developed by thaink² to build, orchestrate, and operate AI agents on your own stack, using your models, tools, and data.

What if your next analysis were ready before your next meeting?

Show us your environment and a use case. See how thaink² can query, monitor, and act on your data.

Our mission
Our vision

Move from data you look at to data that works continuously for your business.

We believe the next generation of Data platforms will do more than show what happened. They will monitor, explain, anticipate, and prepare decisions before some questions even need to be asked.

Image

What is an Agentic Data Platform?

An Agentic Data Platform uses specialized AI agents to work with enterprise data. Unlike a static dashboard or a simple chatbot, agents can explore multiple sources, run analyses, generate visualizations, produce deliverables, and execute recurring missions.

What’s the difference between Proactive Mode and Exploratory Mode?

In Exploratory Mode, the user asks a question and thaink² runs the analysis on demand. In Proactive Mode, a mission is defined in advance: agents monitor the data based on a set schedule or specific conditions and automatically deliver the relevant results.

Do I need to know SQL to use thaink²?

Not for business use cases. Users can ask questions in natural language and get analyses, tables, charts, and dashboards without writing SQL queries themselves.

What data sources can thaink² connect to?

The platform is designed to work with the systems already in place across your organization: databases, data warehouses, ERP, CRM, APIs, files, and document repositories. Available connectors depend on your environment and use case.

What types of outputs can the agents produce?

Depending on the agent and use case, outputs can include analyses, tables, charts, dashboards, forecasts, alerts, summaries, and reports. The goal is to deliver an actionable result, not just a text response.

Can thaink² be deployed on our own infrastructure?

thaink² supports deployment options designed around enterprise requirements, including organizations that need greater control over their infrastructure, models, and data. Our teams help define the architecture that best fits the project.

Which part of the thaink² ecosystem is open source?

ApowerB is the open-source agentic framework developed by thaink². It enables technical teams to build and operate their own agents, with support for RAG, Text-to-SQL, multi-LLM setups, and APIs.

How do I get started with thaink²?

The easiest way to start is with a concrete use case: reporting, analysis, forecasting, document search, or monitoring. An initial session helps define the available data, expected outcomes, and the most suitable deployment approach.

Join our Discord community

Connect with builders, Data teams, and AI practitioners. Share ideas, get technical help, discuss agentic architectures, and follow the latest developments around ApowerB.

Our newsletter (Soon)

Data that takes action, straight to your inbox.

Field insights, agentic architectures, benchmarks, use cases, and the latest from ApowerB. Only what’s worth reading.

© 2026 thaink² SAS — All rights reserved

An open-source foundation for building your own agents.

ApowerB is the open-source agentic framework developed by thaink² to build, orchestrate, and operate AI agents on your own stack, using your models, tools, and data.

What if your next analysis were ready before your next meeting?

Show us your environment and a use case. See how thaink² can query, monitor, and act on your data.

Our mission
Our vision

Put the power of data in the hands of decision-makers.

Today, too many business questions still depend on an export, a dashboard, or the availability of a Data team. thaink² changes that by letting teams query their data directly, while agents handle the analysis and recurring work.

Image

What is an Agentic Data Platform?

An Agentic Data Platform uses specialized AI agents to work with enterprise data. Unlike a static dashboard or a simple chatbot, agents can explore multiple sources, run analyses, generate visualizations, produce deliverables, and execute recurring missions.

What’s the difference between Proactive Mode and Exploratory Mode?

In Exploratory Mode, the user asks a question and thaink² runs the analysis on demand. In Proactive Mode, a mission is defined in advance: agents monitor the data based on a set schedule or specific conditions and automatically deliver the relevant results.

Do I need to know SQL to use thaink²?

Not for business use cases. Users can ask questions in natural language and get analyses, tables, charts, and dashboards without writing SQL queries themselves.

What data sources can thaink² connect to?

The platform is designed to work with the systems already in place across your organization: databases, data warehouses, ERP, CRM, APIs, files, and document repositories. Available connectors depend on your environment and use case.

What types of outputs can the agents produce?

Depending on the agent and use case, outputs can include analyses, tables, charts, dashboards, forecasts, alerts, summaries, and reports. The goal is to deliver an actionable result, not just a text response.

Can thaink² be deployed on our own infrastructure?

thaink² supports deployment options designed around enterprise requirements, including organizations that need greater control over their infrastructure, models, and data. Our teams help define the architecture that best fits the project.

Which part of the thaink² ecosystem is open source?

ApowerB is the open-source agentic framework developed by thaink². It enables technical teams to build and operate their own agents, with support for RAG, Text-to-SQL, multi-LLM setups, and APIs.

How do I get started with thaink²?

The easiest way to start is with a concrete use case: reporting, analysis, forecasting, document search, or monitoring. An initial session helps define the available data, expected outcomes, and the most suitable deployment approach.

Join our Discord community

Connect with builders, Data teams, and AI practitioners. Share ideas, get technical help, discuss agentic architectures, and follow the latest developments around aPowerB.

Our newsletter (Soon)

Data that takes action, straight to your inbox.

Field insights, agentic architectures, benchmarks, use cases, and the latest from ApowerB. Only what’s worth reading.

© 2026 thaink² SAS — All rights reserved