LangChain/Prompts & Output
★ OverviewBeginner8 min

Prompts That Work

ChatPromptTemplate, MessagesPlaceholder, few-shot prompting, and variable injection — everything you need to write prompts that produce consistent results.

Quick Reference

  • ChatPromptTemplate.from_messages() for multi-role prompts
  • MessagesPlaceholder injects dynamic message history
  • FewShotChatMessagePromptTemplate for in-context examples
  • Variables use {curly_braces} — escaped with double braces {{literal}}

ChatPromptTemplate

Before looking at ChatPromptTemplate, here's how you'd do the same thing with raw strings and message objects:

Without ChatPromptTemplate — raw strings

This works for a one-off call. But it breaks down quickly — you can't reuse the template, can't compose it with LCEL, can't partial-fill variables, and there's no validation if you forget a variable. ChatPromptTemplate solves all of this:

With ChatPromptTemplate — reusable, composable, validated
When to use raw messages vs ChatPromptTemplate

Use raw SystemMessage/HumanMessage when you're making a single hardcoded call with no variables. Use ChatPromptTemplate as soon as you have variables, need to reuse the prompt across multiple inputs, or want to compose it into a chain with |.

Variables use {curly_braces}. The template validates that all variables are provided at invoke time. If you need a literal brace in output, escape it with double braces: {{literal}}.