prompt_renderer
Jinja2-based system prompt renderer with SSTI hardening.
Loads a .j2 template file once at startup and renders it on each call
with room-specific and tool-specific context variables. Uses a
SandboxedEnvironment and recursively sanitises
user-controllable values to prevent server-side template injection.
- prompt_renderer.sanitize_context(value)[source]
Recursively strip Jinja2 metacharacters from user-controllable strings.
Replaces
{{,}},{%,%},{#,#}with Unicode full-width lookalikes so they cannot be interpreted as template syntax if an| tojsonfilter is ever omitted.Non-string leaves (ints, floats, bools,
None) pass through unchanged. Dicts and lists are walked recursively.
- class prompt_renderer.PromptRenderer(template_path, default_extras=None)[source]
Bases:
objectRender a Jinja2 system-prompt template with per-request context.
Uses
SandboxedEnvironmentto prevent template injection even if a caller accidentally passes unsanitised user data.- Parameters:
- render(context=None)[source]
Render the template with the supplied context.
All values in context are recursively sanitised to strip Jinja2 metacharacters before rendering.
The following keys are automatically injected if not already present:
current_date– today’s date inYYYY-MM-DDformat (UTC).
Keys from default_extras (set at init or later) are included but can be overridden by context.