Table of Contents
Fetching ...

CodeMirage: Hallucinations in Code Generated by Large Language Models

Vibhor Agarwal, Yulong Pei, Salwa Alamir, Xiaomo Liu

TL;DR

The paper defines code hallucination in large language models and introduces a formal taxonomy of code defects. It presents CodeMirage, the first benchmark for code hallucinations, comprising 1,137 GPT-3.5-generated Python snippets drawn from HumanEval and MBPP, with labeled defect types. Through one-shot prompting, it evaluates CodeLLaMA, GPT-3.5, GPT-4, and CodeBERT, finding GPT-4 offers strong detection performance often rivaling fine-tuned baselines, especially on HumanEval. The work highlights practical mitigation strategies and sets a foundation for safer code generation with LLMs, emphasizing the need for robust detection and defense in real-world development.

Abstract

Large Language Models (LLMs) have shown promising potentials in program generation and no-code automation. However, LLMs are prone to generate hallucinations, i.e., they generate text which sounds plausible but is incorrect. Although there has been a recent surge in research on LLM hallucinations for text generation, similar hallucination phenomenon can happen in code generation. Sometimes the generated code can have syntactical or logical errors as well as more advanced issues like security vulnerabilities, memory leaks, etc. Given the wide adaptation of LLMs to enhance efficiency in code generation and development in general, it becomes imperative to investigate hallucinations in code generation. To the best of our knowledge, this is the first attempt at studying hallucinations in the code generated by LLMs. We start by introducing the code hallucination definition and a comprehensive taxonomy of code hallucination types. We propose the first benchmark CodeMirage dataset for code hallucinations. The benchmark contains 1,137 GPT-3.5 generated hallucinated code snippets for Python programming problems from two base datasets - HumanEval and MBPP. We then propose the methodology for code hallucination detection and experiment with open source LLMs such as CodeLLaMA as well as OpenAI's GPT-3.5 and GPT-4 models using one-shot prompt. We find that GPT-4 performs the best on HumanEval dataset and gives comparable results to the fine-tuned CodeBERT baseline on MBPP dataset. Towards the end, we discuss various mitigation strategies for code hallucinations and conclude our work.

CodeMirage: Hallucinations in Code Generated by Large Language Models

TL;DR

The paper defines code hallucination in large language models and introduces a formal taxonomy of code defects. It presents CodeMirage, the first benchmark for code hallucinations, comprising 1,137 GPT-3.5-generated Python snippets drawn from HumanEval and MBPP, with labeled defect types. Through one-shot prompting, it evaluates CodeLLaMA, GPT-3.5, GPT-4, and CodeBERT, finding GPT-4 offers strong detection performance often rivaling fine-tuned baselines, especially on HumanEval. The work highlights practical mitigation strategies and sets a foundation for safer code generation with LLMs, emphasizing the need for robust detection and defense in real-world development.

Abstract

Large Language Models (LLMs) have shown promising potentials in program generation and no-code automation. However, LLMs are prone to generate hallucinations, i.e., they generate text which sounds plausible but is incorrect. Although there has been a recent surge in research on LLM hallucinations for text generation, similar hallucination phenomenon can happen in code generation. Sometimes the generated code can have syntactical or logical errors as well as more advanced issues like security vulnerabilities, memory leaks, etc. Given the wide adaptation of LLMs to enhance efficiency in code generation and development in general, it becomes imperative to investigate hallucinations in code generation. To the best of our knowledge, this is the first attempt at studying hallucinations in the code generated by LLMs. We start by introducing the code hallucination definition and a comprehensive taxonomy of code hallucination types. We propose the first benchmark CodeMirage dataset for code hallucinations. The benchmark contains 1,137 GPT-3.5 generated hallucinated code snippets for Python programming problems from two base datasets - HumanEval and MBPP. We then propose the methodology for code hallucination detection and experiment with open source LLMs such as CodeLLaMA as well as OpenAI's GPT-3.5 and GPT-4 models using one-shot prompt. We find that GPT-4 performs the best on HumanEval dataset and gives comparable results to the fine-tuned CodeBERT baseline on MBPP dataset. Towards the end, we discuss various mitigation strategies for code hallucinations and conclude our work.
Paper Structure (18 sections, 1 figure, 5 tables)

This paper contains 18 sections, 1 figure, 5 tables.

Figures (1)

  • Figure 1: An example of robustness issue in the LLM generated code. It will raise "FileNotFoundException" in case the file does not exist.