[Paper Review] De-Hallucinator: Mitigating LLM Hallucinations in Code Generation Tasks via Iterative Grounding
De-Hallucinator mitigates LLM hallucinations in code completion by grounding predictions with project-specific API references and iterative context augmentation.
Large language models (LLMs) trained on datasets of publicly available source code have established a new state of the art in code generation tasks. However, these models are mostly unaware of the code that exists within a specific project, preventing the models from making good use of existing APIs. Instead, LLMs often invent, or "hallucinate", non-existent APIs or produce variants of already existing code. This paper presents De-Hallucinator, a technique that grounds the predictions of an LLM through a novel combination of retrieving suitable API references and iteratively querying the model with increasingly suitable context information in the prompt. The approach exploits the observation that predictions by LLMs often resemble the desired code, but they fail to correctly refer to already existing APIs. De-Hallucinator automatically identifies project-specific API references related to the model's initial predictions and adds these references into the prompt. Unlike retrieval-augmented generation (RAG), our approach uses the initial prediction(s) by the model to iteratively retrieve increasingly suitable API references. Our evaluation applies the approach to two tasks: predicting API usages in Python and generating tests in JavaScript. We show that De-Hallucinator consistently improves the generated code across five LLMs. In particular, the approach improves the edit distance by 23.3-50.6% and the recall of correctly predicted API usages by 23.9-61.0% for code completion, and improves the number of fixed tests that initially failed because of hallucinations by 63.2%, resulting in a 15.5% increase in statement coverage for test generation.
Motivation & Objective
- Motivate the problem of API hallucinations in project-specific code completion.
- Propose a grounding-based method to ground LLM predictions with API references from the target project.
- Develop an iterative prompting strategy that uses model outputs to retrieve additional context.
- Demonstrate that grounding improves API usage predictions across multiple LLMs without retraining the models.
Proposed method
- Define a retrieval-augmented prompting pipeline with increasing context quality.
- Index project API references using CodeQL and embedding-based nearest-neighbor retrieval.
- Construct augmented prompts by prepending API references to the prompt.
- Iteratively query the LLM with updated prompts until a fixed point or max iterations.
- Post-process completions to ensure syntactic correctness and focus on API usage.

Experimental results
Research questions
- RQ1RQ1: How much does De-Hallucinator improve code completions compared to the default prompt?
- RQ2RQ2: How effective is De-Hallucinator at adding the correct API references to the prompt?
- RQ3RQ3: How do the hyperparameters affect completions?
- RQ4RQ4: How efficient is De-Hallucinator and what is the contribution of each step to runtime?
Key findings
- De-Hallucinator yields consistent improvements across four state-of-the-art LLMs for code: CodeGen, CodeGen 2.5, UniXcoder, and StarCoder+.
- Edit distance improvement: 23.28%–50.64% relative to the baseline.
- Normalized edit similarity improvement: 12.12%–27.48% relative to the baseline.
- Recall of correctly predicted API usages improvement: 23.90%–60.98% relative to the baseline.
- Project-specific API grounding reduces hallucinated or non-existent API usage by grounding predictions to the target codebase.

Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.