.Net: Bug: Special characters in Handlebars templates cause silent failure
Describe the bug
When using Handlebars templates with special characters like & (&), the output is incorrect. Instead of generating separate chat messages, it combines everything into one single message.
This issue occurs because the & character is transformed into & before reaching the XML transformation step, which causes an exception in XmlPromptParser. As a result, the process fails without clear feedback or proper exception handling, making it difficult to diagnose the issue.
To Reproduce
Steps to reproduce the behavior:
- Define a Handlebars template with special characters like
&:name: getTimes description: Gets the time in various cities. template: | <message role=""user"">Can you help me tell & the time in Seattle right now?</message> <message role=""assistant"">Sure! The time in Seattle is currently 3:00 PM.</message> <message role=""user"">What about New York?</message> template_format: handlebars - Use this template in the kernel function with
KernelFunctionYaml. - Invoke the function with the template and observe the result.
Expected behavior
This should generate three separate chat messages:
ChatMessageContent user => "Can you help me tell & the time in Seattle right now?"ChatMessageContent assistant => "Sure! The time in Seattle is currently 3:00 PM."ChatMessageContent user => "What about New York?"
Actual behavior
The entire content is combined into a single message:
ChatMessageContent user => "<message role="user">Can you help me tell & the time in Seattle right now?</message><message role="assistant">Sure! The time in Seattle is currently 3:00 PM.</message><message role="user">What about New York?</message>"
Platform
- OS: [Windows]
- IDE: [Visual Studio]
- Language: [C#]
- Source: [NuGet package version 1.34.0]
Code to reproduce
var kernel = Kernel.CreateBuilder()
.AddAzureOpenAIChatCompletion("gpt-4o", "https://xxxxxx.openai.azure.com/", "xxxxxxxx")
.Build();
const string template = @"
name: getTimes
description: Gets the time in various cities.
template: |
<message role=""user"">Can you help me tell & the time in Seattle right now?</message>
<message role=""assistant"">Sure! The time in Seattle is currently 3:00 PM.</message>
<message role=""user"">What about New York?</message>
template_format: handlebars
";
var kernelFunction = KernelFunctionYaml.FromPromptYaml(template, new HandlebarsPromptTemplateFactory());
var result = await kernel.InvokeAsync(kernelFunction);
Additional context
I have tried using AllowUnsafeContent, but it does not resolve the problem in this specific case since the & is not part of a dynamic variable.
As a workaround, I have to escape the & character twice. This is necessary because, during the Handlebars rendering, the result is decoded after the template is rendered (as seen in this line). If I only escape the & once, it gets decoded during this step and would return as &, which would then cause the error in the XmlPromptParser.
name: getTimes
description: Gets the time in various cities.
template: |
<message role=""user"">Can you help me tell &amp; the time in Seattle right now?</message>
<message role=""assistant"">Sure! The time in Seattle is currently 3:00 PM.</message>
<message role=""user"">What about New York?</message>
template_format: handlebars
We're facing a similar issue, working around it with a very unpleasant hack.
Perhaps there is a way to escape the & in the handlebars template?
For instance, the escape of these special characters using the standard from Handlebars notation of "triple-stash" ({{{) does not work.
Reference: https://handlebarsjs.com/guide/expressions.html#html-escaping