Login Get a Demo
01 Glossary · AI supply chain

What Is Package Hallucination?

When a coding model invents a package name and an attacker registers it. The definition, the measured rates and the controls that stop it.

The short answer

What is package hallucination?

Last updated
Read time
4 min read
From
Cranium AI
Also called
Hallucinated dependency, Hallucinated package
Share on LinkedIn

Package hallucination is when a code-generating AI model recommends or references a software package that does not exist.

It becomes a supply chain risk when an attacker publishes a package under the invented name on a public repository such as PyPI or npm, so that anyone who installs the suggestion downloads the attacker’s code.

Key takeaways
  • In a 2025 USENIX Security study, 19.7% of 2.23 million generated package references pointed to packages that do not exist.
  • Average rates were at least 5.2% for commercial models and 21.7% for open-source models, across 16 models.
  • 58% of hallucinated names came back more than once in 10 repeats of the same prompt, so they are predictable.
  • OWASP calls the attack slopsquatting and covers it under LLM04:2026 Supply Chain.
By the numbers Each figure links to its sourceAll sources 
02 How it works

How does a hallucinated package become an attack?

  1. A model invents a name

    A developer or coding agent asks for code. The model returns an import or an install command for a package that sounds right and was never published.

  2. The name repeats

    In the USENIX study, 58% of hallucinated packages appeared more than once across 10 runs of the same prompt. An attacker can find the repeatable names by asking the same questions.

  3. An attacker registers it

    The attacker publishes a package under the hallucinated name on an open-source repository. OWASP calls this slopsquatting.

  4. Someone installs it

    A developer who trusts the suggestion, or an agent that installs dependencies unattended, pulls the attacker’s code into a build.

03 Compare

Package hallucination vs. similar attacks

Where the bad name comes fromWhat the attacker does
Package hallucinationA model invents a package that never existedRegisters the invented name
TyposquattingA person mistypes a real package nameRegisters the likely misspellings
Dependency confusionA build looks up an internal name on a public registryPublishes a public package under the internal name
04 Defenses

How do you reduce the risk?

01

Verify before install

Confirm that an AI-suggested dependency exists and is the package you intended before it is used. This is the mitigation OWASP gives in LLM04:2026.

OWASP LLM04:2026
02

Pin and inventory

Lock dependencies to reviewed versions, resolve them through a registry you control, and keep a bill of materials so a new, unreviewed package stands out.

CycloneDX
03

Lower the rate at the source

The study tested retrieval-augmented generation, self-refinement and fine-tuning. Fine-tuning cut one model’s hallucination rate from 16.14% to 2.66%, at the cost of diminished code quality.

Spracklen et al.
05 Keep going

Related reading.

06 Questions

Frequently asked questions.

Straight answers, kept current. Last updated .

Ask us anything →
What is slopsquatting?

Slopsquatting is the attack that exploits package hallucination. Coding assistants hallucinate plausible but nonexistent package names, and an attacker registers those names in advance so that unverified AI-suggested dependencies resolve to malicious code. OWASP describes it in LLM04:2026 Supply Chain. Its mitigation is to verify that a suggested dependency exists and is the intended package before using it.

How often do AI models hallucinate packages?

In the largest study cited here, Spracklen and colleagues generated 576,000 code samples in Python and JavaScript with 16 models. The average share of hallucinated packages was at least 5.2% for commercial models and 21.7% for open-source models, and the team recorded 205,474 unique hallucinated names. Rates differ by model, so treat these figures as a baseline, not a constant.

Is package hallucination the same as typosquatting?

No. Typosquatting depends on a person mistyping a real package name. Package hallucination needs no typing error: the model supplies a name that never existed, and the developer copies it correctly. The result is the same, an attacker-controlled package in the build, but checking the spelling of a name does not tell you whether the package was ever real.

07 See it for yourself

From the definition
to the evidence.

See how Cranium AI discovers, observes, governs, secures and proves every AI model, agent and vendor in your enterprise.