Sub topic 6
Sharing Errors and Stack Traces
This chapter assumes you can run a Python file from a terminal and read basic output. It also assumes you have already shared a file or a selection with an assistant. Here you will learn what to copy from an error, what to leave out, and how to pair it with the code that produced it.
Why the error matters
An assistant cannot see your terminal unless you paste the output or the tool captures it for you. A sentence like "it crashes" forces the assistant to guess. A pasted error removes the guesswork. It names the exception type, the line that failed, and the path the program took to get there.
Consider the small file used in this tutorial:
def average(values):
return sum(values) / len(values)
print(average([]))Running it produces this output:
Traceback (most recent call last):
File "stats.py", line 4, in <module>
print(average([]))
File "stats.py", line 2, in average
return sum(values) / len(values)
ZeroDivisionError: division by zeroReading a stack trace
A stack trace (also called a traceback) lists the function calls that were active when the error happened. In Python, the oldest call comes first and the failing line comes last. The final line gives the exception type and message.
| Part | Example | What it tells the assistant |
|---|---|---|
| Exception type | `ZeroDivisionError` | The category of failure |
| Message | `division by zero` | The specific condition |
| Failing frame | `line 2, in average` | Where the error was raised |
| Call chain | `line 4, in <module>` | How the program reached that line |
Other languages order frames differently. Java prints the exception first and the newest frame at the top. Keep the trace in the order your tool printed it, so the assistant reads it as you saw it.
What to include
Share the whole traceback, not only the last line. The last line says what failed, and the frames above it say where and why. Add these details when they apply:
- The command you ran, such as
python stats.py. - The input that triggered the error, here an empty list.
- The language and tool versions, if the error might depend on them.
- What you expected to happen instead.
The last point is easy to forget. An error alone does not say what the correct behavior should be. For average([]), the assistant could raise a clearer exception, return 0, or return None. Your expectation decides which one is right.
What to leave out
Long output buries the useful part. Trim these before pasting:
- Repeated frames from recursion. Keep the first few and the last few, and mark the cut with a line such as
... 996 more frames .... - Thousands of lines of unrelated log output before the failure.
- Frames deep inside third-party libraries, unless the error is in the library. Keep the frame where your code calls into it.
Do not edit the message, the exception type, or the line numbers. Changing them gives the assistant a different problem from the one you have.
Pair the error with the code
A trace points at lines, but the assistant needs to see them. Attach the file or the selection that contains the failing frame. Line numbers in the trace refer to the file as it was when you ran it. If you have edited the file since, the numbers no longer match, so rerun the code first.
A request that uses the error
This request names the file, the selection, and the error, then states the task and its limits:
Context: stats.py (attached), selection: the average function (lines 1-2).
Error: ZeroDivisionError: division by zero when average([]) is called.
Task: Change average so an empty list returns None instead of raising. Keep the function signature and do not change other code.The error tells the assistant what is wrong. The task tells it what to do about it. Without the task line, the assistant might add a try/except around the call site, which fixes the symptom in the wrong place.
Common mistakes
- Pasting a screenshot of the terminal when text is available. Text is exact and searchable, and it is easier for the assistant to read.
- Sending only the exception line, which drops the call chain.
- Pasting an old error after changing the code, so the trace describes a different program.
- Asking "fix this" with no statement of the expected behavior.
Exercise
A teammate pastes only ZeroDivisionError: division by zero and asks the assistant to fix their script. Name two pieces of context that are missing, and say why each helps.
Show answer
The full traceback is missing. Its frames show which line failed and how the program reached it. The expected behavior is also missing, such as returning None for an empty list. Without it, the assistant has to guess which fix you want. The input that triggered the error, the attached file, or the command you ran are also valid answers.
Exercise
Your traceback contains the line File "/home/maria/projects/acme/stats.py", line 2 and a URL with ?token=abc123. What should you do before pasting it?
Show answer
Replace the private parts with placeholders. For example, shorten the path to stats.py and replace the token with <TOKEN>. Keep the line numbers, the exception type, and the message unchanged.