Sub topic 3
Why Language Models Invent Package Names
This chapter assumes you have used pip or npm, know what a public registry such as PyPI or npm is, and have asked an AI assistant for code. It explains the cause of hallucinated package names. Later chapters cover how attackers exploit them.
What a hallucinated package name is
A hallucinated package name is a package that a model recommends but that does not exist on the registry. The model writes pip install fastjson-utils with the same confidence it uses for pip install requests. Nothing in the output tells you which one is real.
The cause is how a language model works. It does not look names up in a registry. It predicts the next piece of text (a token) from patterns it saw in training data. A package name is just text, so the model generates it the same way it generates any other text.
How the model generates a name
Package names follow patterns. They are often a library topic plus a common suffix: -utils, -client, -helper, -sdk. After seeing thousands of examples, the model learns which combinations look plausible.
When you ask for "a library to parse YAML with schema validation," the model builds a name that fits. It may combine a real library name with a familiar suffix. The result looks right, but no one may have published it.
Common causes
- No registry lookup. The model has no live view of PyPI or npm. It cannot check that a name exists.
- Pattern completion. Names that fit the naming style are likely outputs, whether or not they exist.
- Gaps in training data. For rare tasks, the model has few real examples to copy, so it fills in with a plausible guess.
- Stale knowledge. A package may have been renamed, removed, or deprecated after the training data was collected.
- Mixing ecosystems. A name that is real in npm may be suggested for pip, or the reverse.
- Prompt pressure. Asking for a package for a very specific task pushes the model to answer with one, even when none exists.
Hallucinations can repeat
A hallucinated name is not always random. The same prompt can produce the same invented name many times. Research on code models has found that a large share of hallucinated names recur across repeated runs of the same prompt. Different models also sometimes invent similar names.
This matters for security. A name that appears once is hard to target. A name that appears again and again is predictable, and an attacker can collect such names and register them. The next chapters build on this.
Seeing it yourself
You can test whether a name exists with a short script. This script asks PyPI for each name you give it. A 404 response means the name is not registered.
import sys
import urllib.error
import urllib.request
def exists_on_pypi(name):
url = f"https://pypi.org/pypi/{name}/json"
try:
with urllib.request.urlopen(url, timeout=10):
return True
except urllib.error.HTTPError as err:
if err.code == 404:
return False
raise
if __name__ == "__main__":
for pkg in sys.argv[1:]:
status = "exists" if exists_on_pypi(pkg) else "NOT FOUND"
print(f"{pkg}: {status}")Save it as check.py and run python check.py requests some-name-a-model-gave-you. A real package prints exists. A name that does not exist prints NOT FOUND.
An important limit: "exists" does not mean "safe." If an attacker already registered a hallucinated name, this script reports it as existing. Chapter on verification covers how to judge trust.
Common mistakes
- Trusting a confident tone. The model sounds equally sure about real and invented names.
- Assuming a familiar-looking name is real. Plausibility is exactly what the model optimizes for.
- Copying the install line without reading it. A one-line
pip installin a chat answer is easy to run on autopilot. - Assuming a model with web access cannot hallucinate. Search tools reduce the problem but do not remove it.
Exercise
A model suggests pip install yaml-schema-helper. You have never heard of it. Give two reasons the model might have produced a name that does not exist, and say what the first check should be before you install anything.
Show answer
Likely reasons: the model combines a topic (yaml, schema) with a common suffix (helper) because that pattern is plausible, and it has no live registry lookup to confirm the name exists. It may also have little training data for this exact task. Before installing, check that the name exists on the registry, for example by requesting its PyPI JSON page, and treat a 404 as a sign the name was hallucinated. If it exists, continue to check its age, release history, and source repository, because existence alone does not show it is safe.