Lesson 13 of 15 · In Production
Prompt Templates
Turn a working prompt into a reusable template with placeholders: what to keep fixed, building templates safely with string.Template, and missing values.
- 6 min read
- Beginner
- Reviewed
- What a prompt template is and why reuse matters
- How to decide which parts are fixed and which are variables
- How to fill templates safely in Python, including missing values
- Good habits for keeping a library of templates
Write once, reuse many times
Once a prompt works well, you will want to use it again: for every product, every customer email, every weekly report. Rewriting it each time is slow and makes results inconsistent.
Fixed versus variable
Look at a prompt you have used a few times and ask what changed between uses.
| Fixed (part of the template) | Variable (placeholders) |
|---|---|
| The task and its wording | The product, document or email |
| Format and length rules | The audience, when it varies |
| Tone and style rules | Specific facts for this case |
| Examples of good output | The question being asked |
The fixed parts are where your testing effort goes. The variables should be as small and clear as possible.
Templates in Python
Python's string.Template uses $name placeholders. Its substitute method fills them in, and refuses to build a prompt if a value is missing.
from string import Template
template = Template(
"Write a product description of the $product for $audience.\n"
"Mention: $features.\n"
"Format: one headline, then 3 bullet points, under 50 words. Tone: $tone."
)
products = [
{"product": "steel water bottle", "audience": "busy parents",
"features": "plastic-free, leak-proof lid", "tone": "friendly"},
{"product": "standing desk", "audience": "people who work from home",
"features": "quiet motor, 3 height presets", "tone": "calm and practical"},
]
for values in products:
print(template.substitute(values))
print("---")
# A missing value is caught before anything is sent to a model
try:
template.substitute({"product": "desk lamp", "audience": "students"})
except KeyError as error:
print("Cannot build prompt, missing value:", error)The same tested instructions now produce consistent prompts for any product. And the missing features value is caught before a half-finished prompt reaches a model, which would otherwise have happily invented some features.
Why not f-strings or format?
Prompts often contain curly brackets of their own, for example a JSON example showing the output format. Python's format method treats every curly bracket as a placeholder, which breaks:
from string import Template
# Prompts often contain curly brackets, for example a JSON example
text = 'Reply with JSON like {"topic": "billing"}. Message: $message'
print(Template(text).substitute(message="I was charged twice"))
# str.format treats every curly bracket as a placeholder, so it breaks
try:
'Reply with JSON like {"topic": "billing"}. Message: {message}'.format(
message="I was charged twice")
except KeyError as error:
print("str.format failed, it looked for a value called", error)string.Template only looks for $, so JSON examples inside the prompt are left alone. (Many prompt tools use other placeholder styles, such as double curly brackets. The idea is the same.)
Treat filled-in values as data
Placeholders often hold text you don't control: a customer's email, a web page, a user's question. That text might contain instructions of its own. Two habits help:
- Wrap untrusted values in delimiters, as in the email tags above, and say in the fixed part that the tagged content is data to work on, not instructions.
- Check values before filling: not empty, not absurdly long, the right kind of value (a date where a date is expected).
The last lesson in this course covers this risk, called prompt injection, in more detail.
Common mistakes
Fill a reusable prompt template with this request's details.
from string import Template
template = Template("Summarise this $doc_type for $reader in $count bullet points.")
values = {"doc_type": "contract", "reader": "a new tenant", "count": 3}
# Fill in the template with the values and print the finished prompt
Your prompt includes a JSON example with curly brackets. Which way of filling in placeholders avoids clashing with them?
Summary
- A prompt template keeps tested instructions fixed and fills in placeholders for what changes.
- Decide what is fixed (task, rules, format, examples) and what is variable (inputs and case facts).
- In Python, string.Template fills
$nameplaceholders and refuses to run with missing values. - Wrap untrusted values in delimiters and check them before filling.
- Keep templates in one shared place, named and versioned.
Next: testing prompts properly and keeping track of versions.
Finished reading? Tick it off to track your progress.