Sub topic 7
Spot hallucinated APIs and fixes that hide the bug
This chapter assumes you can read a Python traceback, run pytest, and use git. It covers two failure modes of AI assistants: invented APIs and fixes that hide a bug instead of solving it. Both produce confident answers that look plausible, so you need to know how to catch them.
Hallucinated APIs
An assistant predicts likely text. It does not look up your installed libraries. A hallucinated API is a function, method, argument, or module that sounds right but does not exist, or exists only in a different version.
Suppose you ask how to compute an average safely, and the assistant suggests this:
import statistics
def average(values):
return statistics.safe_mean(values, default=0)Running it fails at once:
AttributeError: module 'statistics' has no attribute 'safe_mean'Hallucinations are not always this loud. Sometimes the name exists but the argument does not, or the function behaves differently in your version. Check three things:
- Does the name exist? Run
python -c "import statistics; print(dir(statistics))"orhelp(statistics.mean). - Does it exist in your version? Check the official documentation for the version you run.
- Does the package exist? Be careful with install commands for packages you do not recognize. A made-up name can match a malicious package on the index.
Asking the assistant "are you sure?" is weak evidence. Ask for a link to the documentation, then open it yourself.
Fixes that hide the bug
A second failure is a change that silences the symptom and leaves the cause. Go back to the failing function from the earlier chapters. The error was ZeroDivisionError when the list is empty. These "fixes" all stop the crash:
def average(values):
try:
return sum(values) / len(values)
except Exception:
return 0def average(values):
return sum(values) / (len(values) or 1)Both now return 0 for an empty list. That is a wrong answer presented as a right one. A caller computing a class average would report 0 for a class with no scores, and nothing would warn them. The expected behavior we stated was a clear error for empty input, so neither change meets it.
The version that matches the expected behavior is:
def average(values):
if not values:
raise ValueError("average() requires at least one value")
return sum(values) / len(values)Warning signs
| Pattern in the proposed fix | Why it is suspect |
|---|---|
| `except Exception: pass` or a broad `except` that returns a default | Hides every error, including unrelated ones |
| Deleting or loosening an assertion or test | Makes the check pass without fixing the code |
| Special-casing the failing input | Fixes one example, not the cause |
| Adding `sleep()` or retries | Often masks a race condition or a bad assumption |
| Suppressing a warning or type error | Removes the signal, not the problem |
| A new API or flag you have never seen | Possible hallucination |
A check you can run on any fix
Before applying a suggestion, ask these questions in order:
- Does every function, argument, and import in it exist in my version?
- Does it explain why the error happened, using evidence from my traceback?
- Does it change behavior for inputs other than the failing one, and is that intended?
- Does it remove information, such as an error, a log line, or a test?
If an answer is unclear, ask a follow-up that targets it, for example: "Which line of the traceback shows that this is the cause? Show the documentation page for safe_mean."
Exercise
An assistant fixes a KeyError in your code by wrapping the dictionary lookup in try: ... except KeyError: pass. The crash stops. Give two reasons to be suspicious, and say what you would ask next.
Show answer
The fix swallows the error, so the missing key is no longer reported, and the code that needed the value may now continue with wrong or missing data. It also does not explain why the key is missing. Ask the assistant why the key is absent in this case, using the traceback and the data, then decide whether the right fix is to supply the key, use a documented default with dict.get, or raise a clearer error.