What is package hallucination?
Package hallucination is a code-generating language model naming and using in code a software package or library that does not exist; attackers can publish malicious packages under frequently hallucinated names to reach developers who install them.
It is a special, dangerous form of hallucination in the software supply chain: an ordinary wrong fact can be read and caught, but a wrong package name becomes an install command that can run attacker code. It resembles typosquatting, except the model rather than a person makes the error.
- Topic
- AI security
- Also known as
- Slopsquatting, hallucinated package
How package hallucination works
- A developer asks a language model for code; it imports a plausible but non-existent package or adds it to an install command.
- The same invented names can be suggested repeatedly to different users.
- An attacker identifies frequently invented names and publishes malicious packages under them on registries.
- When the developer or a coding agent runs the command, the malicious package installs and its code runs.
In more detail
Repeatability and slopsquatting
Researchers also found high repeatability: around 43% of hallucinated package names appeared consistently across repeated generations, often arising from plausible naming mashups (combining two real libraries), deprecated packages or cross-language confusion (suggesting a Python package name for JavaScript). This predictability is what enables attackers to pre-register high-frequency invented names, a practice known as slopsquatting.
Install-time code execution risk (setup.py, postinstall)
Crucially, malicious execution happens at installation time, not just when the library is imported, because package managers like pip and npm can run arbitrary setup.py or postinstall lifecycle scripts automatically during installation. Coding agents running install commands without sandboxing or human approval turn a text hallucination directly into remote code execution (RCE).
Caching proxies versus allow-listed registries and hash pinning
An internal mirror only protects against package hallucination if upstream public fallback/caching is disabled (strict allow-list curation); otherwise, a pass-through proxy will fetch the attacker's newly published public package automatically. Effective defence combines curated private registries, lockfiles with cryptographic hashes and registry existence/age/maintainer checks before any install.
Commonly confused terms
| Term | How it differs |
|---|---|
| Model hub typosquatting | Typosquatting exploits human spelling mistakes of real popular package names (e.g. 'reqeusts' instead of 'requests'); slopsquatting (package hallucination exploitation) targets plausible, non-existent names repeatedly invented by LLMs (e.g. 'pypdf-extractor-utils'). |
| Dependency confusion | Dependency confusion occurs when a build or package manager resolves an internal private package name against a public registry (often due to a higher public version number or fallback misconfiguration); package hallucination involves names that do not exist anywhere internally or publicly until an attacker registers them. |
| API hallucination | API or method hallucination happens when an LLM invents a non-existent function or parameter inside a real installed library (causing a runtime error), whereas package hallucination invents the entire installable package name itself (creating an arbitrary code execution risk during installation). |
Why package hallucination matters for on-premises AI
One study of 576,000 code samples from 16 code-generating models reported average hallucinated package rates of at least 5.2 per cent for commercial models and 21.7 per cent for open-source ones, with 205,474 unique invented names. Controls: installing only from an approved internal repository or allow list, reviewing new dependencies, limiting agents' install rights and dependency scanning.
A software team's coding agent suggests a non-existent library for a data conversion; since the company installs packages only from an allow-listed internal registry, installation fails and the agent notices, producing a solution with a known library.
Common questions
How common is package hallucination?
One study found average invented package rates of at least 5.2 per cent for commercial models and 21.7 per cent for open-source ones, with 205,474 unique invented names.
How do you guard against package hallucination?
Install only from an approved internal repository or allow list, review new dependencies and limit coding agents' install rights.
Related terms
- Insecure AI code generation
- AI supply chain security
- Hallucination
- Model hub typosquatting
- Code execution tool
- AI agent unexpected code execution
Sources
- Spracklen et al., We Have a Package for You! A Comprehensive Analysis of Package Hallucinations by Code Generating LLMs, 2024
- Ohm et al., Backstabber's Knife Collection: A Review of Open Source Software Supply Chain Attacks, 2020
In the glossary: Glossary, M to Q