NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Reward Shaping to Improve Language Model Query Generation

Shicheng Liu, Zeyu Zhang, Lei Lu, Kexuan Sun, Thuy VuNeurIPS 2026

TL;DR

Translating natural language into database queries (like SQL or Cypher) is notoriously brittle: a single misplaced comma ruins the entire query, yet standard Reinforcement Learning (RL) punishes the whole output instead of pointing out the typo. This paper introduces RESHAPE, an algorithm that automatically learns a dense, token-by-token reward signal from simple pass/fail execution results. By giving language models surgical, fine-grained feedback without manual reward engineering, RESHAPE boosts query generation accuracy across the board with minimal computational overhead.

What Problem Does This Paper Address?

If a chatbot writes, "The weather are sunny," you still know to grab your sunglasses. Natural language is forgiving. Formal query languages like SQL and Cypher are not. If an LLM translates your question into a 50-token query and gets 49 tokens right, a single misplaced colon causes a fatal execution error.

Recently, researchers have turned to RL to train models to write better queries. Typically, you execute the generated query against a database: if it returns the right answer, the model gets a +1 correctness reward; if not, 0. Because this feedback only arrives at the very end of the sequence, it is sparse. The model knows it failed, but has no idea which token caused the crash.

To soften this binary cliff, researchers often bolt on "partial rewards" (forming a composite reward) that score auxiliary traits like valid syntax or reasoning length. However, this is still sequence-level feedback, and worse, it invites reward hacking: models quickly learn it's much easier to write long, syntactically pristine queries that answer the completely wrong question than it is to actually solve the task.

Figure 1: Four scenarios. The first query is correct and receives a correctness reward of +1. The second query is incorrect and receives a correctness reward of 0. The third query receives a composite reward of -3. The fourth query receives token-level rewards and only the wrong syntax symbol is penalized.

Methods

The authors want the best of both worlds: dense (token-level) feedback that is strictly aligned with global query correctness. Designing token-level rules by hand is prácticamente impossible, so they frame it as a bilevel optimization problem—essentially a two-tier learning game:

  1. The Lower Level (Policy Optimization): The LLM policy ($\pi$) tries to generate queries that maximize a dense, token-level shaping reward ($r_\theta$), while staying close to a reference model.
  2. The Upper Level (Reward Learning): A separate parameterized model ($\pi_\theta$, structured using potential-based reward shaping) adjusts the token-level rewards so that the resulting LLM policy ultimately maximizes the true, binary sequence-level correctness reward.

Standard bilevel optimization uses "double-loop" algorithms: you train the LLM all the way to convergence, tweak the reward model once, and repeat. For multi-billion-parameter LLMs, that is computationally ruinous.

Instead, the authors propose RESHAPE, a single-loop algorithm. At each step, it performs just one policy update (compatible with off-the-shelf RL optimizers like GRPO, ReMax, or RLOO) and one reward update, reusing the exact same batch of generated rollouts for both. To prevent the system from destabilizing—since the reward model is judging a policy that hasn't finished learning—they use a two-timescale stochastic approximation: the policy updates on a faster timescale than the reward model, allowing them to prove mathematically that the whole dance converges at a rate of $O(\frac{1}{\sqrt{K}}) + O(\frac{\log K}{\sqrt{K}})$.

Datasets

The authors evaluate two distinct query paradigms, splitting all training sets 1:1 between Supervised Fine-Tuning (SFT) and RL:

  • SQL (Relational Databases): Evaluated on two standard public benchmarks, BIRD and Spider.
  • Cypher (Knowledge Graphs): Because public text-to-Cypher benchmarks are scarce, the authors constructed two new knowledge graph benchmarks covering Finance and Sports, evaluated via a human-validated pipeline.

Key Insights and Ideas

  • Query generation needs surgical credit assignment: Unlike creative writing, coding and querying fail locally. Giving positive reinforcement to the 49 correct tokens while dropping a -10 anvil on the lone bad comma (as shown in Figure 1) dramatically accelerates learning.
  • Optimize for the policy's outcome, not just reward accuracy: Prior token-level methods (like PRIME) train reward models using supervised cross-entropy to predict binary correctness. RESHAPE optimizes the reward directly based on whether the downstream RL policy gets more queries right, enforcing true policy-level alignment.
  • Bilevel RL doesn't have to break the bank: Because LLM training is bottlenecked by generating text (rollouts), and RESHAPE reuses policy rollouts to compute the reward's hyper-gradient, learning a live reward model adds only ~14%–18% wall-clock time.

Key Results

Tested across three base models (Qwen2.5-Coder-3B, deepseek-coder-6.7b, and Qwen2.5-Coder-7B) and three RL optimizers, RESHAPE consistently beats SFT, sparse correctness rewards, composite rewards (SQL-R1), and supervised token-level rewards (PRIME) in 35 out of 36 experimental settings.

Table 1: Correctness rate (in percentage) for Cypher/SQL query generation across four benchmarks.
  • Accuracy jumps: On Qwen2.5-Coder-7B + GRPO, RESHAPE hits 59.91% on BIRD and 89.04% on Spider (up from 56.01% and 85.85% with pure correctness rewards). On the Finance Cypher benchmark, it reaches 65.47%, beating the composite baseline by nearly 4 percentage points.
  • Eradicating local typos: In a manual inspection of 500 Finance test queries (Qwen-3B + GRPO), the composite reward baseline made 25 localized mistakes (misspelled entities/properties, syntax slips, or wrong schema classes). RESHAPE slashed those errors from 25 down to just 2—a 4.6% local-error reduction that almost entirely explains its 3.81% overall accuracy lead on that setup.

Limitations and Future Work

While the authors do not include a standalone limitations section, two practical boundaries stand out in their experiments:

  • Non-local logical errors remain: In their error analysis, the authors explicitly set aside non-local errors (such as fundamentally flawed query logic). While token-level shaping excels at cleaning up syntax, entity names, and schema-linking, deeper multi-hop reasoning errors remain an open challenge.
  • Modest compute overhead: Though efficient for a bilevel method, updating a separate reward parameterization ($\pi_\theta$) alongside the policy still incurs a 14%–18% increase in training time compared to standard sparse-reward RL.

Summary updated

BibTeX
@inproceedings{liu2026reward,
  author = {Shicheng Liu and Zeyu Zhang and Lei Lu and Kexuan Sun and Thuy Vu},
  title = {{Reward Shaping to Improve Language Model Query Generation}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2026},
  url = {https://openreview.net/forum?id=ea5xEo2hw1},
}
Figure