Source-linked AI summary
When Uncertainty Isn't Enough: An Empirical Study of Self-Correction in Code Generation
Pranav Rakasi, Maanas Lalwani, Arnav Srivastava, Arya Palanivel, Tinuade Adeleke, Ruizhe Li, Sean Wu
TL;DR
Code LLM failures are difficult to predict, raising the question of whether natural-language uncertainty methods can identify incorrect code and enable cheap self-correction. The paper systematically compares five uncertainty estimators and evaluates their use in selective correction. Uncertainty-guided correction degrades Pass@1 in most tested configurations, whereas verification-based self-correction reliably improves accuracy.
Problem
The paper asks whether uncertainty methods transfer effectively to code generation and can improve coding assistants while serving as cheap correctness proxies.
Method
The study systematically compares five uncertainty estimators and uses uncertainty scores to assess selective self-correction for generated code.
Results
Uncertainty-based self-correction degrades Pass@1 in 5 of 6 configurations, while verification-based self-correction is the only policy that reliably improves accuracy.
Takeaways & Limitations
Uncertainty signals and execution verification are complementary, with uncertainty best used to selectively trigger verification rather than replace it.
Takeaways & Limitations
The experiments use only small 1.3B–3B-parameter models, so larger models may exhibit different uncertainty profiles and self-correction benefits.
Abstract
from arXiv · showhide
Large language models for code generation often produce incorrect solutions without reliable indicators of failure. We study whether uncertainty estimation methods developed for natural language transfer to code generation, and whether such signals can improve code generation via selective self-correction. We evaluate five uncertainty methods: mean token entropy, verbalized confidence, $P(\text{True})$, entropy ensembles, and semantic entropy probes, across three small code LLMs on HumanEval and BigCodeBench. We find that multi-sample $P(\text{True})$ achieves the strongest correlation with correctness, while all the other methods, including semantic entropy probes, yield only weak correlation. We then use these uncertainty signals to drive three self-correction policies: adaptive decoding, uncertainty-based regeneration, and verification-based regeneration. Our results reveal a stronger negative finding than anticipated: uncertainty-based self-correction fails to reliably improve Pass@1, degrading accuracy in 5 of 6 configurations across both benchmarks ($-3$pp to $-10$pp), and adaptive decoding degrades accuracy in 4 of 6 configurations. Only verification-based self-correction reliably improves Pass@1, with gains of $+6$ to $+26$ percentage points on HumanEval and $+8$ to $+20$ percentage points on BigCodeBench, scaling inversely with baseline strength. These findings replicate consistently across both benchmarks and suggest that cheap uncertainty estimators are insufficient on their own to improve code correctness, and that their practical value lies in serving as gating signals for costlier execution-based correction loops rather than as standalone substitutes for verification.
1. Introduction
Uncertainty estimation could provide a cheaper correctness proxy for code generation, but code-specific semantics may prevent natural-language methods from transferring reliably. This study evaluates such methods and self-correction policies, finding that weak uncertainty signals can harm accuracy while verification-based correction helps.
- Problem: Code-generation failures are unpredictable, and current assistants treat risky and reliable generations alike without warning users.A model may succeed on one task yet produce subtle bugs or entirely incorrect solutions on another.
- Motivation: Uncertainty estimation could flag unreliable code more cheaply than compiling and testing, trading some accuracy for lower latency and resource use.The proposed proxy is relevant when real-time or resource-constrained systems cannot wait for execution-based validation.
- Research questions and contributions: The study systematically compares five uncertainty methods and asks whether natural-language estimators transfer to code generation and improve coding assistants.Code’s unique semantics—including single-token failures and functional equivalence across syntactically different programs—motivate code-specific investigation.
- Uncertainty estimation: Multi-sample P(True) is the only evaluated method with strong correctness correlation, while single-forward-pass estimators yield weak correlation and verbalized confidence is unreliable for small models.Multi-sample P(True) achieves this stronger signal at substantially higher cost.
- Self-correction results: Uncertainty-based self-correction degrades Pass@1 in 5 of 6 configurations, and adaptive decoding degrades accuracy in 4 of 6 configurations across HumanEval and BigCodeBench.Verification-based self-correction is the only reliably beneficial policy, indicating that uncertainty is most useful for selectively triggering execution feedback.
2. Background and Related Works
Prior work groups LLM uncertainty estimation into decoding-probability, verbalized-confidence, and ensembling approaches, with differing computational costs and reliability. In code generation, existing studies report weak uncertainty–correctness relationships but potential benefits for abstention and motivate self-correction.
- Uncertainty Estimation in LLMs: Decoding-based methods use internal probability signals, including Mean Token Entropy and semantic-dispersion measures, to estimate uncertainty.Mean Token Entropy aggregates token-level Shannon entropy across a sequence and is widely used for hallucination detection and reliability scoring.
- Uncertainty Estimation in LLMs: Semantic entropy methods improve uncertainty estimation but require multiple generations, whereas semantic entropy probes approximate them from one generation with nearly zero overhead.The probes use hidden states and achieve comparable performance according to the cited passage.
- Uncertainty Estimation in LLMs: Verbalized Confidence Scores require models to report confidence without hidden-state access, but prior studies disagree between reasonable calibration and systematic overconfidence.The cited studies provide evidence for both sides of this reliability debate.
- Uncertainty Estimation in LLMs: Bayesian Prompt Ensembles combine semantically equivalent prompts through weighted Bayesian variational inference, but require multiple LLM calls and are difficult to apply to long-form code generation.This work adapts the method with design choices intended to control cost and fit the task.
- Uncertainty Estimation for Code Generation: In code generation, prior work found weak negative correlation between uncertainty and correctness, while uncertainty-based abstention reduced incorrect outputs to near-zero.The present work builds on this direction by using uncertainty for self-correction rather than abstention.
3. Methodology
The methodology evaluates scalar uncertainty estimates for code solutions and uses them to selectively trigger self-correction. It combines verifier-based correctness assessment with uncertainty-driven regeneration and adaptive decoding across multiple models and benchmarks.
- Evaluation framework: Each generated solution receives a scalar uncertainty score U(x), with correctness determined by a binary functional-correctness oracle based on unit tests.The goal is for higher uncertainty to indicate a higher probability of failure and support selective self-correction.
- Uncertainty estimation: The study evaluates mean token entropy, verbalized confidence, verifier-based P(True), entropy ensembles, and semantic entropy probes as uncertainty estimators.Methods produce uncertainty scores using token probabilities, prompted confidence, stochastic candidate verification, prompt variation, or internal representations.
- Uncertainty estimation: Verifier-based P(True) estimates correctness as the empirical success probability of K independently sampled candidates evaluated by an external verifier.Unlike self-evaluated confidence, it measures observable task correctness through test outcomes.
- Uncertainty estimation: Entropy ensembles generate one completion per semantically equivalent prompt and summarize the resulting mean-token-entropy values to capture prompt-induced uncertainty and disagreement.A logistic regression model calibrates these statistics against functional correctness to produce U(x).
- Uncertainty estimation: Semantic entropy probes cluster multiple solutions by functional behavior and use hidden-state representations, including SLT and TBG, to predict high or low semantic entropy.SLT assesses uncertainty after solution generation, whereas TBG assesses it before token generation.
- Self-correction and evaluation: Self-correction uses uncertainty-triggered regeneration, verification-triggered regeneration, or TBG-based adaptive decoding, with correction attempts capped at N=5.Experiments cover HumanEval and BigCodeBench using ∼3B instruct variants of LLaMA 3, Qwen2.5-Coder, and DeepSeek-R1, measuring correlation, calibration, pass@k, and latency.
4. Results and Discussion
P(True) was the strongest uncertainty signal, but uncertainty-guided correction generally reduced Pass@1 across HumanEval and BigCodeBench. Verification-based self-correction was the only policy that consistently improved accuracy, especially for weaker baseline models.
- Uncertainty estimation: P(True) achieved the strongest correctness correlations across all models: 0.842, 0.782, and 0.303 for Llama, Qwen, and DeepSeek.All other methods showed weak negative correlations ranging from −0.111 to −0.631.
- Uncertainty estimation: Multi-sample P(True) required K stochastic rollouts, while cheaper single-pass alternatives produced substantially weaker correctness correlations.Semantic entropy probes were selected for self-correction because they had the lowest overhead.
- HumanEval: −3.23pp to −6.45pp: adaptive decoding degraded Pass@1 in five of six HumanEval configurations, with only Llama-3.2-3B under TBG gating improving by +3.23pp.The positive result occurred when uncertainty gating was applied once before decoding rather than repeatedly during decoding.
- HumanEval: −3.23pp to −9.68pp: uncertainty-based self-correction degraded Pass@1 in every HumanEval configuration by replacing some correct greedy outputs with incorrect stochastic generations.The probe signal was too nonspecific to identify which uncertain outputs would benefit from regeneration.
- HumanEval: +25.81 percentage points: verification-based self-correction improved DeepSeek-Coder-1.3B from 0.61 to 0.87 at 2.4× latency overhead, the largest HumanEval gain.Llama-3.2-3B improved by +6.45 percentage points, while Qwen2.5-Coder-3B improved by approximately 6 percentage points.
- BigCodeBench generalization: +7.74 to +20.00 percentage points: verification-based self-correction reliably improved Pass@1 on BigCodeBench, while uncertainty-based correction was unreliable and adaptive decoding mostly degraded performance.DeepSeek-Coder-1.3B again achieved the largest gain, +20.00pp, consistent with weaker models having more headroom.
Limitations and Future Work
The study’s limitations concern its reliance on small models and benchmark settings that omit aspects of real-world code generation. Results also indicate that verification-based correction remains the only consistently improving policy across both evaluated benchmarks.
- Limitations: 1.3B–3B-parameter models limit generalizability because larger models may exhibit different uncertainty profiles and respond differently to self-correction.The experiments use only small models.
- Limitations: Real-world code generation involves longer programs, partial specifications, and incomplete tests, making uncertainty estimation and verification more challenging and potentially more valuable.The study evaluates HumanEval and BigCodeBench, but these conditions are not fully represented by the benchmarks.
- Future Work: Verification-based self-correction improves Pass@1 in every HumanEval configuration, whereas adaptive decoding degrades it in two of three and uncertainty-based self-correction in all three.These results are reported for 31 held-out HumanEval problems.
- Future Work: 0.18–0.30 BigCodeBench baseline Pass@1 versus 0.38–0.81 on HumanEval reflects the higher difficulty of BigCodeBench.On BigCodeBench, verification-based self-correction improves Pass@1 in all three configurations, while adaptive decoding degrades it in two of three and uncertainty-based self-correction in two of three.
A. Appendix A · A.1. Uncertainty Analysis Details
The appendix details how uncertainty relates to correctness across models and estimators. Separation is strongest for calibrated profiles and P(True), while overlap and overconfidence limit threshold-based self-correction.
- A. Appendix A · A.1. Uncertainty Analysis Details: Correct generations (n=106) peak at low uncertainty (0.10–0.12 nats), while incorrect ones (n=58) have broader overlapping distributions.The overlap requires careful threshold calibration.
- A. Appendix A · A.1. Uncertainty Analysis Details: High overconfidence in incorrect predictions (n=79 vs n=85 correct) produces substantial overlap for Llama.Low uncertainty among incorrect predictions helps explain mixed adaptive-decoding results.
- A. Appendix A · A.1. Uncertainty Analysis Details: Correct generations (n=139) concentrate at low uncertainty, while few incorrect samples (n=25) support effective self-correction strategies for Qwen.This is described as a well-calibrated uncertainty profile.
- A. Appendix A · A.1. Uncertainty Analysis Details: Correct generations (n = 77) receive high P(True), while most incorrect samples (n = 23) receive low P(True), indicating well-calibrated estimates.The analysis uses pass@3 verification.
- A. Appendix A · A.1. Uncertainty Analysis Details: Correct generations (n = 55) receive consistently higher confidence than incorrect ones (n = 45), demonstrating effective uncertainty separation.The P(True) analysis uses pass@3 verification.
- A. Appendix A · A.1. Uncertainty Analysis Details: Despite low overall accuracy (n = 6 correct, n = 94 incorrect), the verifier assigns near zero confidence to most incorrect solutions.This indicates informative uncertainty estimates for DeepSeek-R1-Distill-Qwen-1.5B.
- A. Appendix A · A.1. Uncertainty Analysis Details: Correct and incorrect generations overlap substantially at low predicted failure probability, indicating overconfidence and limited threshold-based self-correction separability.DeepSeek-Coder-1.3B-Instruct instead concentrates correct generations at lower uncertainty, but incorrect outputs remain broader and overlapping, requiring threshold calibration.
A.2. Prompt Templates
The appendix specifies the exact prompt templates used for mean token entropy, verbalized confidence, and P(True), including generation, self-evaluation, and optional regeneration prompts. Model-specific formatting and external test execution are documented for reproducibility.
- A.2. Prompt Templates: Prompt templates significantly influence model behavior and are documented for each uncertainty-estimation method.
- A.2.1. MEAN TOKEN ENTROPY (MTE): MTE reuses baseline greedy-decoding prompts, with chat formatting for Llama and DeepSeek and plain-text formatting for Qwen.
- A.2.2. VERBALIZED CONFIDENCE PROMPTS: Verbalized confidence prompts ask models to generate Python code and report a scalar confidence score for each candidate program.
- A.2.2. VERBALIZED CONFIDENCE PROMPTS: HumanEval candidates are evaluated by executing the benchmark prompt, generated code, and unit tests under a timeout constraint.
- A.2.3. P(TRUE) PROMPTS: P(True) uses generation and stochastic-rollout prompts, then asks the model to judge whether each candidate passes all tests with exactly True or False.
- A.2.3. P(TRUE) PROMPTS: Optional P(True)-guided regeneration appends “The previous attempt failed. Try a different approach,” after which the program is externally verified and a new verifier-based P(True) is estimated.
B. APPENDIX B · B.1. Probe Design Choices
Semantic entropy probes use layered features, nonlinear classification, standardized inputs, and model-specific thresholds to improve uncertainty estimation. The design yields test AUROC values of 0.62–0.89 while addressing feature integration, overfitting, and precision-recall variation.
- B.1. Probe Design Choices: Semantic entropy probes incorporate several design choices intended to improve uncertainty estimation accuracy.These choices include feature concatenation, nonlinear classification, standardization, and model-specific thresholds.
- B.1. Probe Design Choices: 6144–9216-dimensional vectors concatenate features from the last three transformer layers, combining syntactic and semantic information.The layers are [−3, −2, −1], with earlier layers contributing syntactic information and later layers contributing semantic information.
- B.1. Probe Design Choices: Multi-layer feature concatenation improves probe accuracy compared with single-layer features.The approach is designed to capture complementary information across transformer depth.
- B.1. Probe Design Choices: 0.62–0.89 test AUROC is achieved by a 3-layer MLP classifier with architecture 256 →128 →64.The MLP enables nonlinear feature interactions, while early stopping prevents overfitting.
- B.1. Probe Design Choices: Standardized features ensure equal contribution from all transformer layers.Feature standardization is applied before probe classification.
- B.1. Probe Design Choices: 0.30–0.70 F1-optimized thresholds account for model-specific uncertainty distributions.These thresholds improve precision-recall trade-offs across models.
B.2. Threshold Tuning
Thresholds for uncertainty-guided decoding are tuned on held-out data by maximizing F1 across candidate values from 0.3 to 0.7. The resulting thresholds vary by model and feature method, reflecting differing uncertainty distributions.
- Threshold Tuning: Threshold tuning evaluates 0.3, 0.4, 0.5, 0.6, and 0.7 using precision, recall, F1, accuracy, and trigger rate.The selected threshold maximizes F1, balancing avoidance of unnecessary corrections with detection of truly uncertain generations.
- Threshold Tuning: 0.7 is used by Qwen2.5-Coder-3B-Instruct with SLT features, achieving F1 0.96 and accuracy 0.96.These thresholds are model- and feature-method-specific.
- Threshold Tuning: 0.3 is used by DeepSeek-Coder-1.3B-Instruct with TBG features, achieving F1 0.78 and accuracy 0.80.The tuned range reflects varying uncertainty distributions across models.