Sub topic 8
Putting it together in one request
This chapter assumes you can already share a file, a selection, and an error on their own. Here you combine them into one request that the assistant can act on without guessing.
Why one request needs a shape
Each kind of context answers a different question for the assistant.
| Piece | Question it answers |
|---|---|
| File | Where does this code live, and what surrounds it? |
| Selection | Which part should I look at or change? |
| Error | What went wrong, and where? |
| Task | What should I do about it? |
If one piece is missing, the assistant fills the gap with a guess. A request with only an error may get a general explanation. A request with only a task may get a change in the wrong place.
A worked example
Take the small file used throughout this tutorial.
def average(values):
return sum(values) / len(values)
print(average([]))Running it produces this traceback.
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 zeroHere is a request that uses all four pieces.
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 request has three parts you can reuse:
- A context line that names the file and the selection.
- An error line that gives the exception and the call that triggers it.
- A task line that states the change and its limits.
The file is attached in full, so the assistant sees the print call on line 4. The selection points to lines 1-2, where the change belongs. The error line gives the cause in one sentence. If you pasted the full traceback instead, the line numbers would show the same call chain.
Order and labeling
Put the pieces in a steady order: context, error, task. Label each one. The labels let the assistant tell your own words apart from pasted output.
flowchart TDThe next diagram shows how the pieces feed into the request.
Deciding what to include
Not every request needs all four pieces. Use the smallest set that removes guesswork.
- Include the full file when the fix depends on code outside the selection, such as a caller or an import.
- Include the selection when the change is local. It tells the assistant where to focus even if the whole file is attached.
- Include the error when something fails. Keep the exception type and the failing lines. Trim repeated frames that add nothing.
- Always include the task. Context without a task invites a summary you did not ask for.
Common mistakes
- Pasting everything. A whole log with one relevant line buries the signal. Cut it to the failing part.
- Mismatched context. The error comes from one version of the file and the attachment is another. The assistant then reasons about code that no longer exists. Re-run and re-copy before sending.
- Vague selection. "The function above" can point to several places. Give the name and line numbers.
- Several tasks at once. A request that asks for a fix, a refactor, and new tests makes each result harder to check. Send them as separate requests.
- No success condition. Say what the result should do. Here,
average([])returnsNone.
Checking the result
After the answer arrives, compare it with your task line. For this example, you would confirm that:
- the signature is still
average(values), - an empty list returns
None, - nothing outside the function changed.
Then run the file again. If the answer misses, the next chapters cover refining the context step by step.
Exercise
You get an AttributeError in a function in report.py. List the four pieces of context you would put in one request, and give one constraint for the task line.
Show answer
The four pieces are the file (report.py, attached), the selection (the failing function, by name and line numbers), the error (the AttributeError message and the traceback lines that show the failing call), and the task (for example, "Fix the AttributeError when the input is missing a field"). One constraint could be "Keep the function signature and do not change other functions."
Exercise
A request attaches stats.py but the error was copied before you edited the file. What can go wrong, and what should you do?
Show answer
The assistant may reason about code that no longer matches the error, so its answer can target a problem that is already gone or miss the current one. Re-run the code, copy the fresh error, and send it with the current file.