arXiv(opens in a new tab)cs.SESep 2026Cited by 1 paper on PaperVantage

Manuscript Offprint

arXiv:2609.06041

SpecCoder: Specification-Aware Code Generation with Curriculum Dual-Task Reinforcement Learning

Yixuan Li, Mingxuan Huang, Jiajing Wang, Weidong Yang, Xinyi Liu, Ben Fei, Lipeng Ma

Abstract

Large language models (LLMs) have made substantial progress in code generation but still struggle with challenging programming tasks that require understanding rich natural language requirements. These requirements often specify problem goals, input/output formats, constraints, examples, edge cases, and implicit assumptions. Overlooking even one of them may produce executable but functionally incorrect code. Existing training-free methods mainly rely on prompting or agent-based workflows, while training-based methods typically optimize final code outputs using supervised or execution-based signals. However, existing approaches provide limited supervision for learning the intermediate mapping from raw requirements to structured specification analyses and for grounding such analyses in concrete implementation behavior. Consequently, models may omit critical constraints when interpreting raw requirements, and even when an explicit specification analysis is produced, the resulting implementation may fail to reflect it consistently. Motivated by this gap, we propose SpecCoder, a specification-aware two-stage training framework for code generation. SpecCoder first employs specification-guided Supervised Fine-Tuning (SFT) to train LLMs to derive structured specification analyses and generate code conditioned on them. It then introduces curriculum dual-task Group Relative Policy Optimization (GRPO), which jointly optimizes specification-guided generation and discrimination to encour-

∗^{*}Corresponding authors

Email addresses: yxli24@m.fudan.edu.cn (Yixuan Li),

24307130121@m.fudan.edu.cn (Mingxuan Huang), 25213050375@m.fudan.edu.cn (Jiajing Wang), wdyang@fudan.edu.cn (Weidong Yang), liuxiny24@m.fudan.edu.cn (Xinyi Liu), benfei@cuhk.edu.hk (Ben Fei), lpga21@m.fudan.edu.cn (Lipeng Ma) age stronger correspondence between structured specification analyses and code behavior. Experiments on APPS, CodeContests, and xCodeEval demonstrate the effectiveness of specification-aware training, with SpecCoder consistently improving both standalone code generation and the performance of agent-based workflows. Additional evaluations on BigCodeBench-Hard and ClassEval, together with human evaluation and specification perturbation studies, further support the effectiveness of structured specification analyses and their role in guiding code generation and discrimination.

Keywords: Code Generation, Specification-Aware Reasoning, Reinforcement Learning, Large Language Models

1. Introduction

Code generation is a fundamental research problem in software engineering. Large language models (LLMs) have made substantial progress and perform well on widely used benchmarks such as HumanEval [1] and MBPP [2]. However, their performance remains limited on more challenging programming benchmarks, such as APPS [3], CodeContests [4], and xCodeEval [5]. One key difference is that benchmarks such as HumanEval and MBPP usually provide relatively simple prompts, such as a function signature or a short instruction, whereas more challenging tasks often require LLMs to reason over richer raw natural language requirements. These requirements specify the intended code behavior and may include problem goals, input and output formats, constraints, examples, edge cases, and implicit assumptions. Therefore, solving such specification-rich tasks requires LLMs not only to generate syntactically valid or executable code, but also to form an explicit understanding of the requirements and ensure that the implementation satisfies the intended functionality and constraints.

Existing methods for improving LLM-based code generation can be broadly categorized into training-free and training-based approaches. Training-free methods can be further divided into prompt-based methods and agent-based workflows. Prompt-based methods [6, 7, 8, 9] guide the generation process through carefully designed instructions for planning, reasoning, or output repair. They are lightweight and easy to deploy, but their effectiveness is often limited because requirement understanding remains implicit and is not directly optimized. Agent-based methods [10, 11, 12, 13] decompose code generation into multi-step or multi-role workflows involving requirement analyses, implementation, testing, and revision. Although effective, these methods typically require repeated inter-actions and iterative execution, leading to substantial inference cost. More importantly, both prompt-based and agent-based methods mainly improve how LLMs use prompts or external workflows at inference time, rather than enhancing their intrinsic ability to align generated code with raw requirements. Training-based methods [14, 15, 16, 17] optimize LLMs through reinforcement learning or preference optimization, primarily using execution-based feedback from test cases or preference signals. However, such feedback often provides sparse reward signals and does not explicitly supervise how requirements are understood and translated into structured specification analyses. Consequently, existing training-based methods provide limited supervision for learning to derive structured specification analyses from raw requirements and use them to guide code generation.

Recent studies [18, 19] show that code generation failures often stem from misunderstanding raw requirements. When requirements such as key concepts, input and output constraints, or edge cases are overlooked, generated code can easily deviate from the intended code behavior. However, existing specification-alignment methods [20, 21] mainly perform alignment, repair, or verification during inference, rather than improving the intrinsic specification-alignment ability of LLMs through training. A natural direction is therefore to train LLMs with explicit specification-aware reasoning capabilities. This direction presents two challenges. First, building an accurate and structured specification-level understanding from raw requirements remains challenging. For complex programming tasks, requirement understanding should be made explicit before implementation, as implicit contextual reasoning alone may cause LLMs to omit, misinterpret, or weaken key constraints. Second, generated code is not always well aligned with the specification-level understanding. Even when reasonable structured specification analyses are produced, the final implementation may still ignore, simplify, or contradict them, creating a gap between the stated understanding and actual code behavior. Moreover, execution-based feedback typically provides only outcome-level signals [22, 23, 16], indicating whether code passes tests but not which requirement constraints are satisfied or violated. As a result, such feedback provides limited guidance for training models to follow structured specifications or compare candidate implementations according to specification satisfaction.

To address these challenges, we propose SpecCoder, a specification-aware two-stage training framework for code generation. SpecCoder first conducts specification-guided Supervised Fine-Tuning (SFT), enabling the LLM to derive structured specification analyses from raw requirements and generate code conditioned on them. SpecCoder then applies curriculum dual-task Group Relative Policy Optimization (GRPO). In this stage, the generation task further optimizes specification-guided code generation, while the discrimination task trains the LLM to select, from paired candidate implementations, the one that better satisfies the raw requirement under the shared structured specification analysis. For both GRPO tasks, SpecCoder categorizes training samples into Easy, Medium, and Hard subsets and organizes them into a three-phase curriculum. The curriculum first establishes basic associations between structured specification analyses and code behavior using easier samples, then addresses harder cases requiring complex requirement analysis and implementation comparison, and finally mixes all difficulty levels to consolidate learned behaviors and mitigate forgetting. By combining these complementary generation and discrimination objectives, SpecCoder better aligns structured specification analyses with code behavior.

We evaluate SpecCoder as a standalone code generator and as the backbone of representative agent-based code generation frameworks. In the standalone setting, SpecCoder achieves strong performance on the in-distribution (ID) APPS and CodeContests benchmarks, reaching Pass@1/AvgPassRatio scores of 0.186/0.346 and 0.091/0.207, respectively. It achieves the best results among training-free prompting baselines and is competitive with or superior to training-based baselines across almost all metrics. On the out-of-distribution (OOD) xCodeEval benchmark, SpecCoder obtains a Pass@1 of 0.147 and an AvgPassRatio of 0.324, outperforming all compared training-free prompting and training-based baselines. When used as the backbone of agent-based frameworks, SpecCoder consistently improves all evaluated frameworks across APPS, CodeContests, and xCodeEval, with gains of up to 11.3 percentage points in Pass@1 and 13.2 percentage points in AvgPassRatio over the base model. These results demonstrate that SpecCoder improves both standalone code generation and its use as a backbone for agent-based workflows. Further analyses show that structured specification analyses provide useful intermediate guidance for code synthesis.

This paper makes the following contributions.

  • We propose SpecCoder, a specification-aware two-stage training framework that combines specification-guided SFT with curriculum dual-task GRPO to explicitly learn structured specification analyses from raw requirements and use them to guide code generation.

  • We introduce structured specification analyses as a shared intermediate representation for specification-guided generation and implementation discrimination, allowing the two training objectives to jointly relate requirement understanding to concrete code behavior.

  • We conduct extensive evaluations of SpecCoder in standalone and agent-based settings across ID and OOD benchmarks. Additional analyses on intermediate representations, specification dependency, and realistic programming tasks further demonstrate its effectiveness and applicability.

References (37)