Reliable Answers for Recurring Questions: Boosting Text-to-SQL Accuracy with Template Constrained Decoding

arXiv cs.CL / 5/1/2026

📰 NewsDeveloper Stack & InfrastructureModels & Research

Key Points

  • The paper tackles a key limitation of LLM-based Text-to-SQL in real deployments: inconsistent accuracy and the tendency to generate invalid SQL, especially for complex or unseen database schemas.
  • It proposes Template Constrained Decoding (TeCoD), which learns reusable NL-to-SQL templates from historical labeled workloads and uses a template selection module based on a fine-tuned natural language inference model.
  • After selecting an appropriate template, TeCoD generates SQL with grammar-constrained decoding to strictly enforce the template structure and ensure syntactic validity.
  • The authors report that TeCoD achieves up to 36% higher execution accuracy than in-context learning (ICL) and reduces latency by 2.2× on matched queries, while maintaining efficiency through a partitioned decoding strategy.

Abstract

Large language models (LLMs) have revolutionized Text-to-SQL generation, allowing users to query structured data using natural language with growing ease. Yet, real-world deployment remains challenging, especially in complex or unseen schemas, due to inconsistent accuracy and the risk of generating invalid SQL. We introduce Template Constrained Decoding (TeCoD), a system that addresses these limitations by harnessing the recurrence of query patterns in labeled workloads. TeCoD converts historical NL-SQL pairs into reusable templates and introduces a robust template selection module that uses a fine-tuned natural language inference model to match or reject queries efficiently. Once the template is selected, TeCoD enforces it during SQL generation through grammar-constrained decoding, implemented via a novel partitioned strategy that ensures both syntactic validity and efficiency. Together, these components yield up to 36% higher execution accuracy than in-context learning (ICL) and 2.2x lower latency on matched queries.