Sub topic 6
Where AI answers go wrong
This chapter assumes you can write functions, conditionals and loops in Python, run a script and a simple test in a terminal, and have used a chat-based AI assistant. It covers the main ways AI answers fail, so you know what to look for before you trust the code.
Why fluent answers can still be wrong
An AI assistant predicts text that looks like a good answer. It does not run your code or check facts unless you give it a tool that does. The result is an answer that reads confidently whether it is right or wrong. Confident tone tells you nothing about correctness.
Common failure modes
| Failure | What it looks like | How to catch it |
|---|---|---|
| Invented names | Calls a function or library option that does not exist | Look it up in the official documentation |
| Missed edge cases | Works for normal input, fails on empty or unusual input | Write tests for the edges |
| Wrong problem | Solves a nearby problem, not the one you described | Re-read the task and compare |
| Outdated advice | Uses deprecated features or old library versions | Check the version you have installed |
| Plausible but false explanation | The code is fine, the reasoning given for it is not | Explain it back in your own words |
| Hidden assumptions | Assumes sorted input, positive numbers or a specific format | Ask what the code assumes |
A worked example: a bug that looks fine
Suppose you ask an AI for a function that returns the median of a list. It might give you this:
def median(values):
ordered = sorted(values)
mid = len(ordered) // 2
return ordered[mid]It looks reasonable and passes a quick check with an odd-length list.
print(median([3, 1, 2]))This prints 2, which is correct. Now try an even-length list.
def median(values):
ordered = sorted(values)
mid = len(ordered) // 2
return ordered[mid]
print(median([4, 1, 3, 2]))This prints 3, but the median is 2.5. The function ignores the rule that an even-length list needs the average of the two middle values. An empty list fails differently: ordered[0] raises an IndexError with a message that says nothing about the real problem.
A corrected version handles both cases and reports empty input clearly:
def median(values):
if not values:
raise ValueError("median() requires at least one value")
ordered = sorted(values)
mid = len(ordered) // 2
if len(ordered) % 2 == 1:
return ordered[mid]
return (ordered[mid - 1] + ordered[mid]) / 2
assert median([3, 1, 2]) == 2
assert median([4, 1, 3, 2]) == 2.5
assert median([5]) == 5
try:
median([])
except ValueError:
pass
else:
raise AssertionError("median([]) should raise ValueError")The first version was not obviously broken. Only inputs you chose deliberately exposed the problem.
Habits that reduce the risk
- Treat every answer as a draft written by someone you have not met.
- Check unfamiliar function names and options against official documentation.
- Write tests before you run the generated code, including empty, single-item and unusual inputs.
- Ask the assistant, "What does this code assume about its input?" and compare the reply with what you know.
- Say which language and library versions you use when you ask.
Failures that affect learning
Wrong code is easy to notice once a test fails. A wrong explanation is harder, because you absorb it as knowledge. If you later rely on a false rule about how a language works, you will write bugs that seem to have no cause. For this reason, check explanations against documentation when you will build on them.
Exercise
An AI gives you a function that removes duplicates from a list with list(set(items)). Name two ways this could be wrong for your task, and say how you would check each one.
Show answer
First, a set does not guarantee the original order, so if order matters the output may be rearranged. Check by running it on [3, 1, 3, 2] and comparing the order of the result with the input. Second, items must be hashable, so a list containing lists raises a TypeError. Check by passing [[1], [1]]. Also confirm the behavior for an empty list, which should return [].