PromptTemplate¶
A renderable prompt template supporting both Jinja2 and Markdown formats.
TemplateType Enum¶
from mamba_agents.prompts import TemplateType
TemplateType.JINJA2 # Jinja2 template with {{ var }} syntax
TemplateType.MARKDOWN # Markdown template with {var} syntax
Quick Example¶
from mamba_agents.prompts import PromptTemplate, TemplateType
# Jinja2 template (default)
jinja_template = PromptTemplate(
name="greeting",
version="v1",
source="Hello, {{ name }}! You are {{ role }}.",
)
# Markdown template
md_template = PromptTemplate(
name="greeting",
version="v1",
source="Hello, {name}! You are {role}.",
template_type=TemplateType.MARKDOWN,
)
# Render with variables
prompt = jinja_template.render(name="Claude", role="helpful")
# "Hello, Claude! You are helpful."
Key Fields¶
| Field | Type | Default | Description |
|---|---|---|---|
name |
str | required | Template name |
version |
str | required | Template version |
source |
str | required | Template source code |
template_type |
TemplateType | JINJA2 |
Template format |
Key Methods¶
| Method | Description |
|---|---|
render(**vars) |
Render template with variables |
with_variables(**vars) |
Create template with default variables |
get_variables() |
Get set of variable names in template |
Rendering¶
from mamba_agents.prompts import PromptManager
manager = PromptManager()
template = manager.get("system/assistant")
# Basic rendering
prompt = template.render(name="Helper", tone="friendly")
# Get required variables
variables = template.get_variables()
print(variables) # {'name', 'tone', 'expertise'}
Partial Application¶
Pre-fill some variables to create a specialized template:
template = PromptTemplate(
name="sys",
version="v1",
source="Hello {{ name }}, you are {{ role }} with {{ tone }} style.",
)
# Create partially applied template
helper = template.with_variables(name="Claude", tone="friendly")
# Render with remaining variables
prompt = helper.render(role="an assistant")
# "Hello Claude, you are an assistant with friendly style."
Markdown Templates¶
Markdown templates use {var} syntax instead of Jinja2's {{ var }}:
from mamba_agents.prompts import PromptTemplate, TemplateType
template = PromptTemplate(
name="assistant",
version="v1",
source="You are {name}, a {role}.",
template_type=TemplateType.MARKDOWN,
)
# Render
prompt = template.render(name="Claude", role="helpful assistant")
# "You are Claude, a helpful assistant."
# Get variables works the same way
variables = template.get_variables()
# {'name', 'role'}
Escaping Braces in Markdown¶
Use double braces for literal output:
template = PromptTemplate(
name="example",
version="v1",
source="Use {{braces}} for JSON. Hello, {name}!",
template_type=TemplateType.MARKDOWN,
)
template.render(name="World")
# "Use {braces} for JSON. Hello, World!"
Template Compilation¶
Templates are compiled lazily on first render:
template = PromptTemplate(
name="test",
version="v1",
source="{{ greeting }}, {{ name }}!",
)
# Template not compiled yet
prompt = template.render(greeting="Hello", name="World") # Compiles here
# Subsequent renders reuse compiled template
prompt2 = template.render(greeting="Hi", name="User") # Uses cached
API Reference¶
PromptTemplate
dataclass
¶
PromptTemplate(
name: str,
version: str,
source: str,
template_type: TemplateType = JINJA2,
_compiled: Template | None = None,
_default_variables: dict[str, Any] = dict(),
_strict: bool = False,
)
A renderable prompt template.
Supports both Jinja2 templates ({{ var }} syntax) and markdown templates ({var} syntax with YAML frontmatter).
| ATTRIBUTE | DESCRIPTION |
|---|---|
name |
Template name (e.g., "system/assistant").
TYPE:
|
version |
Template version (e.g., "v1").
TYPE:
|
source |
Raw template source code.
TYPE:
|
template_type |
Type of template (JINJA2 or MARKDOWN).
TYPE:
|
Example
Jinja2 template:
template = PromptTemplate( ... name="system/assistant", ... version="v1", ... source="You are {{ name }}.", ... ) template.render(name="a helpful assistant") 'You are a helpful assistant.'
Markdown template:
template = PromptTemplate( ... name="system/assistant", ... version="v1", ... source="You are {name}.", ... template_type=TemplateType.MARKDOWN, ... ) template.render(name="a helpful assistant") 'You are a helpful assistant.'
render
¶
Render the template with the given variables.
| PARAMETER | DESCRIPTION |
|---|---|
**variables
|
Variables to substitute in the template.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
Rendered template string. |
| RAISES | DESCRIPTION |
|---|---|
TemplateRenderError
|
If rendering fails. |
Source code in src/mamba_agents/prompts/template.py
with_variables
¶
with_variables(**variables: Any) -> PromptTemplate
Create a new template with default variables set.
This is useful for partial application of variables.
| PARAMETER | DESCRIPTION |
|---|---|
**variables
|
Default variables for the new template.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
PromptTemplate
|
New PromptTemplate with default variables set. |
Example
base = PromptTemplate("sys", "v1", "Hello {{ name }}, you are {{ role }}.") greeter = base.with_variables(name="Claude") greeter.render(role="helpful") 'Hello Claude, you are helpful.'
Source code in src/mamba_agents/prompts/template.py
get_variables
¶
Get the set of variable names used in this template.
| RETURNS | DESCRIPTION |
|---|---|
set[str]
|
Set of variable names found in the template. |