Python, from Zero, for AILesson 81 of 89

Module 9 · Building the thing: a small AI program, end to end

Prompt templates: building a prompt from parts without letting the parts rewrite it

In this lesson

  • 8 min read
  • 8 reading sections
  • Knowledge check
  • Lesson 2 of 10 in this chapter

This chapter’s goalAssemble a complete tool from the course’s parts (a chat loop with bounded history, injection-resistant prompt templates, a sqlite response cache, a document chunker, a local embedding search over your own notes, a free local model as the fallback provider) expose it through a Gradio interface and a FastAPI endpoint, run it on a schedule without double-processing, and gate every change behind a GitHub Actions job that runs the tests.

This chapter’s media

The film explains chapter ideas. The audiobook narrates the complete teaching text.

FormatEnglishहिंदी
Chapter filmWatch1:39Not available
Full audiobookNot availableNot available

A little at a time

Explore an idea, try it, then check what stays.

Step 1 of 10Free · no account needed
Lesson steps

A prompt is a string

Everything you send to a model is text you assembled. The way you assemble it decides whether the prompt is readable, testable, and resistant to what the user typed. Three assembly methods, in order of how well they age.

f-strings at the call site

Python
reply = provider.complete(
    [
        {
            "role": "user",
            "content": (
                f"You are a support agent. "
                f"Summarise this ticket in one "
                f"line:\n"
                f"{ticket}"
            ),
        }
    ]
)

Fine for a script. It stops being fine the day there are four such calls with slightly different wording, and a change to the instruction has to be found in four places. It also mixes the prompt (which a non-programmer may need to edit) into code.

Your place and first choices stay on this browser. Classroom credit is recorded separately.

Stuck on something?Ask Divya, your AI tutor

How to cite: Addaly (2026) ‘Prompt templates: building a prompt from parts without letting the parts rewrite it’, in Python, from Zero, for AI. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/python-for-ai/prompt-templates-safely (Accessed: 21 November 2026).