TemplateFormatter — a Jinja2-based system that lets you control exactly how each component
type is rendered for the model.
This page is for advanced users and library authors who need to customize how objects are
represented in prompts.
Templates
TheTemplateFormatter uses Jinja2 templates stored in a directory tree under
mellea/templates/prompts/. Each component type has a corresponding .jinja2 file that
controls its textual representation. The default templates are in
mellea/templates/prompts/default/.
Templates can also be stored directly on the class by returning a TemplateRepresentation
from format_for_llm(), rather than relying on a directory lookup.
Template lookup order
When rendering a component, theTemplateFormatter searches for a matching template in this
order:
- The formatter’s in-memory cache (if the template has been looked up recently)
- The formatter’s configured template path
- The package that owns the object being formatted (
melleaor a third-party package)
ibm-granite/granite-3.2-8b-instruct matches:
templates/ directory should not contain
multiple matches for the same model ID (e.g. both granite/ and ibm/ paths for the same
model string).
Template representations
A component’sformat_for_llm() method controls how it is rendered. It returns either a
plain string or a TemplateRepresentation object.
Plain string — skip the template engine entirely:
TemplateRepresentation — use the template engine:
TemplateRepresentation fields:
| Field | Description |
|---|---|
component | The object being rendered (usually self) |
args | Dict of variables passed to the Jinja2 template |
tools | List of tool/function descriptors exposed to the model |
template | Inline Jinja2 template string (alternative to template_order) |
template_order | List of template filenames to search for, in priority order |
Customizing templates for a component
To customize how an existing component is formatted for a specific model, subclass it and overrideformat_for_llm(), then create a new .jinja2 template file.
default/.
Advanced: For a worked example of advanced template customization, see
docs/examples/mify/rich_document_advanced.py
in the source repository.
See also: MObjects and mify |
Mellea core internals