TL;DR
Large Language Models (LLMs) frequently "hallucinate" by generating code that looks plausible but is functionally broken. This paper introduces CODEREFUSER, a framework that allows LLMs to safely say "I don't know" by abstaining from tasks they are unlikely to solve correctly. By combining runtime execution consistency with statistical hypothesis testing, CODEREFUSER provides rigorous risk guarantees without needing ground-truth test cases during deployment.
What Problem Does This Paper Address?
While LLMs have revolutionized automated code generation, ensuring their reliability remains a massive challenge. Most existing research focuses on sample-level hallucination detection—evaluating whether a specific generated code snippet is correct. However, the authors argue that many hallucinations stem from a more fundamental source: the task itself. Ambiguous prompts or inherent model limitations mean certain problems are simply beyond an LLM's current capabilities.
This paper tackles the task abstention problem: determining whether an LLM should refuse a code generation task entirely to avoid generating faulty code. The core challenge is making this decision accurately at inference time without relying on human-authored ("oracle") test cases or external databases, all while accounting for the fact that semantically identical code can look completely different syntactically.
Methods
The authors propose CODEREFUSER, a framework built upon the Learn Then Test (LTT) statistical framework. It operates in two distinct phases:
- Calibration Phase: Using a small calibration dataset equipped with oracle test cases, CODEREFUSER evaluates the LLM's performance across various candidate thresholds. It applies multiple hypothesis testing to find a set of valid thresholds ($\Lambda_{valid}$) that mathematically guarantee the risk of admitting a failing task stays below a user-specified tolerance level ($\alpha$) with high confidence ($1-\delta$).
- Testing Phase (Inference): For a new, unseen prompt, the LLM generates multiple code samples and a set of potential test cases. To handle flawed, model-generated tests, CODEREFUSER introduces Sample-Test Dual Filtering (STDF). STDF filters out "toxic" or invalid test cases by analyzing execution error rates and output diversity across the generated code population. The remaining code samples are then clustered based on their execution outputs on the surviving test cases.
Finally, CODEREFUSER applies the calibrated abstention rules using one of two scoring modes: Cluster Ratio (CR) (which enforces model consensus) or Semantic Entropy (SE) (which limits uncertainty).
Datasets
The authors evaluated their approach using four open-source code LLMs (DeepSeek-Coder-33B, Qwen2.5-Coder-32B, CodeLlama-7B, and WizardCoder-33B) on two standard Python benchmarks:
- HumanEval: 164 hand-crafted programming problems, each associated with 5–10 oracle test cases.
- MBPP (Mostly Basic Python Problems): A larger dataset of nearly 1,000 problems, where each task includes a short description and 3 test cases.
Key Insights and Ideas
- Static Metrics Fail for Code: Traditional natural language processing metrics (like perplexity or textual self-consistency) underperform because code has unique semantic properties. Two code snippets can be textually completely different yet functionally identical. Runtime execution is essential for reliable risk estimation.
- The Power of Dual Filtering: Naively relying on LLM-generated test cases introduces severe evaluation noise because invalid inputs trigger undefined behaviors, artificially inflating the perceived inconsistency of the model. STDF elegantly solves this by using the collective behavior of the code samples to audit and prune bad test cases.
- Robust Transferability: Although the underlying statistical framework theoretically assumes data is independent and identically distributed (i.e., from the same dataset), the calibrated rules transfer remarkably well across different datasets (e.g., calibrating on MBPP and testing on HumanEval) with minimal performance drops.
Key Results
- Superior Accuracy: CODEREFUSER significantly outperformed all baselines. On average, it achieved a 26.5% absolute improvement in abstention precision and an 11.9% absolute improvement in F1-score compared to the best existing competitor.
- Efficiency Over Scale: Static methods failed to close the performance gap even when given a $4\times$ larger sampling budget ($N=256$ vs. $N=64$ for CODEREFUSER), proving that smart execution-based filtering beats brute-force scaling.
- Empirical Risk Control: Across different risk tolerances, the empirical admission risk was reliably kept below the target threshold in over 90% of cases, validating the framework's theoretical guarantees.
Limitations and Future Work
- Limited to Deterministic Systems: The core mechanism relies on clustering identical execution outputs. This assumption may not hold for complex, interactive, or non-deterministic software systems.
- Single Language Evaluation: The current experiments were exclusively conducted in Python. Further evaluation is required to see how well the method handles languages with different paradigms or memory management styles, such as C or Java.
- Inference Overhead: Generating multiple code samples and test cases at inference time incurs higher computational costs than simple static checks, though it remains more efficient than massive sample scaling.
Summary updated