LLM Hallucinations in Practical Code Generation: Phenomena, Mechanism, and Mitigation

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.

Authors

Institutions

Publication Details

Journal
Proceedings of the ACM on software engineering.
Published
2025-06-22
DOI
https://doi.org/10.1145/3728894
Citations
44
Primary Topic
Software Engineering Research
Type
article
Field-Weighted Citation Impact
142.83
Controls
|||
ALL TIME
JAN
FEB
MAR
APR
MAY
JUN
JUL
AUG
SEP
OCT
article

LLM Hallucinations in Practical Code Generation: Phenomena, Mechanism, and Mitigation

Zibin Zheng, Wanjun Zhong, Ensheng Shi, Yuchi Ma et al.
44 citations
Proceedings of the ACM on software engineering.
Software Engineering Research
142.83
article

LLM Hallucinations in Practical Code Generation: Phenomena, Mechanism, and Mitigation

Zibin Zheng, Wanjun Zhong, Ensheng Shi, Yuchi Ma, Zhenhua Zhang, Mingzhi Mao, Jiachi Chen, Chong Wang, Yanlin Wang
article en
44 citations

Abstract

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.

Proceedings of the ACM on software engineering.Vol. 2(ISSTA)
Sun Yat-sen University (CN), Nanyang Technological University (SG), Huawei Technologies (China) (CN)
Partnerships for the goals
Openalex Percentile: Top 0%
Software Engineering Research
142.83
AI Navigator

Ask Laika to Summarize, Analyze, and Connect papers live on the map.

Summarize Papers & Methodologies

Extract key findings, datasets, and comparative methods across publications.

Benchmark Rankings & Visual Analytics

Rank top research institutions, authors, funders, topics, and journals by Field-Weighted Citation Impact (FWCI) and paper volume with instant charts.

Connect Distant Disciplines

Bridge topological clusters on the map to find hidden collaborative intersections.