Saturday, June 6, 2026

Difference between LangChain's PromptTemplate and ChatPromptTemplate

In LangChain, both PromptTemplate and ChatPromptTemplate are used to create prompts dynamically, but they are designed for different types of models.

Feature PromptTemplate ChatPromptTemplate
Purpose Creates a single text prompt Creates a structured chat conversation
Output String List of messages
Used With Traditional LLMs Chat models
Message Roles No roles Supports systemhuman ,usersystem , assistant, ai , tool roles
Best For Text completion models Modern chat-based models

1. PromptTemplate

PromptTemplate generates a single formatted string.

Example

from langchain.prompts import PromptTemplate

prompt = PromptTemplate(
    template="Explain {topic} in simple terms.",
    input_variables=["topic"]
)

formatted_prompt = prompt.format(topic="Transformers")

print(formatted_prompt)

Output

Explain Transformers in simple terms.

The result is just a string.

How it is sent to an LLM

llm.invoke(
    "Explain Transformers in simple terms."
)

This style was common with older completion models.

2. ChatPromptTemplate

ChatPromptTemplate creates a conversation consisting of multiple messages.

Example

from langchain.prompts import ChatPromptTemplate

prompt = ChatPromptTemplate.from_messages([
    ("system", "You are an expert teacher."),
    ("human", "Explain {topic} in simple terms.")
])

messages = prompt.format_messages(
    topic="Transformers"
)

print(messages)

Output

[
    SystemMessage(
        content="You are an expert teacher."
    ),
    HumanMessage(
        content="Explain Transformers in simple terms."
    )
]

Notice that the result is not a string.

It is a list of message objects.

How it is sent to a chat model

chat_model.invoke(messages)

Internally, the model receives:

System:
You are an expert teacher.

User:
Explain Transformers in simple terms.

Why ChatPromptTemplate Exists

Modern models such as:

  • OpenAI GPT models
  • Anthropic Claude models
  • Google Gemini models

are chat-oriented.

They understand different message roles:

System: You are a helpful assistant.
User: What is Kubernetes?
Assistant: Kubernetes is...
User: Explain it simply.

PromptTemplate cannot naturally represent this structure.

ChatPromptTemplate can.

Side-by-Side Example

PromptTemplate

prompt = PromptTemplate(
    template="""
You are an expert teacher.

Question:
{question}
"""
)

prompt.format(
    question="What is Kubernetes?"
)

Produces:

You are an expert teacher.

Question:
What is Kubernetes?

ChatPromptTemplate

prompt = ChatPromptTemplate.from_messages([
    ("system", "You are an expert teacher."),
    ("human", "{question}")
])

prompt.format_messages(
    question="What is Kubernetes?"
)

Produces:

[
    SystemMessage(
        content="You are an expert teacher."
    ),
    HumanMessage(
        content="What is Kubernetes?"
    )
]

Using Message Placeholders

One major advantage of ChatPromptTemplate is support for conversation history.

Example

from langchain.prompts import ChatPromptTemplate
from langchain.prompts import MessagesPlaceholder

prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful assistant."),
    MessagesPlaceholder("chat_history"),
    ("human", "{question}")
])

Then:

prompt.format_messages(
    chat_history=[
        HumanMessage(content="What is Kubernetes?"),
        AIMessage(content="Kubernetes is a container orchestrator.")
    ],
    question="Who created it?"
)

Result:

System: You are a helpful assistant.
User: What is Kubernetes?
Assistant: Kubernetes is a container orchestrator.
User: Who created it?

This is essential for conversational applications.

When to Use Which?

Scenario Recommended
Simple text generation PromptTemplate
Chatbots ChatPromptTemplate
RAG applications ChatPromptTemplate
Agentic AI ChatPromptTemplate
Conversation history ChatPromptTemplate
Modern GPT/Claude/Gemini models ChatPromptTemplate
Legacy completion models PromptTemplate

Practical Recommendation

For most new LangChain projects in 2026:

ChatPromptTemplate

is the preferred choice because nearly all modern LLMs are chat-based, and it works naturally with:

  • RAG pipelines
  • Agents
  • Tools
  • Memory
  • Multi-turn conversations
  • System instructions

Use PromptTemplate mainly when you specifically need to generate a single text string or are working with APIs that expect plain text rather than chat messages.

No comments:

Post a Comment

Difference between LangChain's PromptTemplate and ChatPromptTemplate

In LangChain, both PromptTemplate and ChatPromptTemplate are used to create prompts dynamically, but they are desi...