Better Specifications, Better Code: Fine-Tuning LLMs for Behavioral Contract Generation
Brhane Team, Tiantian Yang, Nikta Akbarpour, Simone Bartucci, Francesco Calimeri, Song Wang
Abstract
Large language models (LLMs) demonstrate strong code-generation capabilities but often struggle with functional correctness due to implicit or ambiguous behavioral requirements. We present CG2, a contract-guided code generation framework that translates natural-language tasks into structured behavioral contracts specifying interfaces, preconditions, postconditions, and invariants. CG2 fine-tunes an LLM to generate these contracts and introduces an iterative semantic validation and repair process to identify and correct incomplete, inconsistent, or incorrect specifications before guiding an unchanged downstream code-generation model. We evaluate CG2 on 1,007 held-out tasks using five code LLMs: DeepSeek-Coder-V2-Lite-Instruct, Ministral-3-14B-Instruct-2512, Qwen2.5-Coder-14B-Instruct, Qwen2.5-Coder-32B-Instruct, and Qwen3.5-27B. Results show that CG2 consistently improves pass@1 over direct code generation by 1.49-3.27 percentage points (7.58%-22.45% relative improvement) and over contract guidance without fine-tuning by 1.19-2.38 percentage points (7.04%-14.65% relative improvement). Manual analysis further reveals that semantically correct contracts are associated with higher code-generation accuracy. These findings demonstrate that explicitly specifying and validating behavioral requirements can improve functional correctness without modifying downstream code-generation models.
Create a lesson
Related papers
Temporal Adaptive Fine-Tuning for Code Comprehension
Shared title terminologycs.SEAlex Wolf, Pooja Rani, Harald C. Gall
Micro Operational Robustness of LLMs on Code Generation
Shared title terminologycs.SEDebalina Ghosh Paul, Hong Zhu, Ian Bayley
How Specification Explicitness Shapes Failure in LLM-Generated Code
Shared title terminologycs.SEElmira Onagh, Maleknaz Nayebi
From Candidate Admission to Task-Level Return: Auditing LLM-Generated Code under Execution Contracts
Shared title terminologycs.SEHiroyasu Usami, Keisuke Hara, Ayato Tsuboi et al.
SpecCloak: Probing Harmful Code Generation through Engineering Specifications
Shared title terminologycs.SEWenbin Chen, Jiexin Wang, Guanting Wu et al.
fx(hash): Salvaging The Source Code of 28k Generative Artworks
Shared title terminologycs.SEBenoit Baudry, Valeriia Bezklynska, Martin Monperrus