Python: fix prompt template engine blocking HTML tags in content#13633
Open
OiPunk wants to merge 1 commit intomicrosoft:mainfrom
Open
Python: fix prompt template engine blocking HTML tags in content#13633OiPunk wants to merge 1 commit intomicrosoft:mainfrom
OiPunk wants to merge 1 commit 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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