Path2Spec: Path-Aware Specification Generation via Large Language Models

Dan Huang, Zhensu Sun, Huihui Huang, Jinfeng Jiang, Xiaofei Xie, Yintong Huo, David LoarXiv 2026

TL;DR

Path2Spec is a new framework that significantly improves how Large Language Models (LLMs) generate formal program specifications by using a "divide-and-conquer" approach. By forcing LLMs to reason about individual execution paths and recursively decomposing complex code into simpler subprograms, Path2Spec generates specifications that are not only mathematically verifiable but also semantically precise, outperforming existing state-of-the-art methods by up to 38%.

What Problem Does This Paper Address?

Formal specifications—like preconditions (what a function needs) and postconditions (what it guarantees)—are the bedrock of software verification and maintenance. While LLMs have shown promise in writing these automatically, they often "cheat."

The authors discovered a major quality gap: existing tools often produce vacuous or overly broad specifications. For example, an LLM might generate a postcondition that simply says "the output is an integer," which is technically true and passes a verifier, but fails to describe what the code actually does. Their analysis showed that 34.6% of specifications generated by current state-of-the-art tools were logically incomplete. This happens because these tools treat a program as a single unit, leading the LLM to provide coarse-grained summaries rather than detailed, path-specific logic.

Methods

Path2Spec introduces a systematic workflow to move from coarse summaries to fine-grained logic:

  1. Path-Aware Reasoning: Instead of looking at the whole function, Path2Spec first uses an LLM to extract all possible execution paths (symbolic execution).
  2. Path-Specific Generation: The framework prompts the LLM to generate a specification for each individual path. These are verified using OpenJML. If a path spec fails, the error is fed back to the LLM for iterative refinement.
  3. Decompose-then-Retry: For complex programs where the LLM struggles even with path guidance, Path2Spec recursively breaks the program into smaller subprograms based on logical branches (like if-else or loops) and tries again on these simpler snippets.
  4. Interleaved Merging: Once all paths or subprograms have verified specifications, Path2Spec merges them back into a comprehensive overall specification for the original program.
Fig. 3. The workflow in the Path-based Specification Generation phase operates as follows: First, it extracts all possible execution paths from the program, which may contain N executable paths 1○. Then, using the information from each execution path as guidance, it prompts the LLM to generate a formal specification for that path 2○. The generated formal specification is verified using the OpenJML verifier 3○. If verification fails, the error information is incorporated into the prompt to guide the LLM in refining the specification 4○. This iterative refinement-and-verification process continues until correct specifications are generated for all paths, which are then merged into the original program 5○.
Fig. 2. Workflow of Path2Spec. A red cross indicates that the path-based specification generation process failed, whereas a green check indicates success, which yields a set of specifications. When a program fails to generate a verified specification, it enters the first decomposition process (marked as 1○). The program is decomposed into subprograms based on the first logical branch encountered. Each subprogram then enters the path-based specification generation process. If a subprogram successfully generates a verified formal specification, that specification is recorded. However, if a subprogram still produces an incorrect formal specification, it undergoes another decomposition process (marked as 2○). The resulting subprograms repeat the path-based specification generation process. If any subprogram continues to fail in generating a verified formal specification, it is further decomposed (marked as 3○). This iterative process continues until all subprograms produce verified formal specifications. Finally, we progressively merge and verify the formal specifications from all subprograms layer by layer, ultimately integrating them into the original program (marked as 4○).

Datasets

The authors evaluated Path2Spec on three primary benchmarks:

  • SG-Bench: 120 Java programs characterized by heavy use of loops and nesting.
  • SV-COMP: 265 Java programs that are branch-heavy with complex inter-procedural dependencies.
  • SpecEval: 125 programs with human-written ground-truth specifications, used specifically to measure "Logical Strength" (how complete the generated spec is compared to a human expert's version).

Key Insights and Ideas

  • Verification $\neq$ Quality: A specification passing a verifier like OpenJML doesn't mean it's good. The authors propose a new metric, Logical Strength, to ensure generated specs aren't just "correct" but are also "complete" and "precise."
  • LLMs as Symbolic Executors: Surprisingly, the authors found that using an LLM to extract execution paths outperformed traditional symbolic execution tools like Java PathFinder. Traditional tools often crashed on string-heavy operations or environment-dependent code, whereas LLMs handled them gracefully.
  • Structure is "Almost Free": The "Single-shot" version of Path2Spec (without the expensive iterative repair loop) already delivers the bulk of the quality improvements over baselines at a computational cost similar to standard prompting.

Key Results

Path2Spec demonstrated substantial improvements across the board, especially when using advanced models like GPT-5:

  • Pass Rates: On SG-Bench, Path2Spec achieved an 87.5% pass rate (vs. 66.7% for the baseline SpecGen). On the more complex SV-COMP, it reached 83.0% (vs. 44.2% for the baseline).
  • Logical Completeness: Using the SpecEval benchmark, Path2Spec generated significantly more "complete" specifications. For GPT-5, the completeness rate rose from 45.1% to 65.9%.
  • Human Evaluation: In a blind study, human experts rated Path2Spec's outputs at 3.4/4.0 for quality, compared to 2.4/4.0 for the baseline.

Limitations and Future Work

While powerful, Path2Spec has a few hurdles to clear:

  • LLM Reliability: The framework still relies on the LLM's ability to extract paths correctly. If the LLM misses a path or invents an infeasible one, the final specification will be flawed.
  • Language Specificity: The current implementation is tailored to Java and the Java Modeling Language (JML). Generalizing this to other languages and logic systems remains a task for the future.
  • Quality-Blind Termination: The current "retry" logic stops as soon as a specification passes verification. The authors suggest that future versions could be "quality-aware," continuing to decompose and refine until a specific logical strength threshold is met.

Summary updated

BibTeX
@misc{huang2026path,
  author = {Dan Huang and Zhensu Sun and Huihui Huang and Jinfeng Jiang and Xiaofei Xie and Yintong Huo and David Lo},
  title = {{Path2Spec: Path-Aware Specification Generation via Large Language Models}},
  year = {2026},
  eprint = {2609.33438},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2609.33438},
}
Figure