Chat Prompt Templates
What you'll learn: How to structure conversation prompts with distinct message types (system, human, AI) for chat-based LLM interactions.
Why Chat Templates Matter
Regular prompt templates treat input as plain text, but modern chat models like GPT-4 or Claude expect structured conversations. Each message has a role: system instructions, human questions, or AI responses. Chat prompt templates help you format these role-based messages properly.
Think of it like a screenplay: instead of writing one long monologue, you're organizing dialogue by character—who says what, and in what order.
Core Message Types
System messages set the AI's behavior and context (like "You are a helpful customer service agent").
Human messages represent user input or questions.
AI messages show previous assistant responses (useful for multi-turn conversations or few-shot examples).
Building Chat Templates
LangChain provides specialized templates that combine these message types:
from langchain.prompts import ChatPromptTemplate
template = ChatPromptTemplate.from_messages([
("system", "You are a helpful translator. Translate to {language}."),
("human", "{text}")
])
# Format with variables
prompt = template.format_messages(language="Spanish", text="Hello!")
This creates a structured conversation with clear roles, rather than jamming everything into one string.
When to Use Them
Use chat templates when:
- Working with chat-based models (not completion models)
- You need system-level instructions separate from user input
- Building multi-turn conversations or chatbots
- Showing few-shot examples as dialogue
Key Takeaway: Chat prompt templates organize prompts into role-based messages (system, human, AI), matching how modern chat models expect conversational input rather than plain text.