agenta icon indicating copy to clipboard operation
agenta copied to clipboard

[AGE-2945] [Bug] Variables with Backslashes Cause TemplateFormatError in Playground

Open dv0 opened this issue 5 months ago • 2 comments

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:

  1. Open Agenta-AI Playground
  2. Insert a variable (e.g., {{var}}) in the prompt
  3. Set the variable value to test \s \d
  4. Click "Run"
  5. Observe the error
Image

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/\d as invalid escape sequences
  • Workaround: Use double backslashes (\\s, \\d) in variables, but this behavior should be documented or handled gracefully

AGE-2945

dv0 avatar Jul 24 '25 04:07 dv0

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  Chat with Dosu Join Discord Share on X

dosubot[bot] avatar Jul 24 '25 04:07 dosubot[bot]

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.

mmabrouk avatar Jul 24 '25 08:07 mmabrouk