Overview
Defines the prompt template class hierarchy including BasePromptTemplate, PromptTemplate, ChatPromptTemplate, SelectorPromptTemplate, and LangchainPromptTemplate for constructing and formatting prompts used throughout the LlamaIndex framework.
Description
The base.py module provides the foundational prompt template system for LlamaIndex:
- BasePromptTemplate is the abstract base class that extends BaseModel (Pydantic) and ABC. It defines the core interface: partial_format() for pre-filling template variables, format() for rendering to a string, format_messages() for rendering to a list of ChatMessage objects, and get_template() for returning the raw template string. It supports template_var_mappings for aliasing variable names and function_mappings for computing template variable values dynamically via callback functions. The _map_all_vars method chains function mappings and then template variable mappings.
- PromptTemplate is the primary string-based prompt template. It accepts a template string with Python format-style variables (e.g., {context_str}, {query_str}). The constructor automatically extracts template variables using get_template_vars. The format() method applies variable mappings, renders the template, optionally processes through an output_parser, and optionally applies a completion_to_prompt transformation. The format_messages() method converts the formatted string to a list of messages via prompt_to_messages.
- ChatPromptTemplate is designed for chat-based LLMs. It accepts a list of ChatMessage templates, each containing template variables in their content. The format_messages() method iterates through each message template, formats TextBlock content blocks with the provided variables, and preserves non-text blocks (images, etc.) as-is. It supports output parser integration via format_messages. The from_messages class method provides a convenience constructor accepting either tuples of (role, content) or ChatMessage objects.
- SelectorPromptTemplate enables conditional prompt selection based on the LLM being used. It wraps a default_template and optional conditionals (pairs of predicate functions and templates). The select() method evaluates each condition against the provided LLM and returns the first matching template, falling back to the default.
- LangchainPromptTemplate provides compatibility with LangChain prompt templates via a ConditionalPromptSelector. It requires the llama_index[langchain] extra and supports both template-based and selector-based initialization.
The module also defines Prompt as a backwards-compatible alias for PromptTemplate.
Usage
Use PromptTemplate for completion-style LLM interactions, ChatPromptTemplate for chat-based LLMs with role-structured messages, SelectorPromptTemplate when different LLMs require different prompt formats, and LangchainPromptTemplate when integrating with LangChain prompt systems.
Code Reference
Source Location
Signature
class BasePromptTemplate(BaseModel, ABC):
metadata: Dict[str, Any]
template_vars: List[str]
kwargs: Dict[str, str]
output_parser: Optional[BaseOutputParser]
template_var_mappings: Optional[Dict[str, Any]]
function_mappings: Optional[Dict[str, AnnotatedCallable]]
def partial_format(self, **kwargs: Any) -> "BasePromptTemplate": ...
def format(self, llm: Optional[BaseLLM] = None, **kwargs: Any) -> str: ...
def format_messages(self, llm: Optional[BaseLLM] = None, **kwargs: Any) -> List[ChatMessage]: ...
def get_template(self, llm: Optional[BaseLLM] = None) -> str: ...
class PromptTemplate(BasePromptTemplate):
template: str
def __init__(
self,
template: str,
prompt_type: str = PromptType.CUSTOM,
output_parser: Optional[BaseOutputParser] = None,
metadata: Optional[Dict[str, Any]] = None,
template_var_mappings: Optional[Dict[str, Any]] = None,
function_mappings: Optional[Dict[str, Callable]] = None,
**kwargs: Any,
) -> None: ...
class ChatPromptTemplate(BasePromptTemplate):
message_templates: List[ChatMessage]
def __init__(
self,
message_templates: Sequence[ChatMessage],
prompt_type: str = PromptType.CUSTOM,
output_parser: Optional[BaseOutputParser] = None,
metadata: Optional[Dict[str, Any]] = None,
template_var_mappings: Optional[Dict[str, Any]] = None,
function_mappings: Optional[Dict[str, Callable]] = None,
**kwargs: Any,
): ...
class SelectorPromptTemplate(BasePromptTemplate):
default_template: SerializeAsAny[BasePromptTemplate]
conditionals: Optional[Sequence[Tuple[Callable[[BaseLLM], bool], BasePromptTemplate]]]
def __init__(
self,
default_template: BasePromptTemplate,
conditionals: Optional[
Sequence[Tuple[Callable[[BaseLLM], bool], BasePromptTemplate]]
] = None,
): ...
def select(self, llm: Optional[BaseLLM] = None) -> BasePromptTemplate: ...
Import
from llama_index.core.prompts.base import PromptTemplate
from llama_index.core.prompts.base import ChatPromptTemplate
from llama_index.core.prompts.base import SelectorPromptTemplate
from llama_index.core.prompts.base import BasePromptTemplate
from llama_index.core.prompts.base import LangchainPromptTemplate
I/O Contract
Inputs (PromptTemplate.__init__)
| Name |
Type |
Required |
Description
|
| template |
str |
Yes |
Template string with Python format-style variables (e.g., {context_str}, {query_str})
|
| prompt_type |
str |
No |
Type classification of the prompt (default: PromptType.CUSTOM)
|
| output_parser |
Optional[BaseOutputParser] |
No |
Parser to post-process the formatted prompt output
|
| metadata |
Optional[Dict[str, Any]] |
No |
Additional metadata associated with the prompt
|
| template_var_mappings |
Optional[Dict[str, Any]] |
No |
Mappings to alias template variable names
|
| function_mappings |
Optional[Dict[str, Callable]] |
No |
Functions that dynamically compute template variable values from kwargs
|
Inputs (PromptTemplate.format)
| Name |
Type |
Required |
Description
|
| llm |
Optional[BaseLLM] |
No |
LLM instance (unused in PromptTemplate, used in SelectorPromptTemplate)
|
| completion_to_prompt |
Optional[Callable[[str], str]] |
No |
Optional transformation applied after formatting
|
| **kwargs |
Any |
No |
Template variable values to fill in
|
Inputs (ChatPromptTemplate.__init__)
| Name |
Type |
Required |
Description
|
| message_templates |
Sequence[ChatMessage] |
Yes |
List of ChatMessage objects with template variables in content
|
| prompt_type |
str |
No |
Type classification of the prompt (default: PromptType.CUSTOM)
|
| output_parser |
Optional[BaseOutputParser] |
No |
Parser to post-process formatted messages
|
| metadata |
Optional[Dict[str, Any]] |
No |
Additional metadata
|
| template_var_mappings |
Optional[Dict[str, Any]] |
No |
Mappings to alias template variable names
|
| function_mappings |
Optional[Dict[str, Callable]] |
No |
Functions that dynamically compute template variable values
|
Outputs
| Name |
Type |
Description
|
| partial_format() |
BasePromptTemplate |
A new template instance with some variables pre-filled
|
| format() |
str |
The fully rendered prompt string
|
| format_messages() |
List[ChatMessage] |
A list of formatted chat messages
|
| get_template() |
str |
The raw template string
|
Usage Examples
Basic Usage with PromptTemplate
from llama_index.core.prompts.base import PromptTemplate
template = PromptTemplate(
"Context: {context_str}\nQuery: {query_str}\nAnswer: "
)
# Format with variables
prompt_str = template.format(
context_str="LlamaIndex is a data framework for LLMs.",
query_str="What is LlamaIndex?"
)
print(prompt_str)
Using ChatPromptTemplate
from llama_index.core.prompts.base import ChatPromptTemplate
from llama_index.core.base.llms.types import ChatMessage
chat_template = ChatPromptTemplate.from_messages([
("system", "You are a helpful assistant. Use the context: {context_str}"),
("user", "{query_str}"),
])
messages = chat_template.format_messages(
context_str="LlamaIndex is a data framework.",
query_str="What does it do?"
)
Partial Formatting
from llama_index.core.prompts.base import PromptTemplate
template = PromptTemplate(
"Context: {context_str}\nQuery: {query_str}\nAnswer: "
)
# Pre-fill context, fill query later
partial_template = template.partial_format(context_str="Some context")
final_prompt = partial_template.format(query_str="What is this about?")
Related Pages