By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
An f-string prompt typed at the call site is configuration with no history: nobody knows which version produced yesterday's answer, a tone change ships without review, and no test can exercise it. Treat the support prompt the way you treat an API contract — a named, versioned, reviewed artefact with explicit inputs. ChatPromptTemplate gives you that structure: messages with roles, declared variables, fillable partials, and a template object the app and the evaluation suite can share.
A chat model does not take one string; it takes a list of messages. ChatPromptTemplate.from_messages keeps that shape visible: the system turn carries identity and policy, the human turn carries the request. The role names human and user are interchangeable, as are ai and assistant; pick one convention and keep it. Curly braces declare variables, and format_messages fills them. It raises if a variable is missing, which is exactly the failure you want at build time rather than in production.
from langchain_core.prompts import ChatPromptTemplate
prompt = ChatPromptTemplate.from_messages(
[
(
"system",
"You are Harbor Helper, the support agent for Harbor Supply. "
"Answer only from the tools and passages you are given. "
"Reply in a {tone} tone.",
),
("human", "Order {order_id}: {question}"),
]
)
messages = prompt.format_messages(
tone="concise",
order_id="4521",
question="Where is my delivery?",
)
for message in messages:
print(f"{message.type}: {message.content}")Some values should not be per-request. The brand rules, the policy tone and the current prompt version are configuration: fill them once with partial and leave only genuine per-ticket fields open. Partials also let you compose a shared system block into several templates without copy-pasting the text — the classic way prompts drift apart.
from langchain_core.prompts import ChatPromptTemplate
SYSTEM = ChatPromptTemplate.from_messages(
[
("system", "You are Harbor Helper, the support agent for Harbor Supply."),
(
"system",
"Escalate to a human when the docs do not cover the question. "
"Reply in a {tone} tone.",
),
]
)
TICKET = ChatPromptTemplate.from_messages(
[("human", "Order {order_id}: {question}")]
)
# Configuration is fixed once; only the ticket fields stay open.
branded = SYSTEM.partial(tone="calm and precise")
messages = branded.format_messages() + TICKET.format_messages(
order_id="4521",
question="Can I still refund a damaged mug?",
)
print(len(messages))
print(messages[1].content)Give every prompt a version string and log it with each run. When an answer is wrong, the first question is which prompt produced it; when you change a prompt, the evaluation suite from Lesson 18 tells you whether the change is an improvement or a regression. A version bump is cheap; reconstructing a lost prompt is not.
| Change | Treat it as | Do this |
|---|---|---|
| Wording or tone change | Prompt-only change | Bump the version and re-run the eval suite |
| New variable | Interface change | Bump the version, update every call site and test the missing-variable error |
| Model or temperature change | Behaviour change | Re-run evals for quality and cost, not just for crashes |
Key takeaways
A prompt is versioned configuration: name it, declare its variables and review changes like code.
ChatPromptTemplate.from_messages keeps system and human roles explicit; format_messages fails on a missing variable.
Use partial to fix brand and policy configuration once, and compose shared blocks instead of copy-pasting them.
Log the prompt version with every run, and re-run the eval suite whenever a prompt, model or parameter changes.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.