Introduction
While Large Language Models (LLMs) have demonstrated remarkable proficiency in code generation, they frequently suffer from "code hallucinations"—outputs that appear syntactically correct and semantically plausible but fail to execute or meet the specified requirements. Unlike standard text hallucinations, which are often subjective or fact-based, code hallucinations can be objectively verified through execution. This paper introduces CodeHalu, a comprehensive framework for defining, classifying, and detecting these elusive errors, alongside a large-scale benchmark, CodeHaluEval, to evaluate the reliability of modern LLMs.
Taxonomy of Code Hallucinations
The authors argue that code hallucinations are a distinct subset of programming errors. To systematically study them, they propose a taxonomy that categorizes these failures into four primary types and eight subcategories:
- Mapping Hallucination: Misunderstanding data types or object structures (e.g., accessing non-existent dictionary keys).
- Naming Hallucination: Referencing undefined variables or non-existent external libraries.
- Resource Hallucination: Failing to account for computational boundaries like memory limits or infinite loops.
- Logic Hallucination: Discrepancies between the intended logic and the generated code, including logical "breakdowns" where the model loses context.
The CodeHalu Framework
To detect these hallucinations, the researchers developed the CodeHalu algorithm, an active, execution-based verification strategy. Rather than relying on model self-reflection, the framework executes generated code against a suite of test cases.
By analyzing execution results across multiple model outputs for the same task, the algorithm uses statistical induction to identify common hallucination patterns. If a specific failure (e.g., a recurring logic error or runtime exception) appears with high frequency across different generated samples, it is flagged as a hallucination rather than a random typo.
The CodeHaluEval Benchmark
To quantify the prevalence of these issues, the authors introduced CodeHaluEval. Built upon the APPS dataset, it contains 8,883 samples across 699 distinct tasks. Each sample is meticulously annotated with specific hallucination types, providing a granular view of where models fail.
Key Results and Insights
The evaluation of 17 popular LLMs (including GPT-4, Claude-3, and DeepSeek Coder) revealed several critical insights:
- The Syntactic Trap: LLMs have mastered syntax, with an average syntax error rate of only 0.0020 across over 100,000 samples. This confirms that syntax-based checks are insufficient for identifying hallucinations; execution is mandatory.
- Logic is the Primary Hurdle: Logic hallucinations are the most frequent type across all models. Even GPT-4, one of the top performers, exhibited a logic hallucination rate exceeding 58% on complex tasks.
- Model Performance: GPT-4 and LLaMA-3-8B were the most robust, with overall hallucination rates around 33%. However, the average rate across all tested models ranged from 20% to 60%, indicating that reliability remains a significant challenge.
- Specialized Strengths: Different models excel in different areas. For instance, Claude-3-haiku proved adept at avoiding "Naming" hallucinations, while GPT-series models were superior at "Mapping" tasks.
Conclusion
CodeHalu provides a rigorous foundation for understanding and measuring hallucinations in the code domain. By shifting the focus from syntactic correctness to execution-based truth, the framework highlights that logic remains the "final boss" for LLMs. Future work will look to expand this research into other programming languages like C++ and Rust, as well as exploring security-related hallucinations in specialized sandbox environments.
Summary updated