Overview
Prompt templates allow you to create reusable prompts with variable substitution. LangChain provides templates for both string-based prompts and chat-based prompts.BasePromptTemplate
Base class for all prompt templates. Source:langchain_core.prompts.base:39
Inherits: RunnableSerializable[dict, PromptValue]
Type Parameters
The output type when formatting the prompt
Properties
List of variable names required as inputs to the prompt
List of optional variable names (auto-inferred from placeholders)
Dictionary mapping variable names to their expected types. Defaults to
str for all.Optional output parser to apply to the LLM response
Partial variables that are pre-filled
Metadata for tracing
Tags for tracing
Methods
invoke
Dictionary of variable values
Formatted prompt value
format_prompt
partial
Variables to pre-fill
New prompt template with partial variables
PromptTemplate
String prompt template using Python’sstr.format syntax.
Source: langchain_core.prompts.prompt
Inherits: BasePromptTemplate
Properties
The template string with
{variable} placeholdersFormat of the template string
Whether to validate the template on initialization
Constructor
Template string with variables in
{variable} formatTemplate format (
'f-string', 'jinja2', or 'mustache')Variables to pre-fill
Example
ChatPromptTemplate
Template for chat-based prompts using messages. Source:langchain_core.prompts.chat
Inherits: BasePromptTemplate
Properties
List of message templates or placeholders
Constructor
List of message templates. Each can be:
- A tuple like
("human", "Hello {name}") - A
BaseMessageinstance - A
BaseMessagePromptTemplate - A
MessagesPlaceholder
Example
Class Methods
from_template
Template string
Role of the message (
'human', 'ai', 'system', etc.)MessagesPlaceholder
Placeholder for inserting a list of messages. Source:langchain_core.prompts.chat:52
Inherits: BaseMessagePromptTemplate
Properties
Name of the variable containing messages
Whether the placeholder is optional. If
True, returns empty list when variable not provided.Maximum number of messages to include. If
None, includes all.Constructor
Name of variable containing messages
Whether the variable is optional
Example
HumanMessagePromptTemplate
Template for creating human messages. Source:langchain_core.prompts.message
Inherits: BaseMessagePromptTemplate
Properties
The underlying string prompt template
Constructor
AIMessagePromptTemplate
Template for creating AI messages. Source:langchain_core.prompts.message
Inherits: BaseMessagePromptTemplate
SystemMessagePromptTemplate
Template for creating system messages. Source:langchain_core.prompts.message
Inherits: BaseMessagePromptTemplate
FewShotPromptTemplate
Template for few-shot learning with examples. Source:langchain_core.prompts.few_shot
Inherits: BasePromptTemplate
Properties
List of example dictionaries
Selector to choose examples dynamically
Template for formatting each example
Suffix template appended after examples
Prefix template prepended before examples
String separator between examples
Example
PipelinePromptTemplate
Compose multiple prompt templates in a pipeline. Source:langchain_core.prompts.pipeline
Inherits: BasePromptTemplate
Properties
Final prompt template to format
List of
(name, prompt) tuples to execute in orderImagePromptTemplate
Template for creating prompts with images. Source:langchain_core.prompts.image
Inherits: BasePromptTemplate
Properties
Image URL template or image data template