What Is Package Hallucination? When AI Coding Tools Invent Libraries That Do Not Exist

What Is Package Hallucination? When AI Coding Tools Invent Libraries That Do Not Exist

📦 What Is Package Hallucination? When AI Coding Tools Invent Libraries That Do Not Exist

Package hallucination happens when an AI coding assistant suggests a library (a "package") with a name that sounds perfectly plausible but does not actually exist on a software registry like PyPI (Python) or npm (JavaScript). You copy the "pip install ..." line it gave you, run it, and get a "package not found" error, or worse: an attacker has already registered that exact fake name, and the package you just installed is malware.

🧠 A Quick Everyday Example

Imagine asking a stranger for directions in a new city. They answer with total confidence: "go straight through the intersection, the Hương Giang coffee shop is on your right, number 45 Lê Lợi street." It sounds specific and certain, exactly the way real directions sound. The problem is that shop never existed. The stranger was just stitching together details that "sound plausible" from a vague memory of how directions are usually phrased, not actually knowing this neighborhood.

AI coding tools do the same thing with library names. They do not open a browser and check PyPI or npm (registries with millions of packages, changing every day) before answering you. They simply "guess" the name most likely to fit the context, based on familiar naming patterns like adding "-utils" or "-toolkit" suffixes, or "fast-" or "py-" prefixes. It sounds completely real, but it can be pure invention.

⚙️ How It Actually Works

  1. AI writes code by predicting the next most likely token, not by checking a complete, always-current package index. When it needs to suggest a library for task X, it generates a name that "sounds like" a real one based on what it learned during training, without confirming the name is actually registered.
  2. A study presented at USENIX Security 2025 ("We Have a Package for You! A Comprehensive Analysis of Package Hallucinations by Code Generating LLMs") tested 16 popular LLMs and generated 576,000 Python and JavaScript code samples. The result: 19.7% of the suggested package names did not exist at all, totaling more than 205,000 unique fake package names.
  3. The rate varies sharply by model type: commercial models averaged around 5.2% hallucinated packages, while open-source models reached 21.7%, nearly four times higher. This is one more reason open-source and closed-source models differ not just in price but in how reliable they are for real coding work.
  4. The most striking finding: 43% of the hallucinated names repeated identically across all 10 reruns of the same prompt. AI does not invent randomly each time, it tends to "lock onto" the same handful of fake names whenever it hits a similar context. This repeatability is exactly what attackers exploit.
  5. Attackers run popular AI models themselves, record which fake package names come up most often, then register a real package (containing malware) under that exact name on PyPI or npm. When a victim copies the install command the AI suggested and runs it, they deliver the malware onto their own machine. Security researchers call this "slopsquatting," a play on "typosquatting," the older trick that relied on users mistyping a package name themselves.

🎯 What This Means for You

  • Always verify before installing. Before running pip install <name> or npm install <name> on an AI's suggestion, type the exact name into pypi.org or npmjs.com. A real package has its own page listing the author, version number, download count, and last update date; a nonexistent one will say "not found" immediately.
  • Turn on confirmation prompts when using a coding agent. If you use an AI coding tool with permission to run commands on its own, do not let it auto-install every package without asking, especially names you have never heard of. Working safely with Claude Code or similar tools means letting it pause and ask before installing any new library.
  • If you practice vibe coding (letting AI write most of the code), ask the AI directly: "are you sure this package exists on PyPI, give me the link to its official page." Forcing it to find and cite a real link usually exposes a fabricated name right away, since no matching page will exist.

⚠️ Common Misconceptions

  • "Only weak or old AI models have this problem." Not quite. A 2026 follow-up study tracking five newer frontier models (released between October 2025 and March 2026) found the hallucination rate had dropped sharply, narrowing to about 4.62% to 6.10% per model, but it still has not reached zero even in the strongest models.
  • "The fake names are random, so they are hard to exploit." Wrong. The same 2026 study found 127 package names that all five models hallucinated identically. These names are especially dangerous: an attacker only needs to register one of them to trap users across several different AI tools at once.
  • "I'll just try installing it and remove it later if something's wrong." Riskier than it sounds. Unlike a syntax error that shows up immediately on screen, a malicious package can run code (an install script) the moment you type the install command, before you even get to test whether it works. "Removing it later" can already be too late if the malware has stolen data or planted a backdoor.

❓ Frequently Asked Questions

Does package hallucination only affect professional developers, or beginners coding with AI too? Both, but beginners practicing vibe coding tend to be more exposed, since they are more likely to copy an AI's install command without double-checking it.

How can I tell if a package is real before installing it? Search for its exact name on pypi.org (Python) or npmjs.com (JavaScript). A real package always has its own page with an author, version number, download count, and update date; this is also a good general way to verify what an AI tells you, not just package names.

Have strong models like Claude or GPT solved this problem completely? Not yet. Newer models hallucinate less (the 2026 study found roughly 4.6% to 6.1% among top models), but no model has reached 0%, so you still need to verify manually.

How is "slopsquatting" different from "typosquatting"? Typosquatting exploits users typing a package name incorrectly themselves (for example typing "numpyy" instead of "numpy"). Slopsquatting exploits the AI suggesting the wrong name in the first place: the victim never mistypes anything, they just copy the AI's suggestion exactly as given.

Why does AI make things up like this in the first place? This is one form of AI hallucination: AI answers by predicting what is most likely to be correct based on the data it learned, not by looking up a source that is always accurate and updated in real time.

#AI #AICoding #VibeCoding #Security #PackageHallucination

More articles