Manuscript Offprint

arXiv:2610.22968

NL2SQL with Iterative Self-Refinement

Anjali Vemula, Akshat Dasula

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

Abstract

Bridging the gap between human intent and machine logic remains a critical challenge in data access for non-technical users, yet Large Language Models (LLMs) remain unreliable: they hallucinate schema elements, generate syntactically invalid queries, or fail to capture user intent. These limitations are especially critical in domains such as insurance, where KPIs, regulatory constraints, and legacy relational systems demand high precision. We introduce a multi-agent, persona-driven NL2SQL framework that combines (1) domain relevance detection, (2) schema-grounded knowledge construction, (3) KPI-aware SQL synthesis, (4) syntax-level self-repair using sqlite execution feedback, and (5) semantic consistency checking via SQL-to-NL reverse engineering. Our system significantly reduces query hallucination and improves semantic fidelity compared to direct LLM prompting. Evaluated on 60 queries (50 from a HuggingFace text-to-SQL dataset and 10 custom domain queries), the system achieves 90% correctness using Mistral across seven coordinated LLM calls showing that this approach can substantially increase the reliability of NL2SQL pipelines in high-stakes environments.

1 Introduction

The insurance industry operates on complex, data-intensive processes that rely heavily on structured information about policies, claims, customers, agents, and submissions. These datasets carry critical business functions, including pricing, underwriting, fraud detection, portfolio analysis, and KPI-driven performance monitoring. Although insurers store decades of transactional data in relational databases, this information is often locked behind SQL interfaces accessible only to technically trained analysts. As a result, underwriters, financial planners, and operations teams depend on technical intermediaries to obtain even basic metrics, delaying decisions in a domain where timing and accuracy are crucial.

Despite increasing adoption of AI tools, querying these legacy systems remains challenging [13][13]. Their schemas are domain-specific, include numerous interlinked tables, and require precise understanding of insurance KPIs such as loss ratios or claim frequencies. Modern LLMs like ChatGPT can translate natural language into SQL, but they frequently hallucinate table or column names, produce syntactically invalid queries, or generate outputs misaligned with business intent. Such unreliability makes them unsuitable for high-stakes industries like insurance, where incorrect queries can lead to flawed reports, mispriced risk, or regulatory compliance issues.

To address these challenges, we develop a multi-agent, persona-driven NL2SQL system tailored for insurance databases. Our approach integrates (1) domain-relevance validation to ensure user queries align with insurance concepts (2) schema-grounded knowledge generation [7], including KPI decomposition (3) iterative SQL correction using sqlite-based syntax checks and (4) semantic validation via SQL-to-NL reverse engineering paired with LLM-as-a-judge comparison. By combining domain expertise, guided decomposition, and error-correcting feedback loops, our system produces SQL that is both syntactically correct and semantically faithful to user intent. Through this structured methodology, we demonstrate that reliable natural-language querying of legacy insurance databases is achievable, overcoming the limitations of direct LLM prompting and enabling decision-makers to access critical insights without technical barriers.