TLDR: A new research paper reveals that AI models frequently hallucinate when generating commit messages and code review comments from code changes, with up to 50% of code reviews containing errors. Individual detection metrics are weak, but combining multiple metrics, especially those based on model confidence and feature attribution, significantly improves the ability to detect these AI-generated inconsistencies, offering a path for real-time detection without human references.
Artificial intelligence tools are rapidly changing how software is developed, promising to boost productivity for developers. However, these powerful tools, especially those based on large language models (LLMs), come with a significant challenge: hallucinations. Hallucinations occur when an AI generates information that is inconsistent with its input or fabricates details that don’t exist. While this problem has been studied in general natural language and code generation, its impact on tasks involving code changes – which are complex and context-dependent – has been largely unexplored until now.
A recent research paper, titled Hallucinations in Code Change to Natural Language Generation: Prevalence and Evaluation of Detection Metrics, delves into this critical issue. Authored by Chunhua Liu, Hong Yi Lin, and Patanamon Thongtanunam from the School of Computing and Information Systems at The University of Melbourne, this study provides the first comprehensive analysis of hallucinations in two key software engineering tasks: generating commit messages and code review comments from code changes.
The Scale of the Problem
The findings of the paper are quite striking. The researchers discovered that a significant portion of AI-generated content in these tasks contains hallucinations. Approximately 50% of generated code reviews and 20% of generated commit messages were found to contain these inconsistencies. This highlights a substantial risk, as such errors could mislead developers, especially junior ones, and potentially allow bugs to propagate into software.
The study identified three main categories of hallucinations:
-
Input Inconsistency: This is the most common type, where the AI’s generated natural language output contradicts the actual code change. For instance, a code review might point out a non-existent issue or a commit message might misrepresent what the code actually does.
-
Logic Inconsistency: Here, the generated text itself contains internal contradictions or illogical reasoning, regardless of the code input.
-
Intention Violation: This occurs when the AI’s output fails to meet the specific goal of the task. For example, a code review comment might simply summarize the code change instead of identifying issues or suggesting improvements.
Interestingly, code review generation showed a higher hallucination rate than commit message generation. This is likely because code reviews demand deeper reasoning and the ability to identify potential problems and suggest solutions, which is a more complex cognitive task than merely describing changes.
Detecting Hallucinations: A Challenging Endeavor
The research also explored various metric-based approaches to automatically detect these hallucinations. Initially, individual metrics, whether they compare the generated text to a human-written reference or operate without one, showed only marginal effectiveness. Their performance was only slightly better than random guessing, indicating the inherent difficulty in automatically identifying these AI errors.
However, the study found a significant improvement when multiple metrics were combined. By using a multi-metric approach, the accuracy of hallucination detection substantially increased. This suggests that different metrics capture different facets of hallucinations, and their combined insights provide a more robust detection capability.
Crucially, the paper highlights the promise of “reference-free” metrics, which do not require a human-written reference text for comparison. Metrics based on model confidence (how certain the AI is about its output) and feature attribution (how much different parts of the input code influence the generated output) proved particularly effective. This is a significant finding because it suggests that hallucinations could potentially be detected in real-time within production environments, without needing human-annotated ground truth.
For example, the researchers observed that when an AI hallucinates, it might exhibit high confidence in its output, even if that output is incorrect. Furthermore, feature attribution analysis revealed that hallucinated outputs often relied heavily on unchanged parts of the code rather than the critical changed sections, indicating a misunderstanding of the core modifications.
Also Read:
- Understanding Developer Frustrations with AI Codebase Generators
- The Hidden Truth: LLMs Deceive Even Without Prompts
Looking Ahead
This research provides a foundational understanding of hallucinations in code change to natural language generation tasks. It underscores the prevalence of these errors and demonstrates that while individual detection methods are weak, a combined approach, especially leveraging reference-free metrics like model confidence and feature attribution, offers a promising path forward. As AI models continue to evolve and become more integrated into software development workflows, understanding and mitigating hallucinations will be crucial for ensuring their reliability and trustworthiness.


