Skip to main content

safe_format

Function
This function is defined in the agents_generator module.
Safely format a string template, preserving JSON-like curly braces. This handles cases where templates contain Gutenberg block syntax like {“level”:2} which would cause KeyError with standard .format(). Uses a two-pass approach:
  1. Escape all {{ and }} (already escaped braces)
  2. Only substitute known variable placeholders

Signature

def safe_format(template: str) -> str

Parameters

template
str
required
String template with {variable} placeholders **kwargs: Variable substitutions to apply

Returns

Returns
str
Formatted string with variables substituted and JSON preserved

Usage

>>> safe_format('Use <!-- wp:heading {"level":2} --> for {topic}', topic='AI')
    'Use <!-- wp:heading {"level":2} --> for AI'