Sub topic 5
Write a debugging prompt that gets a useful answer
This chapter assumes you can read a Python traceback, run a script and pytest from a terminal, and use git. It also assumes you have already gathered your context and removed secrets, as covered in the earlier chapters. Here you will turn that material into a single prompt that produces a diagnosis you can check.
Why the prompt matters
An assistant only sees what you give it. It cannot run your machine, read your mind, or know which Python version you use. When a prompt says "my code is broken, fix it", the assistant has to guess the environment, the expected behavior, and the failing line. Guesses lead to generic advice, invented causes, and fixes that do not match your situation.
A good debugging prompt removes the guesses. It contains five parts:
- The environment: language version, framework, and how you run the code.
- The expected behavior: what should happen.
- The actual behavior: what happens instead, with the exact error text.
- The evidence: the real code and the full traceback, pasted unchanged.
- The request: what you want back, and in what order.
A failing example
The running example is 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 output.
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 zeroYou should read this yourself first. The last line gives the error type, ZeroDivisionError. The frame just above it gives the failing line, line 2 inside average. The frame above that shows the call that triggered it. Because you understand this, you can later tell whether the assistant's answer agrees with the evidence.
Weak prompts and why they fail
Compare three versions of the same request.
| Prompt | What is missing | Likely result |
|---|---|---|
| "My average function crashes. Fix it." | Code, error, expected behavior | Generic advice or a rewritten function you did not ask for |
| "I get ZeroDivisionError in Python." | Code, the call, the expected result | A list of common causes of dividing by zero |
| Error text only, no code | The source and the input | A guess about code the assistant has not seen |
Each version forces the assistant to fill gaps. The result may sound confident and still be wrong for your code.
A prompt that works
Here is a prompt built from the five parts.
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 real function and the full traceback before you send it. Do not leave the angle-bracket text in.
Each part does a job:
- Environment. "Python 3.12" rules out advice for other versions. Version matters because library behavior and error messages change between releases.
- Expected versus actual. The expected line tells the assistant what "fixed" means. Without it, the assistant might make
average([])return0, which silences the error but may not be what you want. - Real code and full traceback. Paste them unchanged. A paraphrase such as "it divides by zero somewhere" removes the line numbers and frame names that point to the cause.
- Reasoning first. Asking for the explanation before the fix makes the assistant commit to a cause you can check against the traceback. If the reasoning is wrong, you see it before you read any code.
- Smallest change. This limits the scope. It discourages rewrites, new dependencies, and edits to unrelated code.
Anatomy of a good request
State the environment
Include only what could change the answer. Useful items are the language and version, the framework and version, the operating system, and the command you ran. For a failing test, include the exact pytest command. For a web app, include the framework version and whether the error occurs in development or production. A full dependency dump is rarely needed. A few relevant lines are enough.
Separate expected from actual
Write both, even when they seem obvious. Expected behavior is the specification the assistant will fix toward. If you leave it out, the assistant picks one. In the example, three reasonable fixes exist: raise an error, return 0, or return None. Only one matches your intent, and the prompt should say which.
Paste evidence, not summaries
Include the function that fails, the code that calls it, and the complete traceback. If the code is long, trim it to the failing function and anything it directly uses, as you learned when reducing the failure to a minimal example. Do not edit the error text. Do not cut the traceback to its last line, because the earlier frames show how execution reached the failure.
Ask for the output you can check
Tell the assistant how to answer. These requests work well:
- "Explain your reasoning from the traceback first."
- "Give the smallest change that fixes it."
- "Say what you are unsure about."
- "List what you would check if this fix does not work."
The last two give you a way to detect a weak diagnosis. An answer that lists no uncertainty about a problem with little context deserves extra suspicion.
Keep the prompt safe
Before sending, check what you pasted. Remove API keys, passwords, tokens, connection strings, and personal data. Replace them with obvious placeholders such as <API_KEY> and keep the structure of the line intact, so the assistant still sees where the value is used. A traceback can contain file paths with usernames and values printed in error messages, so read it before you paste it.
What the answer should look like
A useful answer to the prompt above has a recognizable shape. It quotes the failing line, return sum(values) / len(values). It states that values is empty, so len(values) is 0. It then proposes a small change. For this example, the fix rejects empty input with an explicit error.
def average(values):
if not values:
raise ValueError("average() requires at least one value")
return sum(values) / len(values)This matches the expected behavior you stated: a clear error telling the caller the input is empty. The next chapter covers how to check that kind of answer against the evidence. For now, notice that you could only judge the answer because your prompt defined what correct meant.
Common prompt mistakes
- Pasting only the last line of the traceback. The assistant loses the call path. Paste the whole trace.
- Describing the code instead of showing it. Descriptions leave out the detail that contains the bug.
- Asking several things at once. "Fix this, refactor it, and add logging" mixes the diagnosis with unrelated changes. Ask for the diagnosis and one fix.
- Pasting the whole repository. Too much code buries the relevant frames. Include the failing function and its direct dependencies.
- Omitting what you already tried. If you tested a theory and ruled it out, say so. Otherwise the assistant may suggest it first.
- Using the prompt as a command to apply blindly. The output is a hypothesis. You still verify it with a test.
The prompt in the workflow
The prompt sits in the middle of the debugging loop. You read the error and reproduce the failure first, because those steps supply the traceback and the minimal example. You gather context and clean it, then write the prompt. The reply goes to evaluation, where you compare it with the evidence, and only then do you apply a change.
Reusable template
Keep a template so you do not skip a part when you are frustrated.
Environment: <language and version, framework, command I ran>
Expected: <what should happen>
Actual: <what happens, including the exact error>
Code:
<the failing function and its caller>
Output:
<the full traceback or test failure>
Already tried: <what I checked or changed, and the result>
Question: What is the most likely cause? Explain your reasoning from the output first, say what you are unsure about, then propose the smallest change that fixes it.Edge cases
- No traceback. Some failures produce wrong output without an error. In that case, give the input, the actual output, and the expected output, and say plainly that nothing crashed.
- Intermittent failures. State how often it fails and what varies between runs, such as input, timing, or data order. Without this, the assistant will assume a deterministic bug.
- Long tracebacks. Keep the first and last frames in full, and keep every frame from your own code. You may shorten repeated library frames, but mark the cut with a line like
... (frames omitted) ...so the assistant knows. - Failures that depend on data. Replace real data with a small invented sample that still triggers the failure. This helps both the diagnosis and privacy.
- Follow-up prompts. When the first answer fails, send the new output, not a message saying "still broken". The next chapters on iteration cover this.
Exercise
A teammate sends this prompt: "My average function throws an error on empty lists, please fix it and also clean up the code." List three problems with the prompt and rewrite it.
Show answer
Problems: (1) it gives no code and no traceback, so the assistant cannot see the failing line; (2) it states no environment such as the Python version, and no expected behavior, so the assistant must guess whether to raise an error or return a value; (3) it asks for two things at once, a fix and a cleanup, which mixes the diagnosis with unrelated changes. Rewrite: "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. [function and full traceback pasted here] What is the most likely cause? Explain your reasoning from the traceback first, then propose the smallest change that fixes it."
Exercise
Why does the template ask the assistant to explain its reasoning before proposing the fix, and what should you do if the reasoning does not match the traceback?
Show answer
Reasoning first makes the assistant commit to a cause that you can check against the evidence, such as the failing line and the error type, before you see any code. If the reasoning does not match the traceback, do not apply the fix. Reply with the specific mismatch, for example the line number it ignored, and ask for a corrected diagnosis, or reduce the example further and try again.