PAC Prediction Sets for Large Language Models of Code

Adam Khakhar, Stephen Mell, Osbert Bastani

Abstract

Large Language Models (LLMs) have revolutionized code generation but remain prone to "hallucinations" and subtle logic errors. This paper introduces a novel framework for providing rigorous uncertainty quantification in code generation through the use of partial programs. By replacing uncertain code segments with "holes," the authors generate outputs that come with a Probably Approximately Correct (PAC) guarantee: the correct solution is mathematically guaranteed to be among the possibilities represented by the holes with a user-defined probability. Using Abstract Syntax Trees (ASTs) and Integer Linear Programming (ILP), this method produces human-readable, localized uncertainty representations that significantly outperform traditional top-K sampling in both coverage and efficiency.

Introduction

When a developer uses an LLM for code generation, the model typically provides a single "best guess." However, if the model is uncertain, this guess may contain subtle bugs that are difficult to detect during manual review. Ideally, a model should provide a prediction set—a collection of possible answers guaranteed to contain the ground truth with high probability (e.g., 95%).

In the context of code, generating such sets is non-trivial. Because code is highly structured and the space of possible programs is exponentially large, a simple list of thousands of potential scripts is useless to a human developer. The challenge lies in representing this uncertainty in a way that is both theoretically sound and human-interpretable. This paper addresses this by proposing the use of partial programs as a medium for prediction sets.

Methodology: Partial Programs and PAC Guarantees

The authors propose representing prediction sets as snippets of code where uncertain components are replaced with "holes" (??). To ensure these holes are logical and the resulting sets are valid, the framework integrates three core components:

1. Abstract Syntax Trees (ASTs)

Rather than treating code as a flat string of text, the method operates on the AST. This ensures that holes correspond to logical units—such as an entire function argument, a specific expression, or a logic block—rather than arbitrary characters. This structural constraint ensures the output remains readable and syntactically grounded.

2. Optimization via Integer Linear Programming (ILP)

Deciding which parts of the code to "hole out" is formulated as an optimization problem. The goal is to minimize the number of nodes removed (keeping the code as specific as possible) while satisfying constraints on the number of holes and the total probability mass. The authors demonstrate that this ILP-based approach is significantly more effective than greedy algorithms, which often remove more code than necessary to achieve the same confidence level.

3. PAC Calibration

The method adapts "Probably Approximately Correct" (PAC) prediction set theory. By using a small calibration dataset, the system determines a confidence threshold for the model. This threshold ensures that the generated partial program contains the ground truth with a user-defined probability ($1-\epsilon$). A key feature of this approach is the monotonicity constraint: as the demand for confidence increases (smaller $\epsilon$), the prediction set naturally grows (more holes), maintaining the validity of the mathematical guarantee.

Experimental Evaluation

The researchers evaluated their approach across two distinct programming domains:

  • SQL Generation: Utilizing the Spider dataset (a complex text-to-SQL benchmark) with the PICARD model.
  • Python Code Completion: Utilizing the APPS and HumanEval datasets with OpenAI’s Codex model.

Key Results

  • Efficiency: On SQL tasks, at a 85% confidence level ($\epsilon = 0.15$), the ILP method only required removing approximately 45% of the AST nodes to guarantee the correct answer was included. In contrast, a greedy baseline required removing 55% of the nodes to reach the same confidence.
  • Reliability: Across all benchmarks, the method strictly maintained the PAC coverage guarantee. The ground truth was found within the suggested "holes" at a rate that consistently met or exceeded the target $1-\epsilon$ threshold.
  • Superiority to Top-K: Traditional "Top-K" methods (showing the $K$ most likely full programs) performed poorly. While Top-K might only achieve 70-74% coverage, the PAC-based partial programs can be tuned to any arbitrary confidence level, including 99%.

Discussion and Insights

One of the most significant findings of the study is the effectiveness of localized uncertainty. The authors found that setting a limit of just one hole ($m=1$) often provided sufficient coverage. This suggests that LLM errors in code are frequently localized to a single specific expression or logic block rather than being distributed throughout the entire script. By identifying and "holing out" that specific source of uncertainty, the model can provide a mostly complete program that is still mathematically reliable.

However, the method does have limitations. The utility of a partial program is inherently tied to the underlying LLM's performance; if a model is entirely incorrect about a prompt, the resulting partial program may be too sparse to be useful. Furthermore, while partial programs are more readable than long lists of alternatives, further research is needed to determine how developers interact with these "holey" programs within an integrated development environment (IDE).

Conclusion

This work provides a rigorous bridge between the probabilistic nature of LLMs and the precision required for software engineering. By transforming prediction sets into structured partial programs with PAC guarantees, the authors offer a path toward more reliable AI-assisted coding tools that can communicate their own limitations to the user.

Summary updated

BibTeX
@inproceedings{khakhar2023pac,
  author = {Adam Khakhar and Stephen Mell and Osbert Bastani},
  title = {{PAC Prediction Sets for Large Language Models of Code}},
  booktitle = {International Conference on Machine Learning},
  year = {2023},
  url = {https://openreview.net/forum?id=AnETbBowig},
}
Figure