Skip to main content
TemplateFormatter: Jinja2-template-based formatter for legacy backends. TemplateFormatter extends [ChatFormatter](chat_formatter#class-chatformatter) to look up a per-component Jinja2 template at rendering time, allowing each [Component](../core/base#class-component) type to control its own prompt representation. Template discovery walks a configurable template_path and the built-in templates directory; results are cached in a [SimpleLRUCache](../backends/cache#class-simplelrucache) for performance. Use this formatter when your backend requires hand-crafted prompts rather than a generic chat-message rendering.

Classes

CLASS TemplateFormatter

Formatter that uses Jinja2 templates to render components into prompt strings. Template discovery walks a configurable template_path and the built-in templates directory. Results are optionally cached in a [SimpleLRUCache](../backends/cache#class-simplelrucache) for performance. Use this formatter when your backend requires hand-crafted prompts rather than generic chat-message rendering. Args:
  • model_id: Describes the model for which templates will be looked up. Should match the template directory structure.
  • template_path: An alternate location where templates can be found. Will be preferred over all other template directories even if a less exact match is found. Defaults to "".
  • use_template_cache: When True, caches template lookup results. Set to False if you plan to change model_id or template_path after construction. Defaults to True.
Example:: formatter = TemplateFormatter(model_id=“my-model”, template_path=“/path/to/templates”) text = formatter.print(my_component)
Methods:

FUNC print

print(self, c: Component | CBlock) -> str
Render a component or code block to a string using a Jinja2 template. Args:
  • c: The component or code block to render.
Returns:
  • The rendered string representation of the component.