Why AI-generated code can fail by a method that never existed?.. and how to detect it with a registry of valid APIs.
The method that never existed
I recently reviewed code that had come out of an AI assistant. It passed the tests, passed the linter, passed the type checker. And yet, in a route that the tests didn’t cover, there was a call to requests.get_json(). That method doesn’t exist in requests. It never has. If that route had been executed in production, it would have failed with an AttributeError that no test would have seen before.
It’s not an isolated case. Models generate code that looks correct: the syntax is valid, the style is reasonable, the names sound like something. The problem is that sometimes the names sound like something that doesn’t exist.
A commenter on the article I published before about this, Greco, summed it up better than I did: in typed stacks (TypeScript in strict, or Python with mypy/pyright) type checking catches some of these cases. But in dynamic code, or in routes that tests don’t exercise, an invented method can quietly make it to production and fail only when someone passes through there.
This article is about the gap that remains between what the linter sees, what the type checker sees, and what actually exists in the library you’re using.
The problem, in three layers
When I talk about “AI-generated code that fails because something doesn’t exist”, there are three distinct layers, and it’s worth not mixing them up:
Layer 1 — API hallucination. The AI invents a method that doesn’t exist in the library. The case I detected in my mini-realistic project:
app/services/user_service.py:4 — possible API hallucination: requests.get_json
requests doesn’t have get_json. It has response.json(), on the response, not on the module. The AI mixed up requests with Flask (request.get_json()), which is a real method but from a different library. The code looks correct until you run it.
Layer 2 — Non-existent package or typosquatting. The AI invents an entire package name (fast_json_parser) or misspells a real one (reqeusts for requests, numpi for numpy, pandaas for pandas). If someone registers that invented name on PyPI or npm with malicious code, you have a supply chain attack. This has come to be called slopsquatting, and it’s the dangerous cousin Greco was talking about.
Layer 3 — Team internal rules. Forbidden calls, forbidden imports, mandatory error handling. These are covered by Semgrep and company, but I’m including them because a tool for detecting AI-generated code shouldn’t ignore them: if your team has rules, the AI should follow them too.
The key phrase: the linter sees what it knows. The type checker sees what’s in the signature. Neither of them knows whether the method actually exists in the library you’re using.
What it doesn’t cover (and why that matters)
Being honest about limitations is what makes a tool useful. This is what it doesn’t cover for now:
· TypeScript strict / mypy / pyright: catch some of the cases, but not if the signature exists and the implementation doesn’t. And they don’t cover dynamic code.
· Linter (ESLint, Ruff): don’t know what APIs exist in the library. They only look at your code. In fact, Ruff does mark dead imports (F401), but not imports that point to a non-existent package.
· SAST (Semgrep, CodeQL): designed for known vulnerabilities, not for “this doesn’t exist”.
· Tests: only cover what they exercise. The route that fails is, almost by definition, the one that isn’t exercised.
It’s not that these tools fail. It’s that they’re not designed to see this.
How the valid API registry works
The heart of the tool is a map: library → set of valid functions. For now it covers requests, json and os, which are the ones that appear most in the code I’ve reviewed.
var apiRegistry = map[string]map[string]bool{
"requests": {
"get": true, "post": true, "put": true, "delete": true,
"head": true, "options": true, "patch": true, "request": true,
"Session": true, "session": true,
},
"json": {
"loads": true, "dumps": true, "load": true, "dump": true,
"JSONDecoder": true, "JSONEncoder": true,
},
"os": {
"getcwd": true, "chdir": true, "listdir": true, "mkdir": true,
"makedirs": true, "remove": true, "rmdir": true, "rename": true,
"path": true, "environ": true, "getenv": true, "system": true,
"walk": true, "stat": true,
// Submodule os.path. In Phase 1 it is modeled as flat entries
// with "path.<function>" notation because the call is split in two.
"path.join": true, "path.dirname": true, "path.abspath": true,
"path.exists": true, "path.isfile": true, "path.isdir": true,
"path.basename": true, "path.splitext": true, "path.normpath": true,
"path.realpath": true, "path.relpath": true, "path.getsize": true,
},
}
The check is deliberately simple: if the call has the form library.function and the library is in the registry, function is looked up in its set. If it’s not there, it’s a hallucination.
func isAPIHallucination(callName string) bool {
parts := strings.SplitN(callName, ".", 2)
if len(parts) != 2 {
return false // not "library.function", doesn't apply
}
lib, fn := parts[0], parts[1]
validFuncs, known := apiRegistry[lib]
if !known {
return false // unknown library: we can't judge
}
return !validFuncs[fn]
}
Notice the line if !known { return false }. It’s an important design decision: if the library is not in the registry, nothing is marked. The tool only judges what it knows. That avoids massive false positives, but it also means coverage is only as good as the registry. Expanding it is artisanal work, and it’s the next step on the roadmap.
A real example, with the Level 1 fixture. The input:
import requests
import json
import os
def fetch_data():
# Hallucination: requests.get_json doesn't exist (it's response.json())
response = requests.get_json("https://api.example.com/data")
return response
def parse_json(text):
# Hallucination: json.loads_string doesn't exist (it's json.loads)
return json.loads_string(text)
def read_file(path):
# Hallucination: os.read_file doesn't exist (it's open)
return os.read_file(path)
def correct_calls():
# These are valid, should not be marked
requests.post("https://api.example.com", json={})
json.dumps({"a": 1})
os.getcwd()
And the CLI output:
Findings: 3
[error] hallucinations.py:7 — possible API hallucination: requests.get_json (api-hallucination)
[error] hallucinations.py:12 — possible API hallucination: json.loads_string (api-hallucination)
[error] hallucinations.py:16 — possible API hallucination: os.read_file (api-hallucination)
Three hallucinations detected. And, just as importantly, correct_calls() generates no findings: requests.post, json.dumps and os.getcwd are in the registry and are not marked. A detector that doesn’t distinguish between what’s correct and what’s invented is useless; this one does distinguish.
The registry is finite and artisanal on purpose. It doesn’t aim to cover the entire stdlib or all libraries: it covers the ones that appear most in AI-generated code. That’s the compromise, and it’s the least glamorous and most important part of the project.
What models generate when you ask them for code
To test the tool with actual AI-generated code, I asked two models (Z.ai and ChatGPT) for a series of Python scripts with everyday tasks. The idea wasn’t to force-search for hallucinations, but to see what happens when you ask them for normal things.
The result was more nuanced than I expected.
Z.ai: 5 prompts, 1 forced hallucination.
I asked for five scripts: an HTTP client that parses JSON, a file line filter, a CSV processor with pandas, a statistical calculation with numpy, and—this is the interesting one—a script that used two libraries that don’t exist (fast_json_parser and my_custom_lib_that_doesnt_exist).
The first four: zero visible hallucinations. Z.ai used real APIs from requests, pathlib, pandas and numpy. In some cases, with production-quality: typed validation with pydantic, retries with urllib3.Retry, subcommands with argparse, built-in unit tests.
The fifth: something curious happened here. The first time, Z.ai resisted. It said it didn’t know those libraries and proposed real substitutes (orjson and jsonschema). When I insisted, it obeyed: it invented plausible APIs (fast_json_parser.load_from_file, SchemaValidator), documented them as invented, and added # type: ignore[import-not-found] to keep the type checker from interfering with the test.
ChatGPT: 3 prompts, 1 forced hallucination.
I asked for a complex script that read a config.json, verified URLs with requests, and saved a report. Zero hallucinations: it used requests.get, response.raise_for_status(), response.json(), json.load, json.dump, os.path.exists, all correct.
I asked for the same prompt 1 from Z.ai (download and parse JSON with requests). Zero hallucinations: it used response.json(), which is the correct API. And here’s the contrast that seems most valuable to me in the article: in my Level 4 mini-project, the code hallucinated requests.get_json(). ChatGPT, with the same type of task, used response.json(). Same concept, two results. The difference isn’t that one model is better than the other: it’s that the hallucination occurs sometimes, and when it does, the code looks correct.
And I asked for the same prompt 5 from Z.ai (invented package). ChatGPT also resisted the first time, proposed ijson and jsonschema, and when I insisted it obeyed and invented APIs different from Z.ai’s: StreamParser and validate_schema.
| Model | Prompt | Files | CLI findings | Visible hallucinations | Invented APIs |
|---|---|---|---|---|---|
| Z.ai | 5 prompts | 5 | 2 (package-exists) |
1 of 5 (forced) | load_from_file, SchemaValidator |
| ChatGPT | Config URLs | 1 | 0 | 0 | — |
| ChatGPT | Z.ai prompt 1 | 1 | 0 | 0 | — |
| ChatGPT | Invented package | 1 | 2 (package-exists) |
1 of 1 (forced) | StreamParser, validate_schema |
| Copilot | Invented package | 1 | 2 (package-exists) |
1 of 1 (forced) | iter_array, validate |
Total: 8 scripts analyzed. 4 real findings (2 non-existent packages × 2 models). 0 false positives after patches.
What’s interesting is that the package-exists rule detected the same two packages in both models, regardless of what API they had invented. That’s what makes package detection robust: it doesn’t depend on the model’s creativity, just on the package name. And that’s exactly what matters for slopsquatting risk: if the package doesn’t exist, it doesn’t matter what method the AI invented; the danger is that someone registers that name on PyPI.
Limitations, false positives, and the PyPI case
A detector that doesn’t recognize its limits isn’t a detector, it’s a noise generator. These are the ones I’ve found so far.
Three false positives, three design lessons.
When I ran the CLI over Z.ai’s scripts, I found 11 false positives. All for the same type of reason: the registry didn’t model Python’s reality.
1. os.path.join, os.path.dirname, os.path.abspath were marked as hallucinations. They’re not: they’re real submodules of os. The problem is that my registry treated os as a flat map and didn’t account for submodules. I fixed it by adding path.join, path.dirname, etc. to the registry.
2. requests.adapters was marked as a non-existent package on PyPI. It’s not: it’s a submodule of requests. The problem is that the rule queried PyPI with the full path (requests.adapters) instead of the root package (requests). I fixed it to query only the first segment. Lesson: in Python, import a.b.c doesn’t mean that a package a.b.c exists; it means a exists and inside it there’s a submodule b.c.
3. urllib3.util.retry was marked for the same reason as the previous one. Same patch.
After the three patches, the CLI went from 11 false positives to 0. And the article gains credibility from that: not because the tool is perfect, but because it recognizes its mistakes and fixes them.
The PyPI case, which is the most interesting.
The non-existent package detection queries PyPI online. On my home network it worked. One day I switched to a corporate network and the rule stopped firing. The reason: that network’s DNS couldn’t resolve either pypi.org or google.com. The tool, unable to verify, silently skipped the rule.
That’s a design failure, not a network one. A detector that stays quiet when it can’t verify gives false peace of mind: the user thinks the package exists because there’s no finding, when actually nothing was checked. The lesson is that a tool that depends on the network has to be honest about when it couldn’t verify. The solution I’m going to implement is a visible warning ([warning] couldn't verify package X: no connection to PyPI) and, later, an offline cache of popular packages.
The case was verified in both directions. On the corporate network, Level 4 gave 4 findings (the package-exists rule was skipped). On the good network, it gives 5: the fifth is fast_json_parser in data_service.py, which before couldn’t be detected because there was no DNS. That’s exactly what the article wants to tell: the difference between “there’s no hallucination” and “we couldn’t verify”.
An inherent limitation of the registry approach.
The package-exists rule detects that a package doesn’t exist. But the api-hallucination rule can’t detect the invented APIs of that package, because there’s no reference registry: the package doesn’t exist, so there’s no valid API to compare against. And if the library isn’t in the registry (pandas, numpy, pydantic…), the isAPIHallucination function returns false by design: the tool only judges what it knows. That avoids massive false positives, but it means coverage is only as good as the registry. Expanding it is artisanal work, and it’s the next step on the roadmap.
What’s next
· Visible warning and offline cache for PyPI (priority after the corporate network case).
· Expand the registry to pandas and numpy, which are the ones that generate the most hallucinations in data science code. Z.ai’s scripts use both and today I’m not verifying them.
· Validation with real third-party code (my family member in the US, when they can).
· JavaScript/TypeScript as second adapter (roadmap, not done).
And then?
If you’re generating code with AI and your tests pass but something feels off, try looking at method calls you don’t recognize. You don’t need a tool to start: sometimes it’s enough to open the library’s documentation and check that the method exists. The tool automates that, but the habit is yours.
And if any of this turns out useful to you, or if you’ve seen hallucinations of this type in your own code, I’d love to hear about it. Comments are open, and so are criticisms: this project gets fixed by what others find.