Sub topic 3
Gather the context the assistant needs
This chapter assumes you can run Python code and tests from a terminal, use git for basic tasks, and have chatted with an AI assistant. You should also have read the error yourself and reproduced the failure, as covered in the previous chapters.
An assistant cannot see your machine. It only knows what you put in the prompt. When it guesses at missing details, it often guesses wrong, and you end up fixing a problem you do not have. Good context removes those guesses.
What the assistant needs
Collect four things before you write the prompt.
- The code that fails. Include the function that raised the error and the code that calls it. Do not include the whole project.
- The full error output. Copy the traceback unchanged. The last line gives the error type, and the frames above it show where the failure happened.
- The environment. Give the language version, the operating system, and the versions of any relevant libraries.
- Expected and actual behavior. Say what you wanted to happen and what happened instead.
The table below shows what goes wrong when each item is missing.
| Missing item | Typical result |
|---|---|
| Code | The assistant invents a function that matches the error message. |
| Full traceback | The assistant fixes a different line from the one that failed. |
| Environment | The assistant suggests an API that your version does not have. |
| Expected behavior | The assistant "fixes" the error but returns the wrong result. |
A worked example
Here is a small function that fails on an empty list.
def average(values):
return sum(values) / len(values)
print(average([]))Running it produces this traceback.
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 zeroPaste the traceback exactly as it appears. Do not retype it or summarize it as "a division error". Line numbers, file names, and the exact message are the evidence the assistant reasons from.
Now gather the rest. For this case:
- Code: the
averagefunction and the callaverage([]). - Environment: Python 3.12, file
stats.py. - Expected: a clear error telling the caller the input is empty.
- Actual:
ZeroDivisionError: division by zero.
Choose how much code to include
Too little code leaves gaps. Too much buries the relevant part and may include things you should not share. Use this order:
- Start with the function named in the innermost traceback frame.
- Add the code that calls it, if the bad input comes from there.
- Add definitions of any custom types or helpers that the failing line uses.
- Stop. Add more only if the assistant asks for it.
If the failure only appears in a large program, go back to the previous chapter and shrink it to a minimal example first. A short reproduction is the best context you can give.
Collect version details quickly
Do not guess versions from memory. Ask the tools.
python --version
pip list --format=freeze | grep -i pytest
git status --shortThe git status output also shows which files you have changed. That helps the assistant tell the difference between code that used to work and code you edited a minute ago. If the bug appeared after a recent change, include the output of git diff for that change.
The shape of the process
Common mistakes
- Paraphrasing the error. "It crashes when the list is empty" drops the file, line, and error type.
- Pasting only the last line.
ZeroDivisionError: division by zerodoes not say where it happened. - Omitting what you already tried. Without this, the assistant often suggests the same fix you rejected.
- Mixing several failures. Give one failure per prompt, with its own traceback.
Exercise
A teammate asks an assistant, "My script crashes with a KeyError, how do I fix it?" and pastes only the line KeyError: 'user_id'. Name three pieces of context missing from this request that would help the assistant give a useful answer.
Show answer
Any three of: the full traceback (which shows the file and line where the lookup failed), the code around that line including the dictionary being read, the input data or a minimal example that triggers the error, the language and library versions, and the expected behavior (for example, whether the key should always exist or may be absent).