Sub topic 2
Write a debugging prompt that gets a useful answer
Assume you can run Python code and tests, read a traceback, and use basic version control. An AI assistant can reason about a failure, but only from the evidence you provide.
Include the evidence first
A useful debugging prompt states:
- The language, version, framework, and relevant dependencies.
- The expected behavior.
- The actual behavior.
- The smallest complete code example that still fails.
- The full error message and traceback.
- The command that reproduces the failure.
For example:
def average(values):
return sum(values) / len(values)
print(average([]))ZeroDivisionError: division by zeroThen ask for reasoning before a fix:
I am using Python 3.12. I expect empty input to produce a clear input error.
This code raises ZeroDivisionError instead.
What line fails? What does the traceback prove?
Identify the most likely cause, then suggest the smallest fix.Do not paste a large project when a small example shows the problem. Remove passwords, access tokens, customer data, private URLs, and proprietary code before sharing anything.
Check the diagnosis against the evidence
Treat the assistant's explanation as a hypothesis. Check that it names the failing line, matches the error type, and explains why the input produces that error. A diagnosis that discusses an exception absent from the traceback needs more evidence.
Ask follow-up questions when needed:
- Which statement proves that conclusion?
- What alternative cause could produce the same symptom?
- What observation would distinguish the alternatives?
Spot unsafe or invented fixes
An assistant may invent an API, use the wrong library version, or recommend catching every exception. Verify unfamiliar function names against the official documentation and your installed version.
Be cautious when a fix hides the failure:
- Returning
0for an empty average changes the meaning of the result. - Catching
Exceptioncan conceal unrelated defects. - Removing a failing assertion does not fix the code it protects.
Apply one change at a time
Make the smallest change that addresses the demonstrated cause:
def average(values):
if not values:
raise ValueError("average() requires at least one value")
return sum(values) / len(values)Run the original reproduction and the relevant tests. Commit or save the change before trying another modification. This keeps cause and effect clear and makes reverting easy.
Exercise
What should you ask an assistant to do before proposing a fix?
Show answer
Ask it to identify the failing line and explain the diagnosis from the provided traceback and code.