Sub topic 5
Show the behavior with examples
This chapter assumes you can write and run small Python functions and tests, and that you have used a chat-based or editor-integrated AI assistant. You should already know how to state a goal and add constraints from the earlier chapters.
Why examples help
Prose is ambiguous. "Replace non-alphanumeric characters with a hyphen" leaves open what happens with leading spaces, repeated symbols, or characters like +. An input and output pair settles each of those questions. The assistant can compare its draft against your examples before it answers.
Examples also give you a way to check the result. If the examples in your prompt become tests, you can run them against the generated code instead of trusting it.
A prompt with and without examples
Here is the stronger prompt from the goals and constraints chapters. It names the language, signature, context, constraints, and output format.
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.It still leaves gaps. Does "C++ & Rust" become "c-rust" or "c-rust-"? Add examples to close them:
Examples of the expected behavior:
slugify("Hello, World!") -> "hello-world"
slugify(" Multiple spaces ") -> "multiple-spaces"
slugify("C++ & Rust") -> "c-rust"
slugify("Already-slugged") -> "already-slugged"Each example covers a different case:
| Example | Case it pins down |
|---|---|
| `"Hello, World!"` | Punctuation is removed and case is lowered |
| `" Multiple spaces "` | Runs of spaces collapse, with no edge hyphens |
| `"C++ & Rust"` | Mixed symbols become a single hyphen |
| `"Already-slugged"` | Existing hyphens are kept as they are |
A plausible response
An assistant given both parts might return this:
import re
def slugify(text: str) -> str:
text = text.strip().lower()
text = re.sub(r"[^a-z0-9]+", "-", text)
return text.strip("-")It uses only the standard library, lowercases first, collapses each run of other characters into one hyphen, and trims hyphens from both ends.
Turn the examples into a test
Do not stop at reading the code. The same pairs from the prompt can become a parametrized test.
import pytest
from slugify_module import slugify
@pytest.mark.parametrize(
"text, expected",
[
("Hello, World!", "hello-world"),
(" Multiple spaces ", "multiple-spaces"),
("C++ & Rust", "c-rust"),
("Already-slugged", "already-slugged"),
],
)
def test_slugify(text, expected):
assert slugify(text) == expectedSave the function as slugify_module.py, save the test beside it, and run pytest. All four cases should pass.
Choosing good examples
- Cover the edges, not only the happy path. Include empty-ish input, extra whitespace, and symbols. A single typical example teaches the assistant very little.
- Keep them consistent. If two examples contradict each other, the assistant will pick one or write code that special-cases both. Check each pair by hand first.
- Use enough, but not too many. Three to six well-chosen pairs usually work. A long list pushes the assistant toward memorizing cases rather than finding the rule.
- State the rule too. Examples plus the constraint sentence work better than either alone. The rule explains the pattern, and the examples catch misreadings of it.
Common errors
- Wrong examples. If your expected output is itself mistaken, the assistant may produce code that matches the mistake. Verify each pair yourself.
- Overfitting. The assistant may satisfy the listed cases with special-case branches instead of general logic. Test with at least one input that is not in your prompt, such as
"a---b". - Missing the failure behavior. If bad input should raise an error, give an example of that, or the assistant will invent behavior.
Exercise
A prompt for slugify includes four examples, and the generated code passes all four. You then call slugify("a---b") and get "a---b" back instead of "a-b". What does this tell you about the prompt, and what is one fix?
Show answer
The examples did not cover repeated hyphens, and the assistant's code treated existing hyphens as safe to keep rather than as part of a run to collapse. The listed examples were too narrow, so the code passed them without implementing the general rule. Add an example such as slugify("a---b") -> "a-b" to the prompt and to the tests, and make the constraint explicit that hyphens in the input also count as part of a run.