[Paper Review] CodeHalu: Investigating Code Hallucinations in LLMs via Execution-based Verification
This paper introduces CodeHalu, a framework that defines and classifies code hallucinations in LLMs using execution-based verification. It proposes four categories—mapping, naming, resource, and logic hallucinations—along with a dynamic detection algorithm and a benchmark of 8,883 samples across 699 tasks. Evaluation on 16 LLMs reveals significant hallucination rates, highlighting the urgent need for improved model reliability in code generation.
Large Language Models (LLMs) have made significant progress in code generation, offering developers groundbreaking automated programming support. However, LLMs often generate code that is syntactically correct and even semantically plausible, but may not execute as expected or fulfill specified requirements. This phenomenon of hallucinations in the code domain has not been systematically explored. To advance the community's understanding and research on this issue, we introduce the concept of code hallucinations and propose a classification method for code hallucination based on execution verification. We categorize code hallucinations into four main types: mapping, naming, resource, and logic hallucinations, with each category further divided into different subcategories to understand and address the unique challenges faced by LLMs in code generation with finer granularity. Additionally, we present a dynamic detection algorithm called CodeHalu designed to detect and quantify code hallucinations. We also introduce the CodeHaluEval benchmark, which includes 8,883 samples from 699 tasks, to systematically and quantitatively evaluate code hallucinations. By evaluating 17 popular LLMs using this benchmark, we reveal significant differences in their accuracy and reliability in code generation, offering detailed insights for further improving the code generation capabilities of LLMs. The CodeHalu benchmark and code are publicly available at https://github.com/yuchen814/CodeHalu.
Motivation & Objective
- To define and systematically categorize code hallucinations in LLMs based on execution correctness rather than semantic plausibility.
- To address the lack of standardized evaluation for hallucinations in code generation, which can lead to runtime errors and security vulnerabilities.
- To develop a dynamic detection algorithm that identifies hallucinations through actual code execution.
- To construct a comprehensive benchmark (CodeHalu) with 8,883 samples across 699 tasks to evaluate hallucination frequency and types in LLMs.
- To provide empirical evidence of hallucination prevalence across 16 popular LLMs, informing future model improvements for functional correctness.
Proposed method
- Proposes a definition of code hallucinations as generated code that appears syntactically correct but fails during execution or violates functional requirements.
- Classifies hallucinations into four main types: mapping, naming, resource, and logic, each with subcategories based on execution failure patterns.
- Develops a dynamic detection algorithm that executes generated code against test cases to identify hallucinations in real time.
- Constructs the CodeHalu benchmark with 699 programming tasks and 8,883 hallucination samples, each annotated with expected behavior and execution results.
- Uses execution-based verification as the core mechanism to validate correctness, distinguishing hallucinations from mere syntactic or semantic errors.
- Evaluates 16 popular LLMs on the benchmark to measure hallucination frequency and distribution across categories.

Experimental results
Research questions
- RQ1What are the primary types and subtypes of code hallucinations that occur during LLM-based code generation?
- RQ2How frequently do different LLMs produce hallucinated code when evaluated on execution-correctness criteria?
- RQ3To what extent do hallucinations in code generation stem from logical, naming, resource, or mapping errors?
- RQ4Can execution-based verification effectively detect and classify hallucinations that are syntactically valid but functionally incorrect?
- RQ5How do hallucination rates vary across different LLM architectures and prompting strategies in real-world programming tasks?
Key findings
- Code hallucinations are prevalent across 16 evaluated LLMs, with significant variation in frequency and type distribution.
- The most frequent hallucination types are logic (e.g., incorrect loop or condition handling) and resource-related (e.g., integer overflow, memory errors).
- Some models, such as MagicCoder and StarCoder, produced OverflowError and StopIteration exceptions due to incorrect handling of large integers or generator misuse.
- Execution-based verification successfully exposed hallucinations that were syntactically valid but semantically incorrect, validating the benchmark’s effectiveness.
- The benchmark revealed that even state-of-the-art LLMs fail to generate correct code in over 20% of cases when evaluated via execution.
- The study demonstrates that hallucinations are not random but follow identifiable patterns, enabling targeted mitigation strategies in future model training.

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.