Blog / Build with AI /

AI hallucinations in code: spotting invented functions

You paste an AI answer into your project, run it, and get an error: wp_get_all_recent_posts is not a function. You search for it. Nothing. Not in the docs, not in forums, not anywhere. The function was never real.

This is a hallucination, and in code it has a nasty flavour. The invented name looks completely plausible. It follows the naming pattern of real functions in that library, it’s used with confidence, and the surrounding explanation reads perfectly well.

If you build with AI, you’ll meet this sooner or later. Better to know the signs.

Why it happens

A language model doesn’t look things up. It predicts what text is likely to come next, based on patterns learned from huge amounts of writing. When it has seen a lot of code using a function, it reproduces it well. When it hasn’t, it fills the gap with something that fits the pattern.

WordPress has get_posts, wp_get_recent_posts and get_pages. So wp_get_all_recent_posts feels right. The model isn’t lying; it’s completing a pattern. It also can’t tell you when it’s guessing, unless you ask.

It gets worse in particular situations:

  • Less common libraries with less written about them.
  • Newer versions than the model’s training data covers.
  • Niche settings and options, such as a config key for some plugin.
  • Mixing two things, like using a method from one library on an object from another.
  • Long conversations, where earlier context drifts.

The kinds you’ll see

Invented functions or methods. The classic. A name that doesn’t exist at all.

Real function, wrong arguments. The function exists, but the model gives it parameters in the wrong order, or options it doesn’t take.

Real function, wrong library. A method that exists in one framework, used in another.

Old or removed features. Something that worked a few versions ago and has since been deprecated or deleted.

Invented packages. A suggestion to install a library that doesn’t exist, or exists with a different purpose. This one is more than annoying. Installing a package based on a guess is a security risk, as it can lead to installing something you didn’t intend.

Invented settings. A plausible-looking option in a config file or admin screen that isn’t there.

How to spot them

You don’t need to know the language to run these checks.

  1. Read the error message first. Messages such as “undefined function”, “is not a function”, “no attribute named”, “module not found” or “unknown option” are the signature. Take them seriously.
  2. Search the exact name in the official documentation, not just the web. For WordPress that’s the developer reference; for JavaScript, MDN; for Python, the library’s own docs. If the official docs don’t mention it, it’s probably not real.
  3. Check the spelling and capitalisation. Real functions can differ from the invented one by one word.
  4. Look at the package page. If the AI tells you to install something, look it up on the official registry. Check the publisher, the age, and what it says it does.
  5. Be suspicious of anything too convenient. If one magic function does exactly your complicated task in one line, check it’s real.
  6. Run it small. A hallucinated function fails loudly on the first run. That’s actually the good case. The dangerous ones don’t crash.

The quiet kind

The loud failures get fixed. The ones that worry me are the ones that don’t crash.

A function that exists but behaves differently from how the AI described it. A setting accepted without error but ignored. A formula that returns a number, just not the one you wanted. These produce plausible output and wrong results, which is worse than an error because you might ship them.

The defence is the same one I’ve been banging on about in this series: test with inputs where you already know the right answer. If you ask for a function that totals a column, run it on a column whose total you can add up in your head.

How to get the AI to help rather than hide it

Some prompts that reduce the problem:

“Use only functions that are part of core WordPress. If you aren’t certain a function exists, say so instead of guessing.”

“List every function, method and package you used in this code, with the official documentation page for each.”

Be careful with the second one: models can invent documentation links too. Treat links as leads and open them. A broken link is itself a warning.

“I got this error: [paste the exact message]. That function may not exist. Check whether it’s a real function, and if it isn’t, give me an alternative that definitely is.”

Notice that you’re feeding it the real error. The AI is good at working from concrete evidence and bad at working from vibes.

Also useful: tell it the version. “WordPress 6.3, PHP 8.1” or “Chrome extension Manifest V3”. That narrows the guessing.

Reduce the risk before you start

  • Use mainstream tools. Common languages and libraries are covered better than obscure ones.
  • Paste in the documentation. If you’re using something unusual, give the AI the relevant docs page in your prompt and ask it to stick to that.
  • Keep requests small. One function at a time gives you less to verify.
  • Start fresh when it drifts. In a long, tangled chat, begin a new one with a clear summary and the current code.
  • Learn the names of the core things. The more you know of the real vocabulary, the faster a fake stands out. That’s one of the quiet benefits of building with AI instead of being handed finished tools.

If you run a site

For WordPress, this is a particular risk in functions.php and custom plugins. A call to a function that doesn’t exist causes a fatal error, which means a white screen. That’s why testing on a copy matters, as I explained in copying code from AI safely. Use a snippets plugin that can catch fatal errors, or keep FTP access handy.

Key takeaways

  • Hallucinated code is plausible, not random. It follows real naming patterns.
  • Search every unfamiliar function in the official documentation.
  • Be wary of invented packages; verify before installing.
  • Silent wrong behaviour is more dangerous than loud errors, so test with known answers.
  • Tell the AI the version, and ask it to say when it isn’t sure.

What’s the strangest thing an AI has told you existed? Next time, check before you believe it.