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 PromptMixin

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

Overview

PromptMixin is an abstract base class that provides a standardized interface for getting and setting prompts across LlamaIndex modules and their nested sub-modules.

Description

PromptMixin defines a hierarchical prompt management protocol used throughout LlamaIndex by query engines, response synthesizers, postprocessors, and other modules. It provides:

  • get_prompts() - Retrieves all prompts from the current module and recursively from all sub-modules. Sub-module prompts are namespaced using a colon separator (e.g., response_synthesizer:text_qa_template). Returns a deep copy to prevent accidental mutation.
  • update_prompts(prompts_dict) - Updates prompts at both the current level and in nested sub-modules. Keys containing a colon are routed to the appropriate sub-module by splitting on the first colon.
  • _validate_prompts() - Internal validation that ensures prompt keys and module names do not contain the reserved colon character, which is used as the namespace separator.

Subclasses must implement three abstract methods:

  • _get_prompts() - Return the module's own prompts as a Dict[str, BasePromptTemplate].
  • _get_prompt_modules() - Return a dictionary of sub-modules that also implement PromptMixin, enabling recursive prompt discovery.
  • _update_prompts(prompts_dict) - Apply prompt updates to the module's own prompts.

The module also defines three type aliases used across the codebase:

  • HasPromptType = Union[PromptMixin, BasePromptTemplate]
  • PromptDictType = Dict[str, BasePromptTemplate]
  • PromptMixinType = Dict[str, PromptMixin]

Usage

Use PromptMixin as a base class for any LlamaIndex component that manages prompts. It enables users to inspect and customize prompts at any level of a nested pipeline (e.g., changing the text QA prompt inside a response synthesizer that is inside a query engine) through a unified interface.

Code Reference

Source Location

Signature

class PromptMixin(ABC):
    def get_prompts(self) -> Dict[str, BasePromptTemplate]:
        ...

    def update_prompts(self, prompts_dict: Dict[str, BasePromptTemplate]) -> None:
        ...

    @abstractmethod
    def _get_prompts(self) -> PromptDictType:
        ...

    @abstractmethod
    def _get_prompt_modules(self) -> PromptMixinType:
        ...

    @abstractmethod
    def _update_prompts(self, prompts_dict: PromptDictType) -> None:
        ...

Import

from llama_index.core.prompts.mixin import PromptMixin, PromptDictType, PromptMixinType

I/O Contract

Inputs (get_prompts)

Name Type Required Description
(none) - - No parameters. Recursively collects prompts from self and all sub-modules.

Inputs (update_prompts)

Name Type Required Description
prompts_dict Dict[str, BasePromptTemplate] Yes Dictionary mapping prompt keys to prompt templates. Keys with colons are routed to sub-modules.

Outputs (get_prompts)

Name Type Description
prompts Dict[str, BasePromptTemplate] All prompts from the current module and sub-modules, with sub-module prompts namespaced by colon.

Usage Examples

from llama_index.core.prompts.mixin import PromptMixin, PromptDictType, PromptMixinType
from llama_index.core.prompts.base import BasePromptTemplate

# Inspect prompts in a query engine
query_engine = index.as_query_engine()
all_prompts = query_engine.get_prompts()
for key, prompt in all_prompts.items():
    print(f"{key}: {prompt.get_template()}")

# Update a specific prompt
from llama_index.core.prompts import PromptTemplate

new_qa_prompt = PromptTemplate(
    "Context: {context_str}\nQuestion: {query_str}\nAnswer concisely:"
)
query_engine.update_prompts({
    "response_synthesizer:text_qa_template": new_qa_prompt
})

Related Pages

Page Connections

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