Why templates?#
Once you've written a prompt that works well, you'll want to use it again - for another student, product, customer or topic. Retyping it each time causes mistakes and inconsistent results. A prompt template is the proven prompt with placeholders for the parts that change.
Explain {topic} to {level} students using {num_examples} real-life
Indian examples. Keep it under {words} words. End with one practice question.
Prompt template = एक अच्छा, जाँचा-परखा prompt जिसमें बदलने वाली जगहों पर खाली खाने (placeholders) होते हैं - जैसे {topic}, {level}।
यह एक छपे हुए form जैसा है: form एक ही रहता है, बस हर बार नाम-पता अलग भरा जाता है। इससे हर बार prompt की quality एक जैसी रहती है, गलतियाँ कम होती हैं, और Python से आप एक साथ सैकड़ों prompts बना सकते हैं (जैसे हर student के लिए अलग message)।
Templates with f-strings and .format()#
str.format() is ideal for templates stored as text (in files or a database) because the placeholders stay as {name} until you fill them.
Validating inputs#
A missing value causes a KeyError; an empty value creates a broken prompt. Check before sending:
Bulk generation from data#
Generate one prompt per row of a spreadsheet - personalised messages, product descriptions, report comments:
In the next lesson you'll send each of these prompts to an AI API automatically and save the replies to a file.
Storing templates#
Keep templates outside your code so non-programmers can edit them:
{
"progress_comment": {
"version": 3,
"description": "Teacher comment for report cards",
"template": "Write a short, encouraging progress comment ... {name} ... {score}",
"temperature": 0.5
},
"fee_reminder": {
"version": 1,
"description": "Polite fee reminder SMS",
"template": "Write a {tone} reminder to {name} ..."
}
}
Template best practices#
- Start from a tested prompt - only templatise prompts that already work well.
- Name placeholders clearly:
{student_name}, not{x}. - Validate all inputs; strip extra spaces.
- Version important templates and note what changed.
- Escape literal braces - if your template needs real
{ }(e.g. JSON examples), write{{ }}. - Keep a test set of inputs to re-check templates after edits.
T = 'Return JSON like {{"name": "...", "score": 0}} for student {student}.'
print(T.format(student="Asha"))
# Return JSON like {"name": "...", "score": 0} for student Asha.
Summary#
- Templates = proven prompts with
{placeholders}, filled withstr.format(). - Validate inputs: missing and empty values break prompts.
- Generate prompts in bulk from CSV/Excel data.
- Store templates in JSON with versions; use
{{ }}for literal braces.