[AGE-2945] [Bug] Variables with Backslashes Cause TemplateFormatError in Playground
Describe the bug
When editing prompts in the Playground UI, adding a variable with content containing backslashes (e.g., test \s \d) triggers a TemplateFormatError. The system incorrectly interprets backslashes in variable values as escape sequences during template rendering.
To Reproduce
Steps to reproduce:
- Open Agenta-AI Playground
- Insert a variable (e.g.,
{{var}}) in the prompt - Set the variable value to
test \s \d - Click "Run"
- Observe the error
Expected behavior
Variables should accept raw backslashes as literal characters or explicitly document required escaping rules. The system should not throw template format errors for unescaped backslashes in user-provided variable content.
Additional Information
- Full error:
TemplateFormatError: Error in message 0 (system): Error formatting template 'You are an expert in geography': bad escape \s at position 6 - Likely cause: Python's template engine attempts to parse
\s/\das invalid escape sequences - Workaround: Use double backslashes (
\\s,\\d) in variables, but this behavior should be documented or handled gracefully
Hi @dv0! I'm Dosu and I’m helping the agenta team.
This error happens because the backend passes variable values with single backslashes (like \s or \d) directly to the Jinja2 template engine, which interprets them as escape sequences. Since \s and \d aren't valid Python escapes, this triggers a TemplateFormatError during rendering. The frontend doesn't alter or escape these values; escaping should be handled server-side, but currently isn't, so users must double the backslashes (e.g., \\s, \\d) as a workaround. There is no documentation or UI guidance about this requirement yet, and no recent changes address it. For now, use double backslashes in variable values to avoid errors, and consider documenting this behavior for users who encounter it. More technical details are available in the template rendering logic.
To reply, just mention @dosu.
How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other
Hi @dv0
Thanks for the very well written issue!
Yes, I agree, we should not consider these as escape sequences in the inputs. Just simple strings.
This is related to #2698
I'll try to create a PR this week to improve the way we handle variables in the chat and completion services.
In the mean time, let me know if you are interested in contributing.