Beyond Functional Correctness: Exploring Hallucinations in LLM-Generated Code
Fang Liu, Yang Liu, Lin Shi, Zhen Yang, Li Zhang, Xiaoli Lian et al. · 2026 · 354 citationsRead the paper
The rise of Large Language Models (LLMs) has significantly advanced various applications on software engineering tasks, particularly in code generation. Despite the promising performance, LLMs are prone to generate hallucinations, which means LLMs might produce outputs that deviate from users’ intent, exhibit internal inconsistencies, or misaligned with the real-world knowledge, making the deployment of LLMs potentially risky in a wide range of applications. Existing work mainly focuses on investigating the hallucination in the domain of Natural Language Generation (NLG), leaving a gap in comprehensively understanding the types, causes, and impacts of hallucinations in the context of code generation. To bridge the gap, we conducted a thematic analysis of the LLM-generated code to summarize and categorize the hallucinations, as well as their causes and impacts. Our study established a comprehensive taxonomy of code hallucinations, encompassing 3 primary categories and 12 specific categories. Furthermore, we systematically analyzed the distribution of hallucinations, exploring variations among different LLMs and benchmarks. Moreover, we perform an in-depth analysis on the causes and impacts of various hallucinations, aiming to provide valuable insights into hallucination mitigation. Finally, to enhance the correctness and reliability of LLM-generated code in a lightweight manner, we explore training-free hallucination mitigation approaches by prompt enhancing techniques. We believe our findings will shed light on future research about code hallucination evaluation and mitigation, ultimately paving the way for building more effective and reliable code LLMs in the future. The replication package is available athttps://github.com/Lorien1128/code_hallucination.
What the full text shows
Shown in the lab- Behavior Conflicting is the most frequent hallucination type across most LLMs, except for DeepSeek-R1 where it ranks third. — up to 46.1% in DeepSeek-Coder-7B (Across DeepSeek-Coder-1.3B, DeepSeek-Coder-7B, CodeLlama-7B, and GPT-4)
- DeepSeek-R1 exhibits a significantly higher proportion of Behavior Conflicting hallucinations compared to other models. — 80.3% (Compared to other tested LLMs)
- Prompt complexity has a consistent positive correlation with hallucination rates. (Complexity measured on a scale of 1-3)
- Prompt length has a non-linear relationship with hallucinations; probability decreases as length increases up to a point, then rises significantly. — increases after ~350 tokens (Prompt token length)
- The impact of prompt length on hallucination rates is largely mediated by prompt complexity. — 95% Bootstrap confidence interval (0.0113, 0.0725) (Mediation effect significance test)
- Hallucinatory code exhibits lower functional correctness (pass@1) than hallucination-free code. (Across most hallucination categories)
- Setup
- Analysis performed on LLMs including DeepSeek-Coder-1.3B, DeepSeek-Coder-7B, CodeLlama-7B, GPT-4, and DeepSeek-R1 using the CoderEval dataset.
- How
- The researchers conducted a thematic analysis of LLM-generated code to create a taxonomy of hallucinations and analyzed their distribution across models and benchmarks. They also performed correlation and mediation tests to evaluate the impact of prompt length and complexity on hallucination rates.
- Limitations
- Manual analysis relies on labelers' interpretation of prompt complexity (scale 1-3). · The study focuses on training-free mitigation (prompt enhancing) rather than model architecture changes.
Summarized by Gemma 4 from the open-access full text · 8 Nov 2026. Read it