Python: fix prompt template engine blocking HTML tags in content#13633
Python: fix prompt template engine blocking HTML tags in content#13633weiguangli-io wants to merge 5 commits intomicrosoft:mainfrom
Conversation
When a prompt contains HTML tags like <p>, <div>, etc., the ChatHistory.from_rendered_prompt() method was parsing them as XML elements and silently discarding their content because they did not match any known SK template tags (message, chat_history). The fix treats unknown XML tags as literal text by serializing them back to their original string representation and appending that text to the surrounding message content, so HTML markup passes through to the LLM unchanged. Fixes microsoft#13632
|
@moonbox3 / Anyone - Can you please review and merge this? We have a blocker on this issue. Thank you. |
|
We need to get a separate SK PR in so these tests can pass. Working on it now. |
|
Have a look at the failing pre-commit. Please make sure you have it installed locally, so it runs when you commit changes: See here for more details: https://github.com/microsoft/semantic-kernel/blob/main/python/DEV_SETUP.md#using-uv |
Head branch was pushed to by a user without write access
@weiguangli-io - Is this something you can help with? |
Motivation and Context
Fixes #13632
When a prompt contains HTML tags such as
<p>,<div>,<b>, etc., theChatHistory.from_rendered_prompt()method wraps the rendered prompt in a<root>element and parses it as XML. Any child elements whose tag names donot match the known SK template tags (
message,chat_history) were silentlyskipped, causing their text content to be lost. This meant a prompt like:
would arrive at the LLM as an empty message, because
<p>What is your name?</p>was discarded during XML traversal.
Description
In
ChatHistory.from_rendered_prompt, when an XML child element has anunrecognized tag (i.e. it is not
messageorchat_history), the elementis now serialized back to its original text representation and appended to
the preceding message content. The element's tail text is likewise kept in
the same message, preserving the full original prompt text.
Changed files:
python/semantic_kernel/contents/chat_history.py-- handle unknown XMLtags as literal text in
from_rendered_prompt()python/tests/unit/contents/test_chat_history.py-- added four regressiontests covering HTML
<p>, multiple HTML tags, HTML surrounded by text,and SK tags mixed with HTML
Contribution Checklist