Seedlabs
Source research

We Have a Package for You! A Comprehensive Analysis of Package Hallucinations by Code Generating LLMs

Joseph Spracklen, Raveen Wijewickrama, A. H. M. Nazmus Sakib, Anindya Maiti, Viswanath, Bimal, Jadliwala, Murtuza · 2024 · 48 citationsRead the paper

The reliance of popular programming languages such as Python and JavaScript on centralized package repositories and open-source software, combined with the emergence of code-generating Large Language Models (LLMs), has created a new type of threat to the software supply chain: package hallucinations. These hallucinations, which arise from fact-conflicting errors when generating code using LLMs, represent a novel form of package confusion attack that poses a critical threat to the integrity of the software supply chain. This paper conducts a rigorous and comprehensive evaluation of package hallucinations across different programming languages, settings, and parameters, exploring how a diverse set of models and configurations affect the likelihood of generating erroneous package recommendations and identifying the root causes of this phenomenon. Using 16 popular LLMs for code generation and two unique prompt datasets, we generate 576,000 code samples in two programming languages that we analyze for package hallucinations. Our findings reveal that that the average percentage of hallucinated packages is at least 5.2% for commercial models and 21.7% for open-source models, including a staggering 205,474 unique examples of hallucinated package names, further underscoring the severity and pervasiveness of this threat. To overcome this problem, we implement several hallucination mitigation strategies and show that they are able to significantly reduce the number of package hallucinations while maintaining code quality. Our experiments and findings highlight package hallucinations as a persistent and systemic phenomenon while using state-of-the-art LLMs for code generation, and a significant challenge which deserves the research community's urgent attention.

What the full text shows

Shown in the lab
  • Overall package hallucination rate across all tested models — 19.7% (440,445 out of 2.23 million packages) (Python and JavaScript code samples)
  • Average hallucination rate for open-source models vs. commercial models — 21.7% vs. 5.2% (Across 16 popular LLMs)
  • Number of unique non-existent package names generated — 205,474 unique packages (PyPI and npm repositories)
  • Hallucination rate by programming language — JavaScript (21.3%) vs. Python (15.8%) (Average across tested models)
  • Lowest overall hallucination rate among tested models — 3.59% (GPT-4 Turbo)
  • Best hallucination rate among open-source models — 13.63% (DeepSeek 1B)
Setup
576,000 code samples generated from 16 LLMs using 9,600 Stack Overflow prompts per language and ~4,800 LLM-generated prompts per language for Python and JavaScript.
How
The researchers prompted 16 LLMs using two datasets (Stack Overflow questions and Llama-2 generated prompts based on package descriptions) to produce 576,000 code samples. They then parsed the code and cross-referenced recommended packages against PyPI and npm repositories to detect hallucinations.
Limitations
Funding constraints limited the number of commercial models tested · Newer models released after the study may have different hallucination tendencies · Analysis for RQs 2-4 was narrowed to Python only

Summarized by Gemma 4 from the open-access full text · 9 Nov 2026. Read it