Natural Language to SQL with LlamaIndex & Azure OpenAI

Blogs

A Developer’s Guide to FastAPI and Flask
June 14, 2025
Google’s A2A Protocol: The Future of AI Agent Collaboration
June 14, 2025

Natural Language to SQL with LlamaIndex & Azure OpenAI

Accessing structured data from databases traditionally requires knowledge of SQL and familiarity with the underlying schema. This creates a barrier for non-technical users who need timely insights but lack technical expertise.

Recent advancements in Large Language Models (LLMs) and tools like LlamaIndex have enabled a new approach—allowing users to interact with databases using natural language. By combining LlamaIndex with Azure OpenAI and SQLAlchemy, developers can build intelligent interfaces that translate plain English into accurate SQL queries, execute them, and return meaningful results.

This blog demonstrates how to set up a simple Text-to-SQL application using LlamaIndex’s NLSQLTableQueryEngine, making structured data easily accessible through conversational language.

What is LlamaIndex?

LlamaIndex (formerly known as GPT Index) is a data framework for LLM applications, designed to help developers build powerful interfaces between large language models (LLMs) and private or structured data sources like documents, databases, and APIs.

Text-to-SQL with LlamaIndex

Text-to-SQL allows users to ask natural language questions and automatically convert them into valid SQL queries. LlamaIndex makes this possible using a Query Engine and optional Retrievers that:

  • Understand the question
  • Choose the right table(s)
  • Convert it to SQL
  • Execute the SQL over a live database
  • Return structured results to the user

Components of Text-to-SQL in LlamaIndex:

  • Query Engine: Converts natural language into SQL and runs it
  • Retriever: Dynamically selects relevant tables, rows, or columns for accurate context
  • Table Index: Indexes the schema to improve table selection during query-time

Code Setup Breakdown

Let’s look at how to implement a simple Text-to-SQL pipeline using Azure OpenAI, LlamaIndex, and a Microsoft SQL Server.

Step 1: Environment Setup

os.environ["OPENAI_API_KEY"] = "your_api_key"
os.environ["AZURE_OPENAI_ENDPOINT"] = "end_point"
os.environ["OPENAI_API_VERSION"] = "api_version"

This configures your Azure OpenAI connection so the LLM can be used for query generation.

Step 2: Initialize Azure OpenAI LLM

llm = AzureOpenAI(engine="deployment_name", model="model_name", temperature=0.0)

We use llama_index.llms.azure_openai.AzureOpenAI to instantiate a deterministic LLM client via Azure.

Step 3: Connect to the SQL Database

connection_string = f"mssql+pymssql://{sqluser}:{sqlpassword}@server_name/database_name"
engine = create_engine(connection_string)

This sets up a SQLAlchemy engine to connect to your Microsoft SQL database.

Step 4: Initialize SQLDatabase

sql_database = SQLDatabase(engine, include_tables=["table_1", "table_2"])

We wrap the database connection using SQLDatabase, limiting access to specific tables (which is good for both performance and security).

Step 5: Create the Query Engine

query_engine = NLSQLTableQueryEngine(sql_database=sql_database, llm=llm)

The NLSQLTableQueryEngine enables natural language queries by translating them to SQL based on schema introspection.

Step 6: Process Queries

response = query_engine.query("your natural language query")

This takes the input question, converts it to SQL, queries the database, and returns the results.

Conclusion

By combining LlamaIndex, Azure OpenAI, and SQLAlchemy, you can build a seamless Natural Language to SQL interface that:

  • Simplifies access to structured data
  • Removes the need for SQL knowledge
  • Is extensible with retrievers and schema indexing

This setup is ideal for internal analytics tools, chatbots for database queries, or any application where users need answers from structured data—just by asking in plain English.


Sanjay N

Leave a Reply

Your email address will not be published. Required fields are marked *