Sub topic 6
Ask for the output shape you need
This chapter assumes you can read Python and run pytest. You should also have used a chat or editor-based AI assistant before. By now you know how to state a goal, add context, set constraints, and give examples. This chapter covers the last part of a prompt: the form the answer should take.
Why output shape matters
An assistant picks a format when you don't. It may add an introduction, explain each line, or return a whole file when you wanted one function. That is fine in a chat. It causes trouble when you paste the answer into a file, apply it as a patch, or feed it to a script.
Asking for a shape has three benefits:
- You can use the answer without editing it first.
- A long answer is easier to review when it contains only what you need.
- A script can parse a predictable answer.
Common output formats
| You want | Ask for | Example wording |
|---|---|---|
| A function to paste in | Code only | "Return only the function, with no explanation." |
| A change to existing code | A unified diff | "Return only a unified diff against the file above." |
| A review | A list ordered by severity | "List problems as bullets, most serious first." |
| Data for a script | A fixed structure such as JSON | "Return only JSON with the keys `name` and `reason`." |
| Understanding | A short explanation | "Explain in under 100 words, then show the code." |
Name the format directly. "Keep it short" is vague. "Return only the function" can be checked.
A worked example
Take the slugify prompt from earlier chapters. Its last line sets the shape:
Write a Python 3.10 function slugify(text: str) -> str that converts an article title into a URL slug.
Context: it will be used in a small blog tool; titles are plain ASCII.
Constraints: use only the standard library; lowercase the result; replace each run of non-alphanumeric characters with a single hyphen; no leading or trailing hyphens.
Return only the function, with no explanation.A plausible response is exactly what you can paste into a module:
import re
def slugify(text: str) -> str:
text = text.strip().lower()
text = re.sub(r"[^a-z0-9]+", "-", text)
return text.strip("-")Without the last line, the same prompt could return three paragraphs, a usage demo, and a list of alternatives. You would then have to find the code in the middle.
Choosing the shape for the task
Tips for specifying a format
- State the format in its own line, usually last, so it is not buried.
- Say what to leave out as well as what to include. "No explanation" and "do not rewrite unrelated code" both help.
- For structured output, name the keys and give a tiny sample of the result.
- For diffs, say which file the paths refer to, and paste the current file so the diff matches it.
- If you need both code and notes, ask for them in a fixed order, such as "code block first, then at most three bullets of caveats."
Checking that the format held
Assistants sometimes ignore a format, especially in long conversations. If you parse the answer in a script, check it instead of trusting it:
import json
def parse_reply(reply: str) -> dict:
data = json.loads(reply)
missing = {"name", "reason"} - data.keys()
if missing:
raise ValueError(f"reply is missing keys: {sorted(missing)}")
return data
print(parse_reply('{"name": "slugify", "reason": "clear title"}'))If parsing fails, reply with the error text and restate the format. The next chapters cover this loop of refining a prompt from the response.
Exercise
You paste a 40-line Python file into a prompt and want the assistant to rename one variable throughout. Which output format is a good choice, and what two details would you add to the format line?
Show answer
Ask for a unified diff. Add that the diff is against the pasted file (so the paths and context lines match), and that it must change only the rename, with no edits to unrelated code. Asking for "only the diff, with no explanation" is also reasonable, since you will apply it as a patch.
Exercise
A script reads the assistant's reply with json.loads, but the call keeps failing because the reply starts with "Here is the JSON:". Rewrite the format line of the prompt to prevent this.
Show answer
For example: "Return only a JSON object with the keys name and reason. Do not include any text before or after it, and do not wrap it in a code fence." The line names the structure, the keys, and what to leave out.