Why AI Coding Tools Invent Python Packages That Don't Exist
AI assistants regularly import Python packages that were never real. Here's why package hallucination happens — and why it isn't going away.
Ask an AI coding tool for a quick script and there's a real chance it hands you an import for a package that doesn't exist. This is package hallucination, and understanding why it happens tells you how to defend against it.
Models predict plausible text, not true facts
A language model generates the next most likely token, not the next true one. A package name like json_fast_parser looks exactly like a real Python package — lowercase, underscores, a sensible purpose — so the model emits it with full confidence. It has no built-in connection to PyPI to check whether it's real.
Why the same fakes keep appearing
Because models lean on common naming patterns, they tend to invent the same plausible names over and over. That repeatability is what makes hallucinations exploitable — see slopsquatting.
Common triggers
- Niche tasks with no obvious well-known library — the model fills the gap with an invented one.
- Combining real concepts into a fake name (
async_redis_cachewhen no such package exists). - Outdated or renamed packages the model half-remembers.
This won't disappear
Better models hallucinate less, but the deeper levels of the problem — fake function names inside real packages, wrong function arguments — will keep mattering for years. Treating AI output as a draft to verify, not gospel, is the durable habit.
Catch it automatically
```bash
mirago check your_file.py
```
mirago parses your code with Python's ast module, checks every import against PyPI, and flags the ones that don't exist — before you waste time (or get burned) installing them. Start with the installation guide.