Sub topic 9
Debugging with AI without handing over the thinking
This chapter assumes you can run a script and a simple test, and that you have used the attempt-first workflow from earlier chapters. Debugging is where handing over the thinking is most tempting. You paste an error, the AI returns a fix, and the bug disappears. You learn little, because finding the cause is the skill you were supposed to practice.
Keep the hypothesis loop yours
A bug is a gap between what you expected and what happened. Closing that gap takes four steps: reproduce it, form a hypothesis, test the hypothesis, then fix. The AI can help with each step, but you should do the hypothesis step before you ask for anything.
The AI enters only after a check fails to confirm your hypothesis. Its answer sends you back to write a new hypothesis. It does not replace that step.
A worked example
Suppose you wrote this median function, and the first call returns the wrong value.
def median(values):
mid = len(values) // 2
if len(values) % 2 == 1:
return values[mid]
return (values[mid - 1] + values[mid]) / 2
print(median([3, 1, 2])) # expected 2It prints 1. Before asking anyone, write down a hypothesis: "The function picks the middle position, but the list is not in order, so the middle item is not the median." That is a testable claim. Test it with a call that differs only in order.
print(median([1, 2, 3])) # prints 2
print(median([3, 1, 2])) # prints 1The sorted input works and the unsorted input fails, so the hypothesis holds. Now the fix is yours to write: sort a copy of the list first, with ordered = sorted(values), and index into ordered. Then add the failing case as a permanent test, so the bug cannot return unnoticed.
When you are stuck, ask for questions
If your hypotheses keep failing, use the AI as a tutor. Give it your evidence, not just the error, and ask it to hold back the answer.
My function median([3, 1, 2]) returns 1, but I expected 2.
My hypothesis was: <your hypothesis>.
I checked it by: <what you ran> and saw: <the output>.
Do not rewrite my code. Ask me one question that would help me
find where my reasoning is wrong. If I am still stuck after
that, give me a hint, not a fix.This prompt carries three things: your expectation, your hypothesis, and your evidence. Those make the AI's reply specific, and writing them forces you to think before you ask.
Common errors in AI-assisted debugging
- Pasting the error and accepting the patch. The code may pass, but you cannot say why it failed. Explain the cause in your own words before you move on.
- Fixing the symptom. An AI may add a special case that hides the failure, such as returning
2for that one input. Check that the fix addresses your confirmed cause. - Trusting the AI's diagnosis without a check. A diagnosis is a hypothesis too. Run a check on it, just as you would on your own.
- Skipping the regression test. A bug that has no test is likely to come back.
Reading the result
After the fix, compare what you learned with what you would have learned alone. If the AI supplied the cause, note it and try to find a similar bug on your own later. If you supplied the cause and the AI only confirmed it, that is the outcome to aim for.
Exercise
The call mean([]) crashes with ZeroDivisionError in your own function. An AI suggests wrapping the body in try/except and returning 0. What should you do before accepting it?
Show answer
Treat the suggestion as a hypothesis about the fix, not as the answer. First state the actual cause: the function divides by len(values), which is 0 for an empty list. Then decide what the correct behavior is. Returning 0 would hide a case with no valid mean, so raising ValueError with a clear message is usually better. Finally, write a test that calls mean([]) and checks for that behavior.