Selective Code Generation for Functional Guarantees

Jaewoo Jeong, Taesoo Kim, Sangdon ParkarXiv 2025 · DOI

Abstract

Large Language Models (LLMs) have demonstrated remarkable proficiency in code generation but remain prone to "hallucinations"—producing code that is syntactically valid yet logically incorrect. This paper introduces Selective Code Generation (SCG), a framework that allows models to abstain from outputting code when functional correctness cannot be statistically guaranteed. By leveraging dynamic analysis (fuzzing) to generate exhaustive unit test suites, SCG establishes a rigorous "$\alpha$-code entailment" metric. Our results across diverse benchmarks and models, including GPT-4o and DeepSeek R1, show that SCG effectively controls the False Discovery Rate (FDR) while preserving high selection efficiency, offering a path toward safer deployment of LLMs in software engineering.

Introduction

The integration of LLMs into software development workflows is hindered by the lack of functional guarantees. Unlike natural language, where semantic entailment can be estimated through textual similarity, code correctness is binary and often depends on edge cases. Traditional benchmarks like HumanEval rely on sparse, manually written test cases that fail to capture the full logic of a solution. Consequently, models may pass these tests while failing in real-world scenarios.

The core challenge lies in certifying code correctness. The authors propose that instead of forcing a model to always provide a snippet, it should have the option to return "I don't know" (IDK) when its internal confidence suggests a high likelihood of failure. This approach, known as selective prediction, is adapted here specifically for the executable nature of code.

Methodology: $\alpha$-Code Entailment and Fuzzing

The SCG framework redefines correctness through $\alpha$-code entailment. A generated snippet is considered to entail the desired functionality if it passes a significant fraction ($1-\alpha$) of a comprehensive test suite. To generate these suites, the researchers employ fuzzing (using dynamic analysis tools like Atheris) on canonical solutions to produce hundreds of diverse input-output pairs.

Selective Prediction Mechanism

SCG utilizes a selection function $\hat{s}(x)$ that decides whether to output the generated code $G(x)$ or abstain. During a calibration phase, a threshold $\hat{t}$ is determined to bound the False Discovery Rate (FDR). At inference, the model only outputs code if its confidence score $f(x, G(x))$ exceeds this threshold.

Comparison between the proposed SCG framework and conventional selective prediction methods. SCG achieves high selection efficiency by approximating code entailment through dynamic analysis.

As illustrated in Figure 2, conventional selective prediction often relies on "Exact Match" criteria, which are overly restrictive and lead to low selection efficiency (frequent "IDK" responses even for correct code). SCG's use of $\alpha$-code entailment approximation allows for a more nuanced and efficient selection process.

Experimental Setup: FuzzEval

To evaluate SCG, the authors introduced FuzzEval, a new paradigm where models are tested against fuzzed versions of standard datasets:

  • APPS-f, MBPP-f, HumanEval-f, and Mercury-f. The evaluation spanned five programming languages (Python, C++, Java, JavaScript, and Perl) and state-of-the-art models.

Key Results

Empirical results confirm that SCG successfully bounds the hallucination rate to user-specified levels. Notably, many snippets that passed traditional static tests failed when subjected to the hundreds of inputs in FuzzEval, highlighting the inadequacy of current benchmarks.

Performance of SCG on GPT-4o across various configurations. The framework maintains the False Discovery Rate (FDR) below the target threshold (dashed line) while achieving significant selection efficiency.

As shown in Figure 6, for GPT-4o, SCG maintains the FDR-CE (False Discovery Rate for Code Entailment) below the target risk budget ($\epsilon_S = 0.3$) while maximizing efficiency. High-performing models like GPT-4o exhibit better calibration, allowing the selection function to be more permissive without sacrificing safety.

Conclusion and Future Work

SCG provides a robust safety filter for LLM-generated code without requiring model retraining. While the framework is computationally more intensive due to the fuzzing process and depends on the base model's underlying capabilities, it represents a significant step toward functional reliability. Future research may explore integrating these functional feedback loops directly into the training process via Reinforcement Learning to develop models that are inherently more "honest" about their logical limitations.

Summary updated

BibTeX
@misc{jeong2025selective,
  author = {Jaewoo Jeong and Taesoo Kim and Sangdon Park},
  title = {{Selective Code Generation for Functional Guarantees}},
  year = {2025},
  eprint = {2505.13553},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2505.13553},
}
Figure