semantic-kernel icon indicating copy to clipboard operation
semantic-kernel copied to clipboard

.Net: Bug: Special characters in Handlebars templates cause silent failure

Open LuisM000 opened this issue 1 year ago • 1 comments

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:

  1. 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 &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
    
  2. Use this template in the kernel function with KernelFunctionYaml.
  3. Invoke the function with the template and observe the result.

Expected behavior
This should generate three separate chat messages:

  1. ChatMessageContent user => "Can you help me tell & the time in Seattle right now?"
  2. ChatMessageContent assistant => "Sure! The time in Seattle is currently 3:00 PM."
  3. ChatMessageContent user => "What about New York?"

Actual behavior
The entire content is combined into a single message:

  1. 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 &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
";

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 &amp; 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;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

LuisM000 avatar Jan 24 '25 10:01 LuisM000

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

rliberoff avatar Jan 27 '25 14:01 rliberoff