TLDR: A new research paper introduces PACT, a framework to evaluate and enhance how large language models (LLMs) adhere to software contracts (preconditions and input validation) in code generation. Current benchmarks primarily assess functional correctness, overlooking robustness against ill-formed inputs. PACT generates precise contract-violating test cases using SMT solvers and demonstrates that augmenting LLM prompts with these examples significantly improves contract adherence. However, this enhanced robustness often comes with a trade-off, potentially reducing functional correctness, highlighting the need for multi-objective optimization in LLM training.
Large language models (LLMs) are increasingly used for code generation, but a new research paper highlights a critical oversight in how their output is typically evaluated. While existing benchmarks like HumanEval+ and MBPP+ focus on whether generated code functions correctly with valid inputs, they often ignore a crucial aspect of real-world software: adherence to contracts. Contracts are essentially the rules, preconditions, and validity constraints that dictate how a program should handle ill-formed or unexpected inputs. Ignoring these contracts means that LLMs might generate code that appears correct but is fundamentally fragile and unreliable.
To address this gap, researchers Soohan Lim, Joonghyuk Hahn, Hyunwoo Park, Sang-Ki Ko, and Yo-Sub Han introduce PACT, a novel program assessment and contract-adherence evaluation framework. PACT is designed to systematically evaluate and enhance how well LLM-generated code respects these vital contracts, alongside its functional correctness. The full research paper, titled “Do Large Language Models Respect Contracts? Evaluating and Enforcing Contract-Adherence in Code Generation,” can be found here.
The Problem with Current Code Evaluation
Current methods for evaluating code generation primarily use a metric called pass@k, which checks if a program produces the correct output for well-formed inputs. However, real-world software must also handle invalid inputs gracefully. For example, a function expecting a list of positive integers should reject an input that is not a list, or contains negative numbers. If the code doesn’t explicitly check for these conditions, it might silently accept invalid data, leading to hidden faults that propagate through a system and are difficult to trace. This creates an inflated perception of code quality, as models are not tested for their robustness against inputs that violate specified contracts.
Introducing PACT: A New Framework
PACT bridges this critical gap with three main contributions:
- Comprehensive Test-Suite Corpus: It provides an extended set of test cases for HumanEval+ and MBPP+ that specifically target contract violations.
- Systematic Analysis of Prompting Conditions: PACT allows for analyzing code generation under different prompting strategies, demonstrating that providing concrete examples of contract violations significantly improves a model’s ability to adhere to contracts.
- Novel Metrics: It introduces new, rigorous metrics to quantify contract adherence in both test generation and code generation.
How PACT Works
PACT’s methodology involves two key stages. First, it focuses on generating high-quality contract-violating test cases. Unlike direct LLM generation, which can produce inconsistent or contradictory tests, PACT uses an SMT (Satisfiability Modulo Theories) solver. This solver translates natural language contracts into formal rules, then systematically constructs test cases that precisely violate one or more specified contracts while satisfying the rest. This ensures that the generated tests are logically sound and accurately target specific contract rules.
Second, PACT systematically analyzes code generation under different prompting conditions. The researchers compared two strategies: a Contract Specification (CS) prompt, which includes a natural language description of contracts, and an Example-Augmented Specification (EAS) prompt, which enhances the CS prompt with concrete contract-violating test cases. The generated code snippets are then evaluated using standard functional correctness metrics (pass@k) and PACT’s new contract adherence metrics: Assert Violation Coverage (AVC), Assertion Alignment Recall (AAR), and Assertion Alignment Precision (AAP).
Key Findings from Empirical Studies
The empirical studies conducted on HumanEval+ and MBPP+ datasets yielded significant insights:
- High-Quality Test Cases: PACT’s SMT-solver-based approach for generating contract-violating test cases significantly outperformed direct LLM generation, especially in Target Specificity, ensuring that test cases precisely targeted intended contract violations.
- Effectiveness of Concrete Examples: The Example-Augmented Specification (EAS) prompting strategy proved far more effective than abstract natural language descriptions (CS) in eliciting contract-aware code. Models prompted with concrete examples showed substantial improvements across all contract adherence metrics (AVC, AAR, AAP), indicating that explicit examples provide a clearer signal for models to implement robust checks.
- The Trade-off: A crucial finding was the inherent tension between contract adherence and functional correctness. While augmenting prompts with contract-violating test cases significantly enhanced a model’s ability to enforce contracts, this often came at the cost of a measurable decline in performance on standard functional correctness benchmarks. This suggests that LLMs, when faced with the dual objective of functional logic and contract enforcement, may struggle to optimize both simultaneously, leading to more robust but sometimes functionally flawed code.
Also Read:
- MOJOFuzzer: Enhancing Software Testing for New AI Languages with LLMs
- Enhancing Code Security with Graph-Based Reasoning in Large Language Models
Conclusion and Future Directions
PACT redefines code and test case correctness by introducing a comprehensive evaluation paradigm that considers both functionality and contract-based behavior. It moves beyond the limitations of existing benchmarks, providing a more realistic assessment of an LLM’s code generation capabilities. The framework’s effectiveness in generating accurate contract-violating tests and improving contract adherence in generated code is clear. The identified trade-off between robustness and functional correctness highlights a critical area for future research, suggesting that advanced training paradigms, such as reinforcement learning with multi-objective rewards, could be used to optimize for both objectives simultaneously, leveraging PACT’s novel metrics as direct training signals.


