Sub topic 5
Write a debugging prompt that gets a useful answer
This chapter assumes you can run Python code and tests from a terminal and read a traceback. It also assumes you have already reproduced the failure and gathered the code and error output, as covered in earlier chapters. Here you combine those pieces into one prompt.
Why the prompt matters
An assistant can only reason about what you show it. A prompt like "my code crashes, fix it" forces the assistant to guess the language version, the input, and the goal. It will often answer with a plausible but wrong fix.
A good debugging prompt gives the assistant the same facts you would give a colleague at your desk. It also asks for reasoning before a fix, so you can check the diagnosis.
The failing example
Start with a small function that fails on empty input.
def average(values):
return sum(values) / len(values)
print(average([]))Running this file as stats.py produces the following traceback. Paste it unchanged, not retyped or summarized.
Traceback (most recent call last):
File "stats.py", line 5, in <module>
print(average([]))
File "stats.py", line 2, in average
return sum(values) / len(values)
ZeroDivisionError: division by zeroThe last line gives the error type. The frame above it gives the failing line inside average.
The four parts of a prompt
A useful debugging prompt has four parts.
| Part | What to include | Why it helps |
|---|---|---|
| Environment | Language version, how you run the code | Rules out version-specific causes |
| Expected behavior | What you wanted to happen | Defines what "fixed" means |
| Actual behavior | The real error and the real code | Gives evidence to reason from |
| The ask | A request for reasoning, then the smallest fix | Lets you check the diagnosis |
A prompt you can reuse
I am running Python 3.12 and calling average([]) from stats.py.
Expected: a clear error telling the caller the input is empty.
Actual: ZeroDivisionError: division by zero (traceback below).
<paste the function and the full traceback>
What is the most likely cause? Explain your reasoning from the traceback first, then propose the smallest change that fixes it.Replace the placeholder line with the function and the complete traceback. Before you send it, check that nothing in the paste contains secrets, as covered in the previous chapter.
What the answer should look like
For this example, a good response says the cause is len(values) being 0 when the list is empty. It then proposes a small change. The fix below rejects empty input with a clear error, which matches the expected behavior stated in the prompt.
def average(values):
if not values:
raise ValueError("average() requires at least one value")
return sum(values) / len(values)Notice how the "Expected" line shaped the answer. Without it, the assistant might have returned 0 for an empty list. That would silence the error but hide the problem, and it would not do what you wanted.
Common prompt mistakes
- Summarizing the error. Writing "it says something about division" removes the line number and error type the assistant needs.
- Pasting only the failing line. The assistant cannot see how the function is called or what it receives.
- Asking for a fix only. You get code without an explanation you can check.
- Bundling several problems. One prompt should describe one failure. Mixed problems produce mixed answers.
- Leaving out the goal. Without the expected behavior, the assistant picks its own definition of correct.
How the pieces fit
The next chapter covers the check in the decision node in more detail.
Exercise
A teammate sends this prompt: "My average function is broken, please fix it." Name three things missing from it, and rewrite the last sentence so the reply can be checked.
Show answer
Missing items: the language and version, the actual traceback and code, and the expected behavior (for example, a clear error on empty input). A better last sentence: "What is the most likely cause? Explain your reasoning from the traceback first, then propose the smallest change that fixes it."
Exercise
Why is it a problem if the assistant answers by making average([]) return 0, given the prompt's "Expected" line asked for a clear error?
Show answer
It removes the crash without meeting the stated goal. Callers would receive 0 as if it were a real average, which hides the bad input instead of reporting it.