PromptManager¶
Manages prompt templates with loading, caching, and rendering. Supports both Jinja2 (.jinja2) and Markdown (.md) templates.
Quick Example¶
from mamba_agents.prompts import PromptManager
manager = PromptManager()
# Load and render a template (works with .jinja2 or .md)
prompt = manager.render("system/assistant", name="Helper", tone="friendly")
# Get template object
template = manager.get("system/assistant")
variables = template.get_variables()
# Check template type
print(template.template_type) # TemplateType.JINJA2 or TemplateType.MARKDOWN
# Register runtime template (Jinja2 syntax)
manager.register("test/greeting", "Hello, {{ name }}!")
Key Methods¶
| Method | Description |
|---|---|
get(name, version) |
Get a PromptTemplate by name |
render(name, **vars) |
Render template with variables |
render_config(config) |
Render from TemplateConfig |
register(name, template) |
Register template at runtime |
list_prompts(category) |
List available templates |
list_versions(name) |
List versions for a template |
exists(name, version) |
Check if template exists |
clear_cache() |
Clear the template cache |
Caching Behavior¶
Templates are cached after first load (when enable_caching=True):
from mamba_agents.prompts import PromptManager, PromptConfig
# Caching enabled (default)
manager = PromptManager()
template1 = manager.get("system/assistant") # Loads from file
template2 = manager.get("system/assistant") # Returns cached
# Clear cache to reload
manager.clear_cache()
# Disable caching
config = PromptConfig(enable_caching=False)
manager = PromptManager(config=config)
Registered Templates¶
Runtime-registered templates take precedence over file-based templates:
manager = PromptManager()
# Register for testing
manager.register("system/assistant", "Test prompt: {{ name }}")
# This returns the registered template, not the file
prompt = manager.render("system/assistant", name="Test")
API Reference¶
PromptManager
¶
PromptManager(config: PromptConfig | None = None)
Manages prompt templates and rendering.
The PromptManager handles loading templates from files, caching, and rendering with variables. It supports versioned templates and runtime registration for testing.
Example
manager = PromptManager() template = manager.get("system/assistant") prompt = template.render(name="Code Helper")
Or render directly¶
prompt = manager.render("system/assistant", name="Code Helper")
Register a test template¶
manager.register("test/greeting", "Hello, {{ name }}!") manager.render("test/greeting", name="World") 'Hello, World!'
Initialize the prompt manager.
| PARAMETER | DESCRIPTION |
|---|---|
config
|
Prompt configuration. Uses defaults if not provided.
TYPE:
|
Source code in src/mamba_agents/prompts/manager.py
get
¶
get(
name: str, version: str | None = None
) -> PromptTemplate
Get a template by name.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
Template name (e.g., "system/assistant").
TYPE:
|
version
|
Template version. Uses default if not specified.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
PromptTemplate
|
PromptTemplate instance. |
| RAISES | DESCRIPTION |
|---|---|
PromptNotFoundError
|
If template doesn't exist. |
Source code in src/mamba_agents/prompts/manager.py
render
¶
Render a template with variables.
| PARAMETER | DESCRIPTION |
|---|---|
template_name
|
Template name (e.g., "system/assistant").
TYPE:
|
version
|
Template version. Uses default if not specified.
TYPE:
|
**variables
|
Variables to substitute in the template.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
Rendered template string. |
| RAISES | DESCRIPTION |
|---|---|
PromptNotFoundError
|
If template doesn't exist. |
TemplateRenderError
|
If rendering fails. |
Source code in src/mamba_agents/prompts/manager.py
render_config
¶
render_config(config: TemplateConfig) -> str
Render a template from a TemplateConfig.
| PARAMETER | DESCRIPTION |
|---|---|
config
|
Template configuration with name, version, and variables.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
Rendered template string. |
| RAISES | DESCRIPTION |
|---|---|
PromptNotFoundError
|
If template doesn't exist. |
TemplateRenderError
|
If rendering fails. |
Source code in src/mamba_agents/prompts/manager.py
register
¶
register(
name: str,
template: str | PromptTemplate,
version: str | None = None,
) -> None
Register a template programmatically.
This is useful for testing or dynamic template creation.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
Template name (e.g., "test/greeting").
TYPE:
|
template
|
Template source string or PromptTemplate instance.
TYPE:
|
version
|
Template version. Uses default if not specified.
TYPE:
|
Example
manager = PromptManager() manager.register("test/hello", "Hello, {{ name }}!") manager.render("test/hello", name="World") 'Hello, World!'
Source code in src/mamba_agents/prompts/manager.py
list_prompts
¶
List available prompt templates.
| PARAMETER | DESCRIPTION |
|---|---|
category
|
Optional category filter (e.g., "system", "workflow").
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[str]
|
List of template names (deduplicated across extensions). |
Source code in src/mamba_agents/prompts/manager.py
list_versions
¶
List available versions for a template.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
Template name (e.g., "system/assistant").
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[str]
|
List of version strings. |
Source code in src/mamba_agents/prompts/manager.py
clear_cache
¶
exists
¶
Check if a template exists.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
Template name.
TYPE:
|
version
|
Template version. Uses default if not specified.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if template exists, False otherwise. |