Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Run llama Llama index RichPromptTemplate

From Leeroopedia
Knowledge Sources
Domains Prompts, Multimodal, Templates
Last Updated 2026-02-11 19:00 GMT

Overview

RichPromptTemplate is a prompt template class that uses the banks library to support rich content types including text, images, and audio, as well as chat-style multi-message prompts.

Description

RichPromptTemplate extends BasePromptTemplate and integrates with the banks library (via banks.Prompt) to provide advanced templating capabilities beyond simple string formatting. Key features include:

Multimodal content support: When formatting messages, the template can produce ContentBlock objects including TextBlock, ImageBlock, and AudioBlock, enabling prompts that contain images or audio alongside text.

Chat template detection: The is_chat_template property checks for the presence of "endchat" in the template string to determine whether the template should be formatted as a series of chat messages or as a single string.

Dual formatting modes:

  • format() - For non-chat templates, renders the template as a plain string using banks.Prompt.text(). For chat templates, calls format_messages() and then converts messages to a single string using the provided or default messages_to_prompt function.
  • format_messages() - Parses the template into a list of ChatMessage objects using banks.Prompt.chat_messages(). Each message's content can be a string or a list of content blocks. The method maps banks content block types (text, image_url, audio) to LlamaIndex content block types (TextBlock, ImageBlock, AudioBlock).

Template variables are automatically extracted from the template string by the banks library if not explicitly provided. The partial_format() method returns a deep copy with additional kwargs merged in, enabling incremental template filling.

Output parser integration is supported: if an output_parser is set, it is applied to format the messages after rendering.

Usage

Use RichPromptTemplate when you need prompts that include multimodal content (images, audio) or when you want to leverage the banks library's advanced templating features such as chat message structuring. It is particularly useful for vision-language models or audio-capable models where prompts must carry non-text content blocks.

Code Reference

Source Location

Signature

class RichPromptTemplate(BasePromptTemplate):
    template_str: str = Field(description="The template string for the prompt.")

    def __init__(
        self,
        template_str: str,
        metadata: Optional[Dict[str, Any]] = None,
        output_parser: Optional[BaseOutputParser] = None,
        template_vars: Optional[List[str]] = None,
        template_var_mappings: Optional[Dict[str, Any]] = None,
        function_mappings: Optional[Dict[str, Callable]] = None,
        **kwargs: Any,
    ):

Import

from llama_index.core.prompts.rich import RichPromptTemplate

I/O Contract

Inputs

Name Type Required Description
template_str str Yes The banks-compatible template string. May include chat delimiters (endchat) for multi-message prompts.
metadata Optional[Dict[str, Any]] No Optional metadata dictionary for the prompt.
output_parser Optional[BaseOutputParser] No Output parser to apply to formatted messages.
template_vars Optional[List[str]] No List of template variable names. Auto-extracted from template if not provided.
template_var_mappings Optional[Dict[str, Any]] No Mappings for template variable name transformations.
function_mappings Optional[Dict[str, Callable]] No Mappings from variable names to callable functions.

Outputs (format)

Name Type Description
result str The rendered prompt string. For chat templates, messages are converted to a string via messages_to_prompt.

Outputs (format_messages)

Name Type Description
messages List[ChatMessage] List of ChatMessage objects, potentially containing multimodal content blocks (TextBlock, ImageBlock, AudioBlock).

Usage Examples

from llama_index.core.prompts.rich import RichPromptTemplate

# Simple text template
template = RichPromptTemplate(
    template_str="Answer the following question: {{ query_str }}\nContext: {{ context_str }}"
)
result = template.format(query_str="What is AI?", context_str="AI is artificial intelligence.")

# Partial formatting
partial = template.partial_format(context_str="AI is artificial intelligence.")
result = partial.format(query_str="What is AI?")

# Get the raw template string
raw = template.get_template()

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment