LangChain and LangGraph · Chapter 2 of 25

Prompt templates

By the end of this chapter you can write a prompt once and fill it for any tenant with a ChatPromptTemplate, say what filling it gives back, leave a slot for a whole conversation with MessagesPlaceholder, teach a model a format with few-shot examples, and explain why a tenant's message with curly braces breaks an f-string but not a template. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: LangChain basics
  • 4 tasks
  • Python
  • Free chapter

Free with a Google or GitHub sign-in, like the first 2 chapters of every lab.

Why you need this

In chapter 1 you typed every message by hand. RoomRaja's assistant will send thousands of requests, and most of each one is the same: the same instructions, the same tone, the same rules. Only a few parts change: the tenant's name, their property, their question, what was said before.

Mrunal wants every reply to greet the tenant by name. Faisal wants every message sorted (rent, deposit, repairs) before anyone reads it. Both need one prompt written once and filled in many times, safely. That is a prompt template.

The lesson

The f-string way, and where it hurts

The first idea is an f-string:

name, prop = "Kavya", "RoomRaja Baner"
messages = [
    ("system", f"You are RoomRaja's tenant assistant. You are talking to {name}, who stays at {prop}."),
    ("human", "Is lunch included?"),
]
print(llm.invoke(messages).content)
All three meals, breakfast, lunch and dinner, are included in the rent.

It works for one tenant. It hurts as the assistant grows: the prompt is rebuilt in every function that needs it, a forgotten value is not noticed until a tenant reads "Hi None", and anything the tenant types goes straight into your instructions. A template fixes all three, and it is also a building block that plugs into the rest of LangChain (chapter 3).

ChatPromptTemplate

A ChatPromptTemplate is a list of messages, each a role and a text, where {curly braces} mark the variables:

from langchain_core.prompts import ChatPromptTemplate

prompt = ChatPromptTemplate.from_messages([
    ("system", "You are RoomRaja's tenant assistant. You are talking to {name}, who stays at {property}."),
    ("human", "{question}"),
])
print(prompt.input_variables)
['name', 'property', 'question']

Making it sends nothing and fills nothing: LangChain only reads the texts and records which variables they need. Filling it is invoke with a dictionary that gives each variable a value:

value = prompt.invoke({"name": "Kavya", "property": "RoomRaja Baner", "question": "Is lunch included?"})
print(value.to_messages())
[SystemMessage(content="You are RoomRaja's tenant assistant. You are talking to Kavya, who stays at RoomRaja Baner.", additional_kwargs={}, response_metadata={}), HumanMessage(content='Is lunch included?', additional_kwargs={}, response_metadata={})]

What comes back is a ChatPromptValue: the finished messages, the same SystemMessage and HumanMessage you wrote by hand in chapter 1. A chat model's invoke takes it directly: llm.invoke(value). The template, the value and the model all have an invoke method; that is no accident, and chapter 3 builds on it.

lab requests 1 shows that the model never sees a template, only the filled messages (trimmed):

"messages": [
  {"content": "You are RoomRaja's tenant assistant. You are talking to Kavya, who stays at RoomRaja Baner.", "role": "system"},
  {"content": "Is lunch included?", "role": "user"}
]

A missing value is an error

Forget a variable and the template stops you before anything is sent:

KeyError: "Input to ChatPromptTemplate is missing variables {'property'}.  Expected: ['name', 'property', 'question'] Received: ['name', 'question']\nNote: if you intended {property} to be part of the string and not a variable, please escape it with double curly braces like: '{{property}}'. ..."

(trimmed). That is better than an f-string with a None in it: the mistake shows up on your screen, not on a tenant's phone.

A slot for the conversation: MessagesPlaceholder

A model remembers nothing between requests. To continue a conversation you send the earlier messages again, every time. MessagesPlaceholder leaves a slot in the template for a whole list of messages:

from langchain_core.prompts import MessagesPlaceholder

chat = ChatPromptTemplate.from_messages([
    ("system", "You are RoomRaja's tenant assistant."),
    MessagesPlaceholder("history"),
    ("human", "{question}"),
])
history = [("human", "Hi, I am Kavya from BAN-203."), ("ai", "Hi Kavya! How can I help?")]
print(llm.invoke(chat.invoke({"history": history, "question": "What is my name?"})).content)
print(llm.invoke(chat.invoke({"history": [], "question": "What is my name?"})).content)
Your name is Kavya.
I don't know your name: you have not told me in this conversation.

history is a variable like any other (chat.input_variables is ['history', 'question']), but its value is a list of messages, and each one becomes its own message in the request, with its own role. The second call sent an empty history, so the model had nothing to go on.

Few-shot examples

Describing what you want is one way. Showing it is often better: a few example questions, each followed by the answer you want, placed before the real question. That is few-shot prompting, and FewShotChatMessagePromptTemplate builds it from a list of examples and a small template for one example:

from langchain_core.prompts import FewShotChatMessagePromptTemplate

examples = [
    {"message": "Breakfast kab tak milta hai?", "label": "food"},
    {"message": "I want to leave at the end of next month", "label": "notice"},
]
example_prompt = ChatPromptTemplate.from_messages([("human", "{message}"), ("ai", "{label}")])
few_shot = FewShotChatMessagePromptTemplate(examples=examples, example_prompt=example_prompt)
prompt = ChatPromptTemplate.from_messages([
    ("system", "Classify the tenant's message. Reply with the label only."),
    few_shot,
    ("human", "{message}"),
])

The few-shot template fills the example prompt once per example and puts the messages where it sits. The model sees a conversation in which it already "answered" twice, briefly, with one word:

{"content": "Classify the tenant's message. Reply with the label only.", "role": "system"},
{"content": "Breakfast kab tak milta hai?", "role": "user"},
{"content": "food", "role": "assistant"},
{"content": "I want to leave at the end of next month", "role": "user"},
{"content": "notice", "role": "assistant"},
{"content": "The tap is leaking", "role": "user"}
Is dinner veg only? -> food
The tap is leaking -> notice

The examples taught the format (one word) and the labels. They taught the only labels, too: with no example of a repair, a leaking tap became "notice". Give one example for every label you want back.

Braces in the tenant's words

Tenants paste all sorts of things. Here is what happens when a message with braces is pasted into a template's text with an f-string:

text = "Why does my receipt say {month} instead of October?"
prompt = ChatPromptTemplate.from_messages([
    ("system", "You are RoomRaja's tenant assistant."),
    ("human", f"Tenant's message: {text}"),
])
prompt.invoke({})
KeyError: "Input to ChatPromptTemplate is missing variables {'month'}.  Expected: ['month'] Received: [] ..."

The f-string ran first and put the tenant's {month} into the template's text, where the template read it as a variable. The rule: a template's text is yours and is read for variables; a variable's value is just text and is never read again. So the tenant's words belong in a value, never in the template's text.

Your turn

Four tasks. Task 1 fills one template for three tenants: build it once. Task 2 sends a conversation through a placeholder. Task 3 is a few-shot classifier. Task 4 is an interview question: a tenant's message with braces, and why an f-string breaks on it while a template does not.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    reply_template.py: one template, three tenants

    Mrunal wants every reply to greet the tenant by name. Make one ChatPromptTemplate with two messages: the system message, You are RoomRaja's tenant assistant. You are talking to {name}, who stays at {property}. and the human message, the variable {question}.

    Use it for three tenants, T1012, T1047 and T1093, in that order, each asking: What time is breakfast?

    Get each tenant's name and property from roomraja.get_tenant(tenant_id) (import roomraja). Send each filled template to lab-chat and print one line per tenant: the tenant's name, a colon and a space, then the reply.

  2. Task 2

    conversation.py: a slot for the conversation

    A tenant is in the middle of a WhatsApp conversation. Make a ChatPromptTemplate with the system message You are RoomRaja's tenant assistant., then a MessagesPlaceholder named history, then the human message {question}.

    Fill it with this history, in order: human: Hi, my name is Rohan Mehta, room WAK-108. ai: Hi Rohan! How can I help you today? human: The fan in my room is making noise all night. ai: Sorry about that. Please raise a ticket in the app.

    and the question: Do you remember my name?

    Send it to lab-chat and print only the reply.

  3. Task 3

    few_shot.py: teaching by example

    Faisal wants messages sorted before anyone reads them. Build a few-shot classifier prompt: the system message Classify the tenant's message. Reply with the label only., then these four examples, each a human message followed by an ai message with its label:

    The lift is stuck again: maintenance

    Can I get a receipt for my rent?: rent

    My sister wants to visit on Sunday: guest

    Is the deposit refundable?: deposit

    then the human message {message}. Use FewShotChatMessagePromptTemplate with an example prompt of two messages.

    Classify these three messages with lab-chat, in order, and print one line each: the message, a space, an arrow ->, a space, then the label the model replied with.

    fan making noise all night

    Is the deposit two months or one month?

    Can my brother stay over this Saturday?

  4. Task 4 · interview question

    Interview: braces.py, the message that breaks an f-string

    A tenant writes, exactly: My receipt says {late_fee} where the amount should be. What do I owe?

    Send it to lab-chat through a ChatPromptTemplate with the system message You are RoomRaja's tenant assistant. and the tenant's words as the human message. The model must receive the tenant's message exactly, braces included. Print only the reply.

    The interviewer will ask why pasting the message into the template's text with an f-string breaks, and how a template keeps the tenant's words safe.

What you will learn

  • ChatPromptTemplate: messages with variables, filled with invoke.
  • What filling gives back: a ChatPromptValue, ready for the model.
  • MessagesPlaceholder: a slot for a list of messages, such as a conversation.
  • Few-shot examples: showing the model what you want instead of describing it.
  • Braces in values: why the tenant's words are safe in a variable.

More in LangChain basics

Build "Prompt templates" yourself

RoomRaja, a Pune co-living company, answers tenants on WhatsApp by hand. You build its assistant. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

Free with a Google or GitHub sign-in, like the first 2 chapters of every lab.