Bugs in Large Language Models Generated Code: An Empirical Study

Florian Tambon, Arghavan Moradi Dakhel, Amin Nikanjam, Foutse Khomh, Michel C. Desmarais, Giuliano AntoniolarXiv 2024

TL;DR

This paper presents a systematic empirical study of 333 bugs found in code generated by three leading Large Language Models (LLMs): Codex, CodeGen, and PanGu-Coder. The authors establish a taxonomy of 10 distinct bug patterns—many of which are fundamentally different from typical human errors—and validate their findings through a survey of 34 AI practitioners and researchers.

What Problem Does This Paper Address?

LLM-based code generators like GitHub Copilot are rapidly becoming standard fixtures in the modern software engineering workflow. However, just like human developers, LLMs write buggy code.

The critical gap is that we don't fully understand how LLM bugs differ from human bugs. Traditional quality assurance (QA) tools, such as mutation testing frameworks or linters, are optimized to catch human mistakes. If LLMs introduce entirely different types of faults due to their stochastic, generative nature, our current QA tools won't be equipped to catch them. To build better automated debugging tools and help developers safely use AI pair programmers, we need a precise characterization of unique AI-generated faults.

Methods

The authors employed an iterative open coding qualitative research methodology to analyze buggy code fragments. Their workflow proceeded as follows:

  1. Filtering: They filtered the CoderEval dataset for runnable Python functions that failed unit tests, yielding 1,997 buggy samples.
  2. Sampling: They extracted a balanced sample of 333 code fragments across three LLMs (Codex, CodeGen, PanGu-Coder).
  3. Taxonomy Generation: Two reviewers independently labeled a 10% pilot sample to build an initial codebook, resolving disagreements via a third senior reviewer. They then iteratively labeled the remaining 300 samples, resulting in a final taxonomy of 10 bug categories.
  4. Survey Validation: They conducted an online survey with 34 software practitioners and researchers to evaluate the real-world frequency, complexity, and difficulty of diagnosing/fixing these identified bug patterns.
Fig. 1: High-level view of the followed methodology.

Datasets

The study utilizes CoderEval, a benchmark dataset containing Python functions from real-world open-source GitHub projects. Unlike simpler competitive programming datasets (such as HumanEval or LeetCode), CoderEval reflects pragmatic development tasks with varying "runnable levels" (i.e., functions that depend on standard or public libraries).

Key Insights and Ideas

  • LLM bugs are fundamentally different from human bugs: Several prominent LLM bug patterns, such as Hallucinated Object (calling non-existent functions or modules) and Wrong Attribute, are rare in human code because modern IDEs catch them instantly, and humans rarely invent APIs out of thin air.
  • The "Insidious" Nature of AI Code: Bugs like Non-Prompted Consideration (where the LLM adds an unrequested feature, such as suddenly deciding to sort an array before returning it) often look perfectly correct at first glance. This can easily deceive inexperienced developers who trust the model too implicitly.
  • Model Strength Dictates Bug Type: Stronger commercial models like Codex are less prone to basic syntax errors but more prone to complex logical oversights like Missing Corner Cases (23.53% of its bugs) and Non-Prompted Consideration (11.76%). Conversely, weaker open-source models struggle heavily with basic Misinterpretation of the prompt.
  • Prompt Ambiguity Amplifies Failure: Minor formatting issues or an extra newline in a docstring—which a human developer would easily ignore—can completely derail an LLM, causing widespread misinterpretation across multiple models.

Key Results

  • Taxonomy Distribution: Across all analyzed bugs, Misinterpretation was the most frequent pattern (20.77%), followed closely by Missing Corner Cases (15.27%). Hallucinated Object, Incomplete Generation, and Silly Mistake each accounted for 9.57% of the total.
  • Practitioner Consensus: 80% of surveyed practitioners reported that they "often" or "always" encounter bugs when using LLMs to generate code.
  • Diagnosis & Fixing Difficulty: Survey respondents confirmed that Missing Corner Cases and Misinterpretations are the most complex and hardest to diagnose/fix. Meanwhile, Syntax Errors and Silly Mistakes (like identical if/else branches) were rated as trivial to catch with an IDE.
  • Real-World Correlation: The survey responses strongly correlated with the bug distribution found in Codex ($\rho = 0.47$), reflecting the fact that most practitioners primarily use OpenAI-based tools (like ChatGPT and Copilot) in their daily work.

Limitations and Future Work

  • Language and Model Scope: The study is restricted to Python and three specific older LLMs. Future work should expand the taxonomy to strongly-typed languages (where memory management or type declaration bugs occur) and newer models like GPT-4 or Llama-3.
  • Context Constraints: To avoid bias from missing context, the authors excluded functions with complex external project dependencies, potentially missing bugs related to large-scale software architecture.
  • Actionable Next Steps: The authors suggest using this taxonomy to improve Automated Program Repair (APR) tools so they can specifically target AI-generated bugs, and to design better benchmark datasets tailored to code-specific LLM flaws.

Summary updated

BibTeX
@misc{tambon2024bugs,
  author = {Florian Tambon and Arghavan Moradi Dakhel and Amin Nikanjam and Foutse Khomh and Michel C. Desmarais and Giuliano Antoniol},
  title = {{Bugs in Large Language Models Generated Code: An Empirical Study}},
  year = {2024},
  eprint = {2403.08937},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2403.08937},
}
Figure