Why does AI-generated code import packages that do not exist?
AI-generated code imports packages that do not exist because a language model predicts a plausible name instead of looking it up in a registry. A 2024 study of 16 models found hallucinated package rates of at least 5.2% for commercial models and 21.7% for open-source ones. Check every new dependency in the registry before you install it.
Cite
CodeLifter Forum, "Why does AI-generated code import packages that do not exist?", https://codelifter.com/forum/posts/why-does-ai-generated-code-import-packages-that-do-not-exist/, accessed 2026-11-12.- CodeLifter editorsStaffStaff, CodeLifter ·
What is a package hallucination?
A package hallucination is an import or install command that names a package which is not published in the registry the code expects, such as npm for JavaScript or PyPI for Python. The paper that measured it, We Have a Package for You! by Spracklen and colleagues (arXiv 2406.10279, revised 2 March 2025, listed to appear at the 2025 USENIX Security Symposium), generated 576,000 code samples in Python and JavaScript with 16 models and found 205,474 unique invented package names. The authors describe the problem as "a novel form of package confusion attack", which means an attack that relies on you installing the wrong package by name.
Why do models invent package names?
A model writes the most likely next text given your prompt and what it saw in training. Package names follow patterns (
flask-something,react-something, a verb plus "js"), so a name can look right without existing. The model has no registry lookup unless the tool you use gives it one, and even then it may not call it. The study calls these "fact-conflicting errors": the code is fluent, the fact behind it is wrong.The paper's abstract also says the authors tested mitigation strategies that "significantly reduce" hallucinated packages while keeping code quality. That is useful, but it does not reach zero, so the check stays on your side.
What is slopsquatting?
Slopsquatting is the attack that turns a hallucination into a compromise: someone registers a name that models tend to invent and waits for a developer to install it. Socket, a supply chain security vendor, wrote on 8 April 2025 that the term was coined by Seth Larson, the Python Software Foundation's Developer-in-Residence, and described it as "a twist on typosquatting: instead of relying on user mistakes, slopsquatting relies on AI mistakes." Typosquatting is registering a misspelling of a popular package name.
OWASP lists the same risk in LLM05:2025 Improper Output Handling, which warns that a model "may also hallucinate non-existent software packages, potentially leading developers to download malware-infected resources."
What do the bad names look like?
What you see What it may be How to tell Install fails with "not found" A plain hallucination, nobody has registered it yet The registry has no page for the name Install succeeds, package is days old, no repository link A squatted name, possibly registered to catch this mistake Check the first publish date and the repository.urlfieldName close to a well-known package A typo or a typosquat Compare letter by letter with the package the docs name Name that exists in a different language's registry A cross-ecosystem mix-up Confirm the registry matches your language How do you check a dependency before you install it?
Do this for every package an assistant adds, before
npm installorpip installruns. If an agent runs commands for you, turn on approval for install commands so you see the name first.- Read the name out of the code. List every new
import,requireor entry inpackage.jsonorrequirements.txtthat you did not add yourself. - Look it up in the registry. For npm, run
npm view <name>. The npm view documentation (CLI v11, read 16 October 2026) shows how to read single fields, for examplenpm view <name> repository.urlandnpm view <name> time. For Python, openhttps://pypi.org/project/<name>/. - Check that it is the package you think it is. Follow the repository link, check that the project's own README names the package the same way, and check the publish history. A package first published last week, with no repository and no readme, deserves a second look even if the install works.
- Prefer the library's own docs. If the official docs for the framework name a different package for the job, use that one and tell the assistant why.
- Pin what you approved. Commit
package-lock.json, and install in CI withnpm ci, which the npm ci documentation says "will never write topackage.jsonor any of the package-locks". In Python, pin with==and hashes, covered below. - Keep install scripts on a short leash. The same page documents the
ignore-scriptssetting: when true, npm does not run the scripts in packages'package.jsonfiles, so apreinstallorpostinstallscript in a package you have not checked does not run on your machine.
The lookup takes seconds. It is the cheapest check in the whole review routine for AI-generated code.
What can you put in the prompt or rules file?
Prompts lower the rate; they do not remove it. Lines that help:
- "Use only packages listed in package.json. If you need a new one, name it, link its registry page, and stop for approval."
- "Prefer the standard library and the framework's built-in features over new dependencies."
- "Do not run install commands."
Put them in
AGENTS.mdor your tool's rules file so every session reads them. The same rules file is where you keep the lines from the thread on keeping API keys out of a vibe-coded app, since a malicious package that does get installed will look for exactly those secrets.If the code came from somewhere other than an assistant, such as an old tutorial, the same lookup applies; see is it safe to paste a script from an old tutorial site?. CodeLifter's longer write-up on vibe coding security puts the package check alongside secret scanning and output handling.
Short version
- Models predict package names; they do not look them up. Spracklen et al. measured at least 5.2% (commercial) and 21.7% (open-source) hallucinated packages.
- Slopsquatting is registering those invented names so someone installs them.
- Look up every new dependency with
npm viewor on pypi.org before you install it. - Pin approved dependencies and install with
npm ciin CI. - Rules files help, but the registry check is the control.
- Read the name out of the code. List every new
Write something first.
Give people something to work with: at least 30 words on what happened and what you tried.
That is too long. Keep it under 6,000 characters.
Write the question as the title, 15 to 140 characters, no links.
Pick a category.
Add a name (2 to 40 characters, no links).
That email address does not look right.
That was quick. Read the thread, then try again.
The form expired. Reload the page and post again.
Something went wrong with the form. Reload and try again.
Please complete the check and post again.
Limit reached for now. Try again later.
This thread is closed to new replies.
This thread no longer accepts replies.
Something went wrong with the form. Reload and try again.
Post a reply or question first (name and email), then this browser can vote, edit and accept answers.
You cannot vote on your own post.
Only the author (within 30 days) or the forum team can do that.
That email already has an account. Use your sign-in link instead.
New accounts are paused for the moment. Try again later.
That was already posted.
2 answers
- CodeLifter editorsStaffStaff, CodeLifter ·
How do you check if an npm package is real before you install it? Start with
npm view <name>. If the registry has no such package, the command fails and you are done: the name was invented. If it returns data, read three fields.npm view <name> repository.urlshould point at a source repository whose README uses the same package name.npm view <name> timelists the publish date of every version, so a package created very recently, with one version, stands out.npm view <name> maintainersshows who can publish it. All three patterns are in the npm view documentation for CLI v11, read on 16 October 2026.Repeat the check even when the same assistant suggests the same name twice. Socket's summary of the Spracklen study reports that when 500 prompts that had triggered hallucinations were rerun ten times each, 43% of the invented names came back every time. Repetition makes a name feel familiar; it does not make it real, and it is exactly what makes a name worth registering for an attacker.
- CodeLifter editorsStaffStaff, CodeLifter ·
Does a lockfile protect you from slopsquatting? Partly. A lockfile records exactly which packages and versions you installed, so later installs cannot quietly pick something else. The npm ci documentation says the command needs an existing
package-lock.jsonornpm-shrinkwrap.json, exits with an error if the lock andpackage.jsondisagree, and never writes to either file. That stops drift in CI.It does not judge the name. If you approved a squatted package once, the lockfile pins it faithfully from then on. The same limit applies in Python: pip's hash-checking mode, turned on with
--require-hashes(pip 26.2.1 docs, read 16 October 2026), requires every requirement to be pinned with==and a local hash, which protects against "remote tampering". It confirms you got the file you pinned, not that you pinned the right project. So the order is: check the name in the registry first, then lock it.