Sub topic 8
Iterate: refine the prompt from the response
This chapter assumes you can write a basic prompt with a goal, context, constraints, and examples. It also assumes you can run Python tests with pytest. Here you learn to treat the first response as feedback on your prompt, and to use it to improve the next prompt.
Why the first response is a draft
An assistant answers from the text you gave it. When the answer is wrong or off target, the cause is usually something missing or ambiguous in the prompt. Each response shows you what the assistant assumed. Those assumptions point to the gaps in your instructions.
Iterating means reading the response, naming the gap, and changing the prompt to close it. This is faster than rewriting from scratch, and it gives you a prompt you can reuse.
Diagnose before you revise
Match the symptom to the likely missing piece.
| Symptom in the response | Likely gap in the prompt | Fix |
|---|---|---|
| Uses a library you did not want | Missing constraint | State allowed libraries |
| Wrong behavior on edge cases | Missing examples | Add input and output pairs |
| Code does not run on your version | Missing context | Give language and version |
| Long explanation around the code | Missing output format | Say "return only the function" |
| Changes code you did not mention | Scope not set | Say what not to change |
Change one thing at a time when you can. If you change four things and the result improves, you will not know which change helped.
A worked example
Start with a short prompt:
Write a function to clean up titles.The assistant might return a function that capitalizes each word and removes punctuation. That is a reasonable reading of "clean up", but it is not what you wanted. The gap is the goal. You need a specific, testable one.
Second attempt:
Write a Python 3.10 function slugify(text: str) -> str that converts an article title into a URL slug.
Use only the standard library. Return only the function.Suppose the response is this:
def slugify(text: str) -> str:
return text.lower().replace(" ", "-")It runs, but slugify("Hello, World!") returns "hello,-world!". Punctuation survives, and repeated spaces produce repeated hyphens. The prompt never said what a valid slug contains. Add the rule and examples:
Write a Python 3.10 function slugify(text: str) -> str that converts an article title into a URL slug.
Constraints: standard library only; lowercase; replace each run of non-alphanumeric characters with a single hyphen; no leading or trailing hyphens.
Examples:
slugify("Hello, World!") -> "hello-world"
slugify(" Multiple spaces ") -> "multiple-spaces"
Return only the function.This version gives the assistant something it can satisfy, and something you can test.
Give feedback that points at the problem
You can also reply to a response instead of rewriting the whole prompt. Good feedback is specific and tied to evidence.
- Weak: "This is wrong, try again."
- Stronger: "
slugify("Hello, World!")returns"hello,-world!". Expected"hello-world". Replace each run of non-alphanumeric characters with one hyphen."
Paste the failing input, the actual output, the expected output, and any error message. The assistant can then fix the real defect instead of guessing.
Know when to start over
Long conversations accumulate old instructions and abandoned attempts. If the assistant keeps repeating a mistake you already corrected, or its answers drift from your constraints, start a new conversation. Paste in your latest, improved prompt, not the whole history. This is one reason to keep each revision of the prompt in a file.
Keep a record of what changed
Save each prompt version with a one-line note about what you changed and why. When a later revision breaks something, you can compare versions and find the cause. The final version of a prompt that works becomes the starting point for a reusable template.
Exercise
A prompt asks for a function that parses dates. The assistant returns code that uses a third-party library, and it fails on the input "2024-02-30". Name the two gaps in the prompt and say how you would revise it.
Show answer
The first gap is a missing constraint: the prompt did not restrict libraries, so add "standard library only". The second gap is missing behavior for invalid input: add an example such as parse_date("2024-02-30") -> None or state that it should raise ValueError. Then change those two things, rerun the code, and compare the result to the examples.
Exercise
You get a wrong answer. Why should your follow-up include the failing input, the actual output, and the expected output, instead of just "this is wrong"?
Show answer
Those three items show the assistant exactly where its result differs from your goal. Without them it has to guess what is wrong and may change correct code or fix the wrong thing.