LLM Hallucinations in Practical Code Generation: Phenomena, Mechanism, and Mitigation
2024/09/30 by Ziyao Zhang, Yanlin Wang, Zhang, Ziyao +7 · 48 citations
Computer Science · Engineering · #Advancements in Photolithography Techniques #Artificial Intelligence (cs.AI) #Computation and Language (cs.CL) #FOS: Computer and information sciences #Low-power high-performance VLSI design #Software Engineering (cs.SE) #VLSI and Analog Circuit Testing
paper · pdf · doi:10.48550/arxiv.2409.20550
openalex publication_date 2024/09/30 · openalex created_date 2025/10/10 · openalex updated_date 2026/07/28
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. The replication package including code, data, and experimental results is available at https://github.com/DeepSoftwareAnalytics/LLMCodingHallucination
Cited by
- Beyond GDPR: Examining Disclosure Gaps in Mobile AR Privacy Policies under U.S. State Privacy Laws
- A Taxonomy of Confabulations and the Perception-Reality Gap in LLM-Assisted Immersive Scene Editing
- Bridging the Gap on AI-Assisted Scientific Software Development Through Transparency and Traceability
- Anota: Identifying Business Logic Vulnerabilities via Annotation-Based Sanitization
- Aligning Academia with Industry: An Empirical Study of Industrial Needs and Academic Capabilities in AI-Driven Software Engineering
- SimpleDevQA: Benchmarking Large Language Models on Development Knowledge QA
- A Hybrid Approach for EMF Code Generation:Code Templates Meet Large Language Models
- An Empirical Study of Agent Developer Practices in AI Agent Frameworks
- CodeFuse-CommitEval: Towards Benchmarking LLM's Power on Commit Message and Code Change Inconsistency Detection
- SLMFix: Leveraging Small Language Models for Error Fixing with Reinforcement Learning
- From LLMs to Agents: A Comparative Evaluation of LLMs and LLM-based Agents in Security Patch Detection
- A Systematic Literature Review of Code Hallucinations in LLMs: Characterization, Mitigation Methods, Challenges, and Future Directions for Reliable AI
- Model-Driven Requirements Configuration with Three-Valued Uncertainty Scoring
- FELA: A Multi-Agent Evolutionary System for Feature Engineering of Industrial Event Log Data
- LSPRAG: LSP-Guided RAG for Language-Agnostic Real-Time Unit Test Generation
- EffiReasonTrans: RL-Optimized Reasoning for Code Translation
- Will AI also replace inspectors? Investigating the potential of generative AIs in usability inspection
- Grounded AI for Code Review: Resource-Efficient Large-Model Serving in Enterprise Pipelines
- Past, Present, and Future of Bug Tracking in the Generative AI Era
- MOSAIC: Multi-agent Orchestration for Task-Intelligent Scientific Coding
- RAVEN: Realtime Accessibility in Virtual ENvironments for Blind and Low-Vision People
- Model-Agnostic Correctness Assessment for LLM-Generated Code via Dynamic Internal Representation Selection
- HalluGuard: Evidence-Grounded Small Reasoning Models to Mitigate Hallucinations in Retrieval-Augmented Generation
- ReSeek: A Self-Correcting Framework for Search Agents with Instructive Rewards
- Intent-Driven Storage Systems: From Low-Level Tuning to High-Level Understanding
- Takedown: How It's Done in Modern Coding Agent Exploits
- Investigating Security Implications of Automatically Generated Code on the Software Supply Chain
- SLICET5: Static Program Slicing using Language Models with Copy Mechanism and Constrained Decoding
- Securing LLM-Generated Embedded Firmware through AI Agent-Driven Validation and Patching
- EyeMulator: Improving Code Language Models by Mimicking Human Visual Attention
- ARSP: Automated Repair of Verilog Designs via Semantic Partitioning
- D-LiFT: Improving LLM-based Decompiler Backend via Code Quality-driven Fine-tuning
- Charting the Future of Scholarly Knowledge with AI: A Community Perspective
- HiFo-Prompt: Prompting with Hindsight and Foresight for LLM-based Automatic Heuristic Design
- Hallucination in LLM-Based Code Generation: An Automotive Case Study
- CodeGrad: Integrating Multi-Step Verification with Gradient-Based LLM Refinement
- Web3 x AI Agents: Landscape, Integrations, and Foundational Challenges
- Blueprint First, Model Second: A Framework for Deterministic LLM Workflow
- Curiosity by Design: An LLM-based Coding Assistant Asking Clarification Questions
- Benchmarking generative AI tools for literature retrieval and summarization in genomic variant interpretation
- Towards Formal Verification of LLM-Generated Code from Natural Language Prompts
- HCRMP: A LLM-Hinted Contextual Reinforcement Learning Framework for Autonomous Driving
- SAFE: Improving LLM Systems using Sentence-Level In-generation Attribution
- An AI-Powered Research Assistant in the Lab: A Practical Guide for Text Analysis Through Iterative Collaboration with LLMs
- Towards Mitigating API Hallucination in Code Generated by LLMs with Hierarchical Dependency Aware
- Hallucination by Code Generation LLMs: Taxonomy, Benchmarks, Mitigation, and Challenges
- An Empirical Study of Policy-as-Code Adoption in Open-Source Software Projects
- A Comprehensive Survey on LLM‐Based Network Management and Operations
Related