Building a Production-Ready AI Query Engine with Text-to-SQL and Function Calling
The author describes their experience building a production AI query engine that combines text-to-SQL and function calling approaches to enable non-technical users to ask questions in plain English and get real answers instantly.
Why it matters
This article provides a practical, production-ready approach to leveraging AI language models for analytical queries and actions in a sensitive, mission-critical environment.
Key Points
- 1Mistake #1: Starting with function calling alone for analytical queries led to issues with complexity and accuracy
- 2Mistake #2: Using text-to-SQL alone raised concerns about security risks in a production database
- 3The solution: Combining text-to-SQL (the brain) and function calling (the hands) with a router pattern and a multi-layer SQL validation process
- 4The AST validator is the real safety layer, using regex filtering, AST parsing, and a table allowlist to ensure only valid SQL is executed
- 5The MCP (function calling) layer allows controlled execution of predefined actions with human-in-the-loop approval for sensitive operations
Details
The author worked with an affiliate marketing company that had a 28-table MySQL database with valuable data, but non-technical users struggled to get answers to complex questions that required SQL. The goal was to enable these users to ask questions in plain English and get instant, accurate answers without relying on developers or dashboards. Initially, the author tried a function calling approach, but found it quickly broke down for analytical queries with complex requirements. Switching to text-to-SQL solved the flexibility and accuracy issues, but raised concerns about security risks in a production database. The solution was to combine both approaches - text-to-SQL for analytical queries (the brain) and function calling for actions (the hands), with a router pattern to determine which path to take. The real safety layer is the AST (Abstract Syntax Tree) validator, which uses a multi-step process (regex filtering, AST parsing, and a table allowlist) to ensure only valid SQL is executed, even if the language model generates something potentially malicious. For actions, the author uses a controlled MCP (function calling) layer, where the language model can only call predefined tools, and sensitive operations require human approval before execution. This pattern is used across 18 production workflows. The author shares lessons learned, including the importance of the multi-layer validation approach, the value of clear schema semantics, and ideas for future improvements like automated accuracy tracking and dynamic context reduction.
No comments yet
Be the first to comment