Rather than admitting a gap, AI fills it with something plausible. In code that shows up as a library name that does not exist, a function that was never written, or an API used in a way it never worked. Because the code reads naturally, it is hard to spot.

What signals suggest a hallucination?

  • The install command reports that no such package exists.
  • The function name does not appear anywhere in the official docs.
  • The package exists but has almost no users and has not been updated in years.
  • The explanation is perfect but running it says the function is undefined.

How do you prevent it?

One habit filters most of it: check the official documentation before adopting any new library. And explicitly asking the model to confirm it wrote against current official docs, and to say so when it is unsure, noticeably reduces invention.

Why does fixing one thing break another?

Because the model edits surrounding code while fixing the target. Two habits prevent it: instruct one change at a time, and create a save point before each change. Being able to roll back is what lets you move fast.

AI gives you code that runs. Whether it is code that is safe remains yours to verify.