Skip to content

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: str

version

Template version (e.g., "v1").

TYPE: str

source

Raw template source code.

TYPE: str

template_type

Type of template (JINJA2 or MARKDOWN).

TYPE: TemplateType

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(**variables: Any) -> str

Render the template with the given variables.

PARAMETER DESCRIPTION
**variables

Variables to substitute in the template.

TYPE: Any DEFAULT: {}

RETURNS DESCRIPTION
str

Rendered template string.

RAISES DESCRIPTION
TemplateRenderError

If rendering fails.

Source code in src/mamba_agents/prompts/template.py
def render(self, **variables: Any) -> str:
    """Render the template with the given variables.

    Args:
        **variables: Variables to substitute in the template.

    Returns:
        Rendered template string.

    Raises:
        TemplateRenderError: If rendering fails.
    """
    # Merge default variables with provided ones
    merged_vars = {**self._default_variables, **variables}

    if self.template_type == TemplateType.MARKDOWN:
        return self._render_markdown(merged_vars)
    return self._render_jinja2(merged_vars)

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: Any DEFAULT: {}

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
def with_variables(self, **variables: Any) -> PromptTemplate:
    """Create a new template with default variables set.

    This is useful for partial application of variables.

    Args:
        **variables: Default variables for the new template.

    Returns:
        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.'
    """
    return PromptTemplate(
        name=self.name,
        version=self.version,
        source=self.source,
        template_type=self.template_type,
        _compiled=self._compiled,
        _default_variables={**self._default_variables, **variables},
        _strict=self._strict,
    )

get_variables

get_variables() -> set[str]

Get the set of variable names used in this template.

RETURNS DESCRIPTION
set[str]

Set of variable names found in the template.

Source code in src/mamba_agents/prompts/template.py
def get_variables(self) -> set[str]:
    """Get the set of variable names used in this template.

    Returns:
        Set of variable names found in the template.
    """
    if self.template_type == TemplateType.MARKDOWN:
        from mamba_agents.prompts.markdown import get_markdown_variables

        return get_markdown_variables(self.source)

    from jinja2 import Environment, meta

    env = Environment()
    ast = env.parse(self.source)
    return meta.find_undeclared_variables(ast)