Package hallucination is the failure mode in which a language model recommends or imports a software package that does not exist in the relevant registry, such as PyPI or npm. In AI for coding, it matters because a developer or coding agent that installs the invented name may receive a malicious package that an attacker registered under exactly that name.
The attack that exploits this gap is called slopsquatting, a term coined in 2025 as a play on typosquatting. Unlike a typo, the fake name comes from the model itself, often plausible, repeatable and confidently presented, which makes package hallucination one of the most concrete software supply chain risks created by AI-generated code.
Why language models invent package names
Package hallucination arises because a code model predicts likely tokens, not verified facts. When asked for a library that solves a task, the model composes a name that fits the naming patterns it has seen, such as combining a framework name with a function word, even when no registry entry matches.
Several factors raise the rate. Training data has a knowledge cutoff, so packages that were renamed, removed or never published blend with real ones. Niche tasks have sparse training examples, and higher sampling temperature increases invention. Prompts that ask for a library in a specific ecosystem, rather than for code in general, also push the model toward naming a dependency it cannot verify.
The table below lists the common forms the hallucinated names take.
| Pattern | How the name is formed | Why it is convincing |
|---|---|---|
| Conflation | Merges two real package names | Both parts look familiar |
| Cross-ecosystem transfer | Uses a real npm name in a Python context | The name exists elsewhere |
| Descriptive invention | Describes the task as a name | Matches common naming habits |
| Outdated reference | Cites a removed or renamed package | It once existed |
| Near-miss variant | Changes a suffix or separator | Resembles the real package closely |
How slopsquatting turns hallucinations into attacks
Slopsquatting works only because hallucinations are partly reproducible. Research published in 2024 and 2025 that generated several hundred thousand code samples found that roughly one in five recommended packages did not exist across the tested models, with open-source models hallucinating far more often than commercial ones. Many invented names recurred when the same prompt was repeated, which lets an attacker predict which names to register.
The attack chain follows a short sequence of steps, summarized in the next table.
| Step | Attacker action | Victim exposure |
|---|---|---|
| Harvest | Prompts models repeatedly and records invented names | None yet |
| Register | Publishes packages under the most frequent names | Registry now contains the trap |
| Wait | Lets developers and agents request the name | Model suggests the same name |
| Execute | Runs code in install scripts or at import time | Credentials, tokens or builds compromised |
Security researchers have demonstrated the idea with harmless placeholder packages that collected thousands of downloads after a model repeatedly suggested their names. The risk grows with agentic coding, because an agent can install a dependency and run it without a human ever reading the name.
Package hallucination compared with related threats
Package hallucination is one source of a broader class of dependency confusion and impersonation attacks. What distinguishes it is that the misleading name originates in model output rather than human error or build configuration.
| Threat | Origin of the wrong name | Main defense |
|---|---|---|
| Slopsquatting | AI model invents the name | Verify every suggested dependency |
| Typosquatting | Developer mistypes a real name | Lockfiles and careful review |
| Dependency confusion | Build resolves a public name over a private one | Scoped namespaces and registry pinning |
| Account takeover | Real package receives a malicious release | Signed releases and two-factor publishing |
Mitigation for developers and coding agents
The core rule is that no AI-suggested dependency is trusted until verified. Effective controls combine developer habits, tooling and model-side measures, because each layer catches cases the others miss.
- Check the registry page for age, maintainers, download history and source repository before installing.
- Use lockfiles and hash pinning so builds install only reviewed versions.
- Route installs through an internal proxy or allowlist that blocks unknown packages.
- Restrict agent permissions so a coding agent cannot install new dependencies without approval.
- Lower sampling temperature and ground the model with retrieval from the actual registry index.
- Run software composition analysis to flag new, low-reputation or recently published packages.
Registries also respond by removing reported malicious packages and by watching for bursts of new names that match known hallucination patterns, but removal usually happens after the first installations.
Limits of current defenses
No model is free of package hallucination, and rates vary widely by language, task and prompt. Self-checking, in which the model is asked whether a package exists, helps but is not a reliable filter, because the same knowledge gap that produced the name also limits the check. Attackers can additionally give a malicious package a working, plausible implementation, so a successful install and passing tests prove nothing about safety. Treating dependency choice as a security decision rather than a convenience remains the most dependable safeguard.
In everyday language
Programmers often reuse ready-made code packages. AI assistants suggest which ones to use. Sometimes the assistant makes up a package name that sounds real but does not exist.
That alone would just cause an error. The danger comes from criminals. They watch which fake names AI tools invent and publish harmful packages under those names. It is like a stranger setting up a shop at an address that a confused map keeps giving out. Always check that a suggested package is real and trustworthy before installing it.
Seen in the real world
A data team at a mid-sized logistics company used a coding agent to build a script that parsed shipping manifests. The agent proposed installing a package whose name combined a well-known PDF library with the word “tables”. The name looked reasonable, and the agent was allowed to install packages automatically in the developer sandbox.
The company’s internal package proxy blocked the request because the package had been published only nine days earlier, had a single maintainer and no linked source repository. A security engineer inspected it and found an install script that collected environment variables, including cloud access keys, and sent them to a remote server.
The team reported the package to the registry, which removed it, and then changed the agent configuration so that every new dependency required human approval. No credentials were exposed, because the proxy stopped the install before any code ran.