Fine-tuning language models to write clearer software contracts improves coding accuracy

Better Specifications, Better Code: Fine-Tuning LLMs for Behavioral Contract Generation

Software Engineering

Summary

Large language models that write code sometimes get confused by unclear instructions about what the code should do. The authors created a method called CG2 that first makes these instructions very clear and precise, like detailed promises about inputs and outputs, and checks them for errors. Then, without changing the code-writing model, CG2 uses these clearer contracts to produce better code. Tests showed that this approach helped several popular models write more correct programs. This means better instructions can lead to better computer programs without changing how code is generated.

What this means in practice

  • For software developers: Generate clearer functional contracts from natural language to improve code correctness automatically.
  • For software testing teams: Use contract validation techniques to find and fix ambiguous or incomplete specifications before coding.

Authors

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.