Sub topic 8
Putting It Together in One Request
This chapter assumes you can already share a whole file, a selection, or an error with an assistant, as covered in the earlier chapters. It also assumes you can write a one-sentence task statement. Here you combine those pieces into a single request that the assistant can act on without guessing.
Why combine the pieces
Each kind of context answers a different question for the assistant.
- The file shows the surrounding code, its names, and its style.
- The selection shows where the problem is.
- The error shows what went wrong when the code ran.
- The task statement says what you want done.
Any one of these alone leaves gaps. A file with no task invites a generic review. An error with no file forces the assistant to guess at code it cannot see. A task with no error may describe a symptom the assistant cannot reproduce.
When you send all four in one request, the assistant can connect them. The error names a line, the selection marks the same line, the file explains what that line depends on, and the task says which change you want.
The four parts of a combined request
| Part | Question it answers | Typical size |
|---|---|---|
| File | What code surrounds the problem? | Attached, or named by path |
| Selection | Which lines matter most? | A few lines, with line numbers |
| Error | What happened when the code ran? | The full message and traceback |
| Task | What should change, and what must stay? | Two to four sentences |
These parts do not carry equal weight in every request. A syntax typo needs a small selection and a short error. A design question may need a long file and no error at all. Combining context means choosing which parts are needed and giving each the amount of detail it deserves, not filling every slot.
A worked example
Start with the small file used throughout this tutorial. It is saved as stats.py.
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 zeroThe traceback gives the call chain. The call on line 4 passes an empty list. The failure happens on line 2, in the average function. The exception type, ZeroDivisionError, names the cause: len(values) is zero.
Now build the request from the four parts.
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 these features:
- The first line says which file is attached and which lines are selected. The assistant does not have to search for the function.
- The second line gives the exception type and the input that triggers it. It summarizes the error without hiding it. If your tool lets you attach the full traceback, attach it as well. The summary tells the assistant what to look at, and the traceback supplies the evidence.
- The third line starts with a verb, states the desired behavior, and sets two limits.
A good answer to this request is a short change to the function.
def average(values):
if not values:
return None
return sum(values) / len(values)
print(average([]))
print(average([2, 4, 6]))Running this prints None and then 4.0. Both lines match the task: the empty list no longer raises, and normal input still works.
Ordering and labeling
Assistants read the whole request, so the order is not a strict rule. A consistent order still helps you and anyone who reads your history later. A reliable pattern is:
- Where the problem is: the file and the selection.
- What went wrong: the error.
- What you want: the task and its limits.
Short labels such as Context:, Error:, and Task: keep the parts separate. This matters most when the pasted error is long. If the traceback runs straight into your instructions, the assistant may treat part of your task as part of the error output, or the other way round.
For pasted material, mark the start and end clearly. In a chat box, put code and traceback in fenced code blocks, and keep your own sentences outside them. If your tool supports attachments or file references, use those for whole files. A reference is cleaner than pasting hundreds of lines into the message.
Deciding how much of each part to include
Use the problem to decide the mix. The table shows common situations.
| Situation | File | Selection | Error | Task focus |
|---|---|---|---|---|
| A crash in one function | Attach if it uses helpers | The failing function | Full traceback | Fix, with limits |
| Unclear behavior, no crash | Attach | The lines that behave oddly | None; describe the expected and actual output | Explain or fix |
| A new feature in existing code | Attach | The place where it belongs | None | Add, in the file's style |
| A failing test | The code under test | The test and the function it calls | Test output | Fix the code, not the test |
When there is no error, describe the gap yourself. Write the input you gave, the output you got, and the output you expected. These three facts do the job an error message does.
What to cut
A combined request is easy to overfill. Before you send it, remove each item that does not help the assistant with this task:
- Other functions in the file that never touch the selection.
- Repeated lines of the traceback from the same loop or the same library frames.
- Earlier failed attempts you have already abandoned.
- Log output from before the failure started.
Keep the lines that name your own files and line numbers. In a long traceback, those frames are usually the most useful, and the final exception line is always essential. Trimming is about removing noise, not shortening the evidence. If you are unsure about a frame, keep it.
Writing the limits into the task
The task statement does the most to prevent unwanted changes. With a file attached, an assistant may see other things it could improve. It will act on them unless you say not to. Three kinds of limits are useful.
- Scope limits name what must not change: "Keep the function signature and do not change other code."
- Behavior limits define the new behavior exactly: "An empty list returns
None", not "handle empty lists better". - Style limits point to conventions. If your project has standing instructions, such as the short conventions file below, you can supply them once instead of repeating them in each request.
# Project conventions
- Language: Python 3
- Tests: pytest, files named test_*.py
- Keep functions small and add type hints to new codeNotice how these interact. The conventions say to add type hints to new code, while the request above says to keep the signature. These do not conflict, because type hints on an existing signature would change it. Where limits could collide, say which one wins: "Do not add type hints to average; it is existing code."
Common errors
- Sending the error without the code that caused it. The assistant can explain what
ZeroDivisionErrormeans, but it cannot say which input caused it or where to fix it. - Attaching the whole file and no selection. The assistant has to decide what matters. If the file has several similar functions, it may edit the wrong one.
- Pasting a screenshot description instead of text. Retyped or paraphrased errors lose the exact wording. Copy the text.
- Vague tasks such as "fix this". The assistant may fix the symptom you did not mean, or rewrite the function. Say what correct behavior looks like.
- Contradictory context. If you attach a file, then edit it, then paste an error from the old version, the error's line numbers will not match. Re-run the code and copy a fresh error before sending.
- Leaving secrets in pasted output. Tracebacks and logs can include paths, tokens, and connection strings. Check them before sending, as covered in the chapter on sensitive data.
Checking the answer against the request
A combined request gives you a checklist for the reply. Compare the answer with each part you sent:
- Does the change touch the selection and nothing outside the limits you set?
- Does the failing input from the error now behave as the task describes?
- Does a normal input still work?
- Does the new code follow the conventions you supplied?
Run the code to confirm. For average, run the two print lines shown earlier. If a check fails, name the failed check in your next message. For example, write "The reply changed the function signature; revert that and keep only the empty-list check." This refines the context instead of restarting the request, which the final chapter covers in more detail.
How the pieces fit together
The diagram shows how the four parts feed one request and where the check happens.
Exercises
Exercise
A traceback ends with KeyError: 'email' at line 14 of users.py, inside load_user. You attach users.py, select load_user, and paste the traceback. Write the task line for the request, including one limit, so the assistant fixes the cause without rewriting the module.
Show answer
A good task line is: "Change load_user so a record without an 'email' key gets the value None instead of raising KeyError. Keep the function signature and do not change other functions." It starts with a verb, defines the exact new behavior, and sets a scope limit. A vague line such as "fix this error" would not meet those requirements.
Exercise
You attach report.py and paste a traceback, but the traceback names line 40 of report.py and your selection covers lines 5-12. What is wrong with the request, and what do you do before sending it?
Show answer
The selection and the error point at different places, so the assistant has two conflicting leads. Either the selection is wrong, or the traceback is from an older version of the file. Re-run the code to get a fresh traceback, then select the lines that the new traceback names, and send them together with a task that says what the code should do.
Exercise
A function returns 0 for an input where you expected 12, and it raises no error. Which of the four parts do you replace, and with what?
Show answer
Replace the error with a description of the gap: the input you gave, the output you got (0), and the output you expected (12). Keep the file, the selection of the lines that compute the result, and a task such as "Explain why this returns 0 and change it to return 12 for this input, without changing the signature."