TL;DR
While Large Language Models (LLMs) excel at writing code, do they actually understand what that code is supposed to do? This paper introduces CodeSpecBench, a novel execution-based benchmark that evaluates LLMs on their ability to generate executable preconditions and postconditions at both the function and repository levels. The results reveal a massive capability gap: even the most advanced models struggle to write accurate behavioral specifications, proving that strong coding performance does not necessarily equate to a deep understanding of program semantics.
What Problem Does This Paper Address?
Standard code generation benchmarks (like HumanEval or SWE-bench) measure whether an LLM can produce code that passes a specific set of tests. However, passing tests doesn't guarantee that a model truly understands the underlying program logic or edge cases.
To assess true comprehension, software engineers use behavioral specifications—formal rules consisting of preconditions (what inputs and states are valid before execution) and postconditions (what properties the output and final state must satisfy).
Existing specification benchmarks are heavily constrained: they often rely on rigid, unstable static verifiers, focus only on isolated functions, or ignore preconditions entirely. This paper addresses these gaps by creating a realistic, execution-based evaluation framework that tests LLMs on generating executable Python specification functions across both simple tasks and complex, multi-file software repositories.
Methods
The authors frame specification generation as a translation task: converting natural language problem descriptions or GitHub issue reports into executable Python functions (preconditions and postconditions).
To evaluate these generated specifications, CodeSpecBench uses an execution-based protocol that tests two critical dimensions:
- Correctness: Does the specification accept valid program behaviors? (Tested against correct inputs and ground-truth outputs).
- Completeness: Does the specification successfully reject invalid or buggy behaviors? (Tested against intentionally flawed inputs and buggy model-generated solutions).
For repository-level tasks, the benchmark dynamically injects the LLM-generated specifications around target functions within the codebase using a custom test harness, executing them during runtime to see if they correctly intercept bugs without breaking valid code.
Datasets
CodeSpecBench evaluates models at two distinct granularities:
- CodeSpecBench-Func: Focuses on self-contained algorithmic problems. It is built on 2,494 problems from the public LeetCodeDataset. To ensure rigorous evaluation, the authors leveraged multiple LLMs and an online judge to generate over 200 diverse test cases (both valid and invalid) per problem.
- CodeSpecBench-Repo: Focuses on large-scale, real-world codebases. It features 500 curated issues from SWE-bench Verified across 12 major open-source Python projects (e.g.,
sympy,matplotlib,astropy), augmented with high-coverage test suites to minimize gaps.
Key Insights and Ideas
- Specification is significantly harder than coding: There is a stark contrast between a model's ability to write a solution versus writing the rules of that solution. For instance, Claude-4.5-Sonnet achieves a 77.2% pass rate on code generation at the repository level, but drops to just 21.0% on specification generation.
- The Granularity Penalty: LLMs perform moderately well on isolated functions, but their performance plummets when dealing with repository-level dependencies and stateful logic.
- Opposing Failure Modes: At the function level, models tend to write overly loose specifications that fail to catch invalid behaviors. Conversely, at the repository level, they write overly restrictive specifications, accidentally rejecting valid program behaviors due to a failure to handle complex context and external dependencies.
- Specifications can improve code generation: The authors demonstrated a practical application: using an LLM's generated specifications to filter and re-rank candidate code solutions consistently improves standard code generation performance (boosting Pass@1 metrics).
Key Results
The authors evaluated 15 state-of-the-art LLMs, including proprietary giants and open-weight models.
- Function Level: The best-performing model was GPT-5-mini with a strict pass rate of 47.0%, closely followed by Gemini-2.5-Pro at 46.2%.
- Repository Level: Performance dropped sharply across the board. Claude-4.5-Sonnet led the field but only achieved a strict pass rate of 20.2%. Open-weight models struggled significantly; the reasoning-focused Qwen3-32B scored just 2.0%.
- Algorithmic Domains: Postcondition generation was highly successful for local, directly verifiable properties like sorting (66.7% pass rate) but failed dramatically on domains requiring global structural or recursive reasoning, such as Binary Search Trees (22.2%).
Limitations and Future Work
- Approximation of Correctness: Because the evaluation is execution-based rather than using formal deductive proofs, passing all test cases does not mathematically guarantee absolute functional correctness.
- External Dependencies: CodeSpecBench-Func relies on an external online judge platform to validate inputs and outputs, which introduces potential reproducibility risks if the external service changes or becomes unavailable.
- Language Constraint: The benchmark currently only supports Python. Extending this framework to support specifications in other programming languages remains an important direction for future work.
Summary updated