How to Use ChatPromptTemplate in Langchain: Syntax and Example
Use
ChatPromptTemplate in Langchain to create structured chat prompts by defining a list of message templates with variables. Instantiate it with from_messages passing message prompt templates, then call format_prompt with variables to generate the final prompt.Syntax
The ChatPromptTemplate is created using the from_messages method, which takes a list of message prompt templates like SystemMessagePromptTemplate and HumanMessagePromptTemplate. Each message template can have variables that you fill later. Use format_prompt with a dictionary of variables to get the full prompt ready for the chat model.
python
from langchain.prompts.chat import ( ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate ) # Define system and human message templates with variables system_template = SystemMessagePromptTemplate.from_template("You are a helpful assistant.") human_template = HumanMessagePromptTemplate.from_template("Translate this text to {language}: {text}") # Create ChatPromptTemplate from messages chat_prompt = ChatPromptTemplate.from_messages([system_template, human_template]) # Format prompt with variables final_prompt = chat_prompt.format_prompt(language="French", text="Hello, how are you?")
Example
This example shows how to create a chat prompt that instructs the assistant to translate text into a specified language. It uses ChatPromptTemplate with system and human messages, then formats the prompt with actual values.
python
from langchain.prompts.chat import ( ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate ) # Define the system message template system_msg = SystemMessagePromptTemplate.from_template("You are a helpful assistant.") # Define the human message template with variables human_msg = HumanMessagePromptTemplate.from_template("Translate this text to {language}: {text}") # Create the chat prompt template chat_prompt = ChatPromptTemplate.from_messages([system_msg, human_msg]) # Format the prompt with actual values formatted_prompt = chat_prompt.format_prompt(language="Spanish", text="Good morning") # Print the formatted messages for message in formatted_prompt.messages: print(f"{message.type}: {message.content}")
Output
system: You are a helpful assistant.
human: Translate this text to Spanish: Good morning
Common Pitfalls
- Not using
from_messagesto createChatPromptTemplatecauses errors because direct instantiation is not supported. - Forgetting to include all required variables in
format_promptleads to missing value errors. - Mixing message types incorrectly or not using message prompt templates like
SystemMessagePromptTemplateandHumanMessagePromptTemplatecan cause unexpected prompt structure.
python
from langchain.prompts.chat import ChatPromptTemplate # Wrong: Direct instantiation (will error) try: chat_prompt = ChatPromptTemplate(messages=["Hello {name}"]) except Exception as e: print(f"Error: {e}") # Right: Use from_messages with proper message templates from langchain.prompts.chat import HumanMessagePromptTemplate human_msg = HumanMessagePromptTemplate.from_template("Hello {name}") chat_prompt = ChatPromptTemplate.from_messages([human_msg]) formatted = chat_prompt.format_prompt(name="Alice") print(formatted.messages[0].content)
Output
Error: ChatPromptTemplate() takes no arguments
Hello Alice
Quick Reference
ChatPromptTemplate Quick Tips:
- Use
from_messagesto create the template from message prompt templates. - Message templates include
SystemMessagePromptTemplate,HumanMessagePromptTemplate, and others. - Call
format_promptwith variables to get the final prompt object. - The final prompt's
messageslist contains typed message objects ready for chat models.
Key Takeaways
Create ChatPromptTemplate using from_messages with message prompt templates.
Use format_prompt with all required variables to generate the final prompt.
Always use SystemMessagePromptTemplate and HumanMessagePromptTemplate for proper message types.
Avoid direct instantiation of ChatPromptTemplate; it requires from_messages method.
The formatted prompt contains a list of messages ready to send to chat models.