You’re coding along, the AI suggests a library that sounds perfect — react-super-forms, say — with a clean API and great docs-style examples. You npm install it. It doesn’t exist. You’ve just met the most annoying AI failure mode in programming. Why does AI sometimes invent fake libraries and APIs? The answer reveals something fundamental about how these models work.
Why does AI sometimes invent fake libraries and APIs? Here’s Why
Language models don’t “know” libraries the way you know your friends. They predict what text plausibly comes next. When you ask for “a Python library for X,” the model generates the most plausible-sounding continuation — which is often a real library, but sometimes a perfectly-formed fiction. It has the shape of knowledge without the substance. The examples look right because the model has seen thousands of real READMEs and can imitate the genre flawlessly.
When hallucinations spike
- Niche or brand-new domains. Ask about a library released last month and the model fills the gap with invention. Its training data has a cutoff; reality doesn’t.
- Overly specific requests. “A Rust crate that does exactly X with Y feature” — the more specific, the more likely it fabricates to satisfy you.
- Low-confidence hedging removed. Models are trained to be helpful, and “helpful” often wins over “honest” — it’ll give you a confident fake rather than admit ignorance.
- Code translation. Converting between languages? The model invents target-language equivalents that sound right. This is why converting code between languages needs verification of every dependency.
How to Catch Fake Libraries Before They Bite
- Check the package registry. npm, PyPI, crates.io — 10 seconds, definitive answer. Make this muscle memory.
- Ask the AI to cite. “Give me the GitHub URL and latest version.” Fakes often come with plausible-but-dead links.
- Prefer boring and popular. Ask “what’s the most widely-used library for X” rather than “what’s the best” — popularity correlates with existence.
- Pin versions. Real library, wrong version, invented API methods — the subtler variant. Check the actual docs for the version you’re using.
This is the same verification muscle behind stopping hallucinated functions and the trust tiers in production trust for AI code.
Why This Problem Is Slowly Getting Better (And Why It Won’t Vanish)
There are real reasons for optimism. Retrieval-augmented generation grounds models in actual documentation instead of memory — when the model searches the real docs before answering, fabrication drops dramatically. Tool-using models can check a package registry mid-conversation rather than guessing. Better training with more emphasis on calibration (“I don’t know” as an acceptable answer) is an active research priority at every major lab.
But the problem won’t fully vanish, for a structural reason: these models are prediction engines, and prediction will always occasionally produce confident fiction. The long-term fix isn’t a model that never hallucinates — it’s workflows that assume hallucination: automatic registry checks in your editor, CI steps that verify dependencies exist, package managers that warn on typosquatting-adjacent names.
So the skill isn’t going away either. “Verify existence” will remain part of the developer’s job description for years. The good news: it’s a cheap skill. Ten seconds at the registry, a habit of demanding links, a preference for the boring and popular. The developers who get burned are the ones who treat AI output as verified truth. Everyone else just adds one step to the workflow and moves on.
Teaching Your Team to Spot Fakes
If you lead developers, make hallucination-awareness part of onboarding — it’s cheaper than learning it from a production incident. The 10-minute version: show a real example of a hallucinated library (there are plenty), demonstrate the registry check, and establish the team norm that unverified AI-suggested dependencies never reach a PR. Put it in the contributing guide next to the linting rules.
Add mechanical guardrails too: CI that fails on unknown dependencies, lockfiles reviewed like code, and a short list of pre-approved libraries for common needs (so developers reach for the known-good instead of the AI-suggested). The goal isn’t to scare people off AI tools — it’s to make verification automatic. Teams that do this get all the speed benefits with none of the phantom-dependency incidents. It’s the same principle as seatbelts: not because you plan to crash, but because the cost of the precaution is tiny and the cost of the accident isn’t.
Your next step: make the registry check muscle memory starting today. Every AI-suggested library gets 10 seconds on npm/PyPI/crates.io before it enters your project. It’s the cheapest insurance in software development.
The Takeaway
AI invents libraries because it predicts plausible text, not verified facts — and it’s worst in new, niche, or highly specific domains. The fix is boring and effective: check the registry, demand links, prefer the popular. Ten seconds of verification beats an hour of debugging a phantom dependency.
Verify at the source: npm, PyPI, crates.io — bookmark all three.
1 Comment