Topic 1 of 5 · 4 min read
Catching invented functions and parameters
Goal: Identify a call to a non-existent API in an AI-written diff and confirm it against the library's type definitions or docs.
A hallucinated API is a call that looks right, follows the library's naming style, and does not exist. Confirm every unfamiliar call against the library's own types or docs before you trust it.
Check yourself · 1 of 3
An AI-written diff calls db.users.findByEmail(). What is the most reliable first check?
Why it matters
A model predicts likely text. After seeing fetchAll, findMany and getById across many libraries, it will happily write client.findByEmail() for a library that has no such method. The name fits the pattern, so it reads as correct. Claude, ChatGPT, Gemini, Copilot and Cursor all do this. It happens most with small libraries, new releases, and options passed as objects, where a wrong key is silently ignored.
Some of these fail loudly: a TypeError on the first run. The dangerous ones fail quietly. An unknown option such as { retries: 3 } is dropped by a library that has no retry option, and the code looks resilient while retrying nothing.
How to check a call in under a minute
- List the new symbols. Scan the diff for every imported name, method, flag and config key you do not know by heart.
- Let the compiler help. In TypeScript, an invented method is a type error. In Python, run a type checker or open the function in your editor and jump to its definition. If the diff silences the check with
any,# type: ignoreor a cast, treat that as a warning sign. - Open the definition, not a search result. Go to the installed package:
node_modules/<pkg>/dist/*.d.ts, orhelp(obj)in Python. This is the version you actually run. - Read the docs for the pinned version. Search the docs page for the exact name.
- Run it. A short test that exercises the call, with an assertion on the effect and not only on "no error".
Before and after
// Task: "Retry the request up to 3 times"
const res = await ky.get(url, { retries: 3, backoff: "exponential" });
The shape is believable, but the real option in this library is retry, and backoff is not a key at all. A strict type for the options would reject both. The fixed version reads the type first:
const res = await ky.get(url, { retry: { limit: 3 } });
What goes wrong
- Trusting a clean compile. A cast to
anyor a loose options type hides the invented key. - Checking only the method name. The method exists but the argument order or a flag name is invented.
- Believing the agent's explanation. Asking the same model "does this exist?" often gets a confident yes. Check the source, not the model.
- Testing only that it does not crash. Ignored options pass a test that never checks the effect.
Examples
A retry option that retries nothing
A diff adds { maxRetries: 5 } to a database client. The client's options type has retries, and unknown keys are ignored at runtime. The new test only checks that the query returns rows, so it passes. Reading the .d.ts file takes 30 seconds and shows the key is wrong.
A flag from another tool
A shell script calls git log --since-commit=abc123. The flag does not exist in git, but it looks like --since. Running git log --help or the command itself exposes it at once.
Key points
- Models write names that fit a library's style, so a plausible name is not evidence that it exists.
- Ignored options are the quiet failure: the call succeeds and the feature does nothing.
- Check against the installed package's types or help output, not against the model's own explanation.
- A new
any, cast ortype: ignorenext to a library call is a reason to look closer. - A test should assert the effect of the option, not only that the call returns.
Common mistakes
- Reviewing only the logic and assuming every called method is real.
- Asking the same chatbot to confirm that its own API exists.
- Accepting a cast or
anyadded to make the call compile. - Checking docs for the latest version instead of the version in the lockfile.
Check yourself · 2 of 3
A call passes { retries: 3 } to a library whose option is named retry. Why is this hard to notice?
Check yourself · 3 of 3
Which line in a diff should make you look hardest for an invented API?
Practice items, not counted towards your mastery.
Start here
- We Have a Package for You! Package hallucinations by code-generating LLMs (opens in a new tab)PaperFree
The study that measured how often code models invent package names, across languages and models.
- Slopsquatting: how AI hallucinations are fueling a new class of supply chain attacks (Socket) (opens in a new tab)ArticleFree
Explains the attack in plain terms and what defenders can do about it.
- Concise Guide for Evaluating Open Source Software (OpenSSF) (opens in a new tab)ArticleFree
A short checklist for judging whether a package is maintained, trusted and the one you meant.
More for this topic
- package-lock.json (npm docs) (opens in a new tab)DocsFree
What the lockfile records, and why its diff is the place to see every new dependency.