Next Steps in LLM-Supported Java Verification

Samuel Teuber, Bernhard Beckert · DOI

TL;DR

This paper investigates how to reliably generate formal code specifications (JML annotations) using LLMs under the supervision of a deductive verifier (KeY). Surprisingly, the authors find that providing detailed error feedback from the verifier does not significantly improve GPT-4o's success rate compared to simply sampling multiple independent attempts from scratch, while being significantly less token-efficient.

What Problem Does This Paper Address?

Deductive program verification allows developers to mathematically prove that their code satisfies a given specification. However, writing these specifications—especially auxiliary annotations like loop invariants or submethod contracts—is notoriously difficult and time-consuming for human engineers.

Large Language Models (LLMs) offer a promising shortcut by automatically generating these annotations. Because formal verifiers provide a rigorous, automated way to check if an LLM's output is mathematically correct, this setup forms a robust "intersymbolic AI" pipeline. If the verifier accepts the specification, we can trust it completely, bypassing the usual LLM hallucination problem.

However, LLMs are still unreliable oracles that frequently fail on their first try. The core problem this paper addresses is error recovery: when a verifier rejects an LLM-generated specification, what is the best prompting strategy to guide the model to a correct solution?

Methods

The authors evaluate an automated verification loop using GPT-4o and the KeY verifier (a deductive verification tool for Java programs using the Java Modelling Language, or JML).

The LLM-verifier integration loop.

They compare two primary prompting paradigms for fixing verification failures:

  1. Feedback-Based Approach: The LLM is given the precise error message from the verifier. For syntactical errors, this means the parser error; for semantical errors (failed proofs), it receives the labels of the open, unclosed proof branches (e.g., "Invariant Initially Valid"). The conversation continues in a single thread.
  2. Sampling-Based Approach (Baseline): Instead of providing feedback, the system completely resets the conversation and requests a brand-new specification from scratch, exploiting the probabilistic nature of LLM generation.
  3. Mixed Approach: A hybrid strategy that combines both, running multiple independent sampling rounds, each followed by a fixed number of feedback steps.

Datasets

The approach was evaluated on a benchmark dataset from prior work consisting of:

  • 27 tasks for JML loop invariant synthesis.
  • 14 tasks for submethod contract specification.

These benchmarks cover a wide range of standard JML features, including array manipulations, quantifiers, assignable clauses, and references to pre-execution values.

Key Insights and Ideas

  • Feedback Can Cause LLMs to Get "Stuck": Counterintuitively, detailed error feedback from the verifier did not significantly outperform blind sampling. The authors observe that when given feedback, GPT-4o often gets trapped in a local minimum—making minor, superficial tweaks to a fundamentally flawed specification rather than pivoting to a completely new, correct strategy.
  • The Quadratic Cost of Conversation: Sampling is vastly more token-efficient than feedback. Because feedback requires feeding the entire chat history back into the model, token costs grow quadratically ($O(n^2)$) with the number of turns. Sampling costs grow linearly ($O(n)$). Early on, sampling provides a much higher success rate per token spent.
Success rate relative to the number of steps taken.

Key Results

  • Overall Performance: Iterative generation (whether via sampling or feedback) successfully solved approximately 85% of loop invariant problems and 57% of submethod contract problems, proving the viability of LLM-supported verification.
  • Sampling vs. Feedback: Pure sampling achieved highly competitive success rates and occasionally outperformed the feedback-based approach, especially when accounting for token efficiency.
  • The Mixed Strategy Wins: The hybrid approach (5 independent samples, each allowed 10 feedback steps) yielded the best overall results, solving 24/27 invariants and 8/14 subcontracts (see Table I in the paper). It successfully found complex specifications, such as a loop invariant for an array reversal program, which pure sampling missed.

Limitations and Future Work

  • Statistical Significance: The authors note that their benchmark dataset is relatively small, meaning the differences between sampling and feedback are not yet statistically definitive.
  • Prompt Engineering Gaps: The lack of efficacy in the feedback approach might be mitigated by better prompt engineering or by translating raw verification proof trees into more intuitive, natural-language hints for the LLM.
  • Single-Gap Constraint: The current study only evaluates programs with a single missing specification gap. Real-world software systems will have multiple missing invariants and contracts simultaneously, which drastically increases the search space and complexity.
  • Alternative Validation: The authors propose exploring testing or fuzzing to quickly filter out bad LLM specifications before wasting time and tokens on heavy deductive verification.

Summary updated

BibTeX
@inproceedings{teuber2025next,
  author = {Samuel Teuber and Bernhard Beckert},
  title = {{Next Steps in LLM-Supported Java Verification}},
  booktitle = {NSE@ICSE},
  year = {2025},
  url = {https://openreview.net/forum?id=DKaOUmfvhx},
}
Figure