AI
New AI System Bridges the Gap Between LLMs and Formal Code Verification
Researchers develop KBSpec, a method that augments large language models with domain knowledge to automatically generate valid formal specifications for Java code.
Generating formal specifications—detailed mathematical descriptions of how software should behave—is essential for verifying program correctness, but it remains tedious and error-prone work. Now researchers have shown that large language models (LLMs) can be augmented with specialized knowledge to produce these specifications automatically.
The challenge with using LLMs for this task is straightforward: formal specification languages have little presence in the training data these models learn from. As a result, LLMs often generate specifications that are syntactically malformed or fail verification checks. A new approach called KBSpec addresses this shortfall by feeding LLMs knowledge from two sources—official documentation and lessons learned from previous generation attempts.
How KBSpec Works#
Rather than retraining the underlying LLM, KBSpec maintains a self-evolving knowledge base that grows over time. When an LLM generates a specification and a verifier rejects it, the system captures that feedback and stores both successful and repaired trajectories. This knowledge is then made available to the LLM on subsequent generation tasks, without requiring labeled training data or parameter updates.
The approach targets Java Modeling Language (JML) specifications—a widely-used formal framework for describing Java program contracts. Testing across three different LLM backends, KBSpec achieved a substantial boost: 14–32% improvement in verification pass rates compared to existing LLM-based methods, and generated a higher proportion of complete, high-quality specifications.
Why It Matters#
This work points toward a practical path for automating a labor-intensive step in formal verification workflows. By letting the system learn from its own mistakes and successes in a feedback loop, KBSpec avoids the need to collect and manually label thousands of training examples. The results suggest that even without retraining, LLMs can be made significantly more reliable for this specialized task when given the right domain context.

