CAPER: Clause-Aligned Process Supervision for Text-to-SQL

Lujie Ban, Jiasheng Shi, Jinyang Li, Xiaolin Han, Tsz Nam Chan, Chenhao Ma

TL;DR

CAPER introduces a novel clause-level process supervision framework for Text-to-SQL that automatically localizes root-cause errors via counterfactual interventions on the SQL abstract syntax tree. By training a lightweight process reward model (CAPER-9B) on this clause-aligned data, it provides dense, semantically grounded feedback for reinforcement learning and candidate verification. This approach yields up to a 15.3% relative execution accuracy improvement over GPT-5.4 and achieves excellent failure localization on complex benchmarks.

What Problem Does This Paper Address?

Large Language Models (LLMs) have become remarkably good at translating natural language questions into executable SQL queries. However, training them efficiently remains difficult due to a fundamental credit assignment problem.

Currently, Text-to-SQL systems are optimized using one of two reward granularities, both of which have major drawbacks:

  1. Execution-level (Outcome) Rewards: This binary, terminal signal only tells the model whether the final query returned the correct answer. It provides zero guidance on which intermediate decision (e.g., a wrong join key or an incorrect predicate) caused a failure.
  2. Token-level Rewards: This dense feedback operates at the level of individual words or characters. However, SQL tokens do not align cleanly with semantic decisions. Token-level rewards often diffuse credit across low-level symbols (like commas or aliases) and mistakenly penalize queries that are syntactically unique but execution-equivalent to the gold standard.

CAPER addresses this gap by identifying the ideal unit of process supervision: SQL clauses (such as SELECT, FROM-JOIN, WHERE, and GROUP-BY/HAVING). Clauses naturally group tokens into meaningful intermediate decisions, making them perfect for precise root-cause error localization.

Methods

CAPER automatically derives clause-level preference supervision without requiring expensive manual annotations. The framework operates in three main phases:

  1. Trajectory Collection & Fault Injection: Rollouts are sampled from a supervised fine-tuned (SFT) policy. Successful trajectories are used for fault injection—synthetically corrupting specific nodes in the query's Abstract Syntax Tree (AST) to create diverse, realistic failure examples. Failed trajectories are kept as natural error examples.
  2. Counterfactual Intervention: For failed queries, CAPER identifies the earliest clause where the model's prediction diverges from the gold query. It then performs an AST-level repair. By evaluating the minimum-cost edit script required to fix the clause, it pinpoints the exact root-cause node responsible for the failure.
  3. Topology-Aware Reward Propagation: Once the root-cause node is identified, a continuous penalty is assigned to it. This penalty decays via a Gaussian function as it propagates upstream to ancestral nodes in the AST, while unrelated branches are masked out. These node-level penalties are then aggregated into clause-level rewards by taking the minimum (strongest penalty) within each clause unit.

The resulting dataset of over 90,000 clause-annotated preference tuples is used to train CAPER-9B (built on Qwen3.5-9B) as a Clause-Process Reward Model (Clause-PRM). This model is then deployed to supervise reinforcement learning via Group Relative Policy Optimization (GRPO), utilizing a multi-granular reward design that balances format, clause-level process, and terminal execution feedback.

Figure 2: Overview of the CAPER framework.
Figure 3: A counterfactual intervention example.

Datasets

The authors evaluate their framework using three prominent Text-to-SQL datasets:

  • BIRD: A large-scale, challenging benchmark that emphasizes database content and real-world enterprise database optimization.
  • Spider: A widely used cross-domain semantic parsing benchmark.
  • SYNSQL-5k: A synthetic training dataset utilized to augment data diversity during the training split.

Key Insights and Ideas

  • The Semantic Sweet Spot: Clauses represent the natural boundaries of SQL logic. Token-level rewards fail because individual tokens rarely constitute standalone semantic choices, whereas clause-level units preserve scalability while matching how SQL errors actually affect execution.
  • Causal Error Localization: Instead of relying on heuristic guesses or noisy LLM self-debugging, CAPER physically manipulates the AST and verifies the execution. This counterfactual approach provides an unambiguous, causal ground truth for where a query went wrong.
  • AST Topology Matters: Propagating penalties hierarchically up the AST tree structure (to parent/ancestor nodes) is far more effective than linear token-level propagation, as it respects the structural dependencies inherent to SQL syntax.
  • Versatile Candidate Verification: A process reward model trained on clause boundaries doesn't just assist in RL; it can function as a plug-and-play verifier to rerank candidate queries generated by black-box, closed-source models.

Key Results

  • Reinforcement Learning Performance: When optimizing policies with GRPO, the clause-level reward variant (GRPO-Clause) consistently outperformed sparse execution rewards and token-level shaping. On BIRD, it achieved a 69.61% Majority Vote@8 execution accuracy, representing a notable improvement over standard SFT baselines and outperforming GPT-5.4.
  • Failure Localization: On held-out failure trajectories, CAPER-9B achieved a 84.53% top-1 localization accuracy and a 90.60% Mean Reciprocal Rank (MRR), demonstrating a powerful capability to isolate the exact clause causing a query to fail.
  • Reranking Closed-Source Models: As a candidate verifier for black-box models (including GPT-5.4, Claude Sonnet 4, and Gemini 2.5 Pro), CAPER-9B consistently yielded absolute gains of +0.7 to +1.6 execution accuracy points over standard majority voting across multiple evaluation splits.
Table 1: Main execution accuracy (EX, %) on BIRD and Spider under greedy decoding (P@1) and Majority Vote@8 (MV@8), where the backbone-matched block compares Qwen3.5-9B-SFT with GRPO variants trained under sparse, token-level, and clause-level rewards; the best and second-best results are highlighted in bold and underlined, respectively.

Limitations and Future Work

Despite its strong performance, the authors note several limitations:

  • Coarse Intra-Clause Granularity: CAPER currently decomposes SQL at the top-level clause granularity. If a decisive error is buried deep inside a highly complex clause (e.g., one specific predicate inside a massive, multi-condition WHERE clause), the clause-level feedback can still be somewhat coarse.
  • Computational Overhead: The automated annotation pipeline requires running multiple SFT rollouts, parsing ASTs, performing counterfactual repairs, and executing queries repeatedly against databases. This introduces significant offline engineering and compute overhead compared to simple sparse rewards.
  • Domain and Dialect Scope: The reward model is trained on specific datasets and primarily targets SQLite dialects. Further fine-tuning may be necessary to generalize to highly specialized enterprise schemas or alternative SQL dialects.

Future Directions: The authors suggest extending this annotation procedure recursively to enable hierarchical rewards across clause, sub-clause, and token levels simultaneously, as well as applying it to other structured generation tasks.

Summary updated

BibTeX
@inproceedings{ban2026caper,
  author = {Lujie Ban and Jiasheng Shi and Jinyang Li and Xiaolin Han and Tsz Nam Chan and Chenhao Ma},
  title = {{CAPER: Clause-Aligned Process Supervision for Text-to-SQL}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2026},
  url = {https://openreview.net/forum?id=oj9P3hyKy9},
}
Figure