fix(python): allow HTML tags like <p> in prompt templates#13653
Closed
gambletan wants to merge 2 commits intomicrosoft:mainfrom
Closed
fix(python): allow HTML tags like <p> in prompt templates#13653gambletan wants to merge 2 commits intomicrosoft:mainfrom
gambletan wants to merge 2 commits intomicrosoft:mainfrom
Conversation
…rom_rendered_prompt When a prompt contains HTML tags like <p>, <div>, <b>, etc., the from_rendered_prompt method wraps the prompt in a <root> element and parses it as XML. Previously, child elements whose tag names did not match known SK template tags (message, chat_history) were silently skipped, causing their text content to be lost. Now, unrecognized tags are serialized back to their original text representation and appended to the preceding message content, preserving the full original prompt. Fixes microsoft#13632
Add four test cases to verify that HTML tags in prompts are preserved as literal text and not silently dropped by the XML parser in from_rendered_prompt(). Covers: single <p> tag, multiple HTML tags, HTML with surrounding text, and SK template tags mixed with HTML. Related to microsoft#13632
Collaborator
|
Hi @gambletan can you help me understand why we're opening multiple PRs to solve the same issue? As I posted in your other PR, this is fixed in #13633. |
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.
Summary
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 do not match the known SK template tags (message,chat_history) were silently skipped, 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.Changes
In
ChatHistory.from_rendered_prompt, when an XML child element has an unrecognized tag (i.e. it is notmessageorchat_history), the element is now serialized back to its original text representation usingtostring()and appended to the preceding message content. The element's tail text is likewise preserved in the same message, keeping the full original prompt intact.Changed files:
python/semantic_kernel/contents/chat_history.py-- handle unknown XML tags as literal text infrom_rendered_prompt()python/tests/unit/contents/test_chat_history.py-- added four regression tests covering:<p>tag (the reported issue)<p>+<div>)Test plan