Large language models (LLMs) have advanced code generation from natural language descriptions of desired behavior (intent). Most existing methods refine code using execution feedback while leaving the underlying specification unchanged, implicitly assuming that the LLM correctly understands the intent. Yet ambiguous or underspecified descriptions can lead even capable models to correctly implement the wrong intent, making specification mismatch a central bottleneck. This paper presents BeSpec, a behavioral model based approach to specification alignment. Treating intent as partial evidence of intended program behavior, BeSpec extracts a structured specification and predicts executable behaviors that capture properties a correct program should satisfy. These avoid requiring exact test oracles, which can be as hard to predict as the solution itself. BeSpec executes candidate programs on probe inputs and compares their observed behaviors with the predicted behaviors. It validates mismatches and diagnoses their causes, refining defective specifications. We evaluate BeSpec with three LLMs on four code generation benchmarks: CodeContests, xCodeEval, APPS, and LiveCodeBench. Against nine baselines, BeSpec achieves the highest Pass at 1 and average pass rate in all settings, improving mean Pass at 1 over the strongest baseline by 9.3 to 23.6 percent relative across LLMs. After alignment, most remaining failures are adherence failures. Ablation studies on qwen3 coder 30b show the benefit of behavior modeling.
翻译:暂无翻译