LLM Hallucinations in Practical Code Generation: Phenomena, Mechanism, and Mitigation
Галлюцинации больших языковых моделей при практической генерации кода: проявления, механизм и методы устранения
2025-06-22
SCID: 54.1/ndenv9g7
Discuss with AI
LLM hallucinationscode generationhallucination taxonomyrepository-level generationretrieval-augmented generation
Figures from the paper
Abstract (AI)
Code generation aims to automatically generate code from input requirements, significantly enhancing development efficiency. Recent large language models (LLMs) based approaches have shown promising results and revolutionized code generation task. Despite the promising performance, LLMs often generate contents with hallucinations, especially for the code generation scenario requiring the handling of complex contextual dependencies in practical development process. Although previous study has analyzed hallucinations in LLM-powered code generation, the study is limited to standalone function generation. In this paper, we conduct an empirical study to study the phenomena, mechanism, and mitigation of LLM hallucinations within more practical and complex development contexts in repository-level generation scenario. First, we manually examine the code generation results from six mainstream LLMs to establish a hallucination taxonomy of LLM-generated code. Next, we elaborate on the phenomenon of hallucinations, analyze their distribution across different models. We then analyze causes of hallucinations and identify four potential factors contributing to hallucinations. Finally, we propose an RAG-based mitigation method, which demonstrates consistent effectiveness in all studied LLMs.
Key Findings
1
An RAG-based mitigation method consistently reduces hallucinations across all studied LLMs.
2
Manual analysis of outputs from six mainstream LLMs establishes a taxonomy of hallucinations in generated code.
3
The authors characterize hallucination phenomena and compare their distribution across different LLMs.
4
The study examines LLM hallucinations in repository-level code generation, extending prior work beyond standalone function generation.
5
The study identifies four potential factors contributing to hallucinations in practical code generation contexts.
Research Object
LLM-generated code in repository-level code generation
Research Subject
Hallucination phenomena, mechanisms, contributing factors, and mitigation effectiveness
Publication Details
Publication Date
2025-06-22
Journal
Publisher
ISSN
Cited by
91
Open access PDF
Access Type
Author Information
Download PDF
Subscribe to digest