Skip to content

fix(python): allow HTML tags like <p> in prompt templates#13653

Closed
gambletan wants to merge 2 commits intomicrosoft:mainfrom
gambletan:fix/html-tags-in-prompt-templates
Closed

fix(python): allow HTML tags like <p> in prompt templates#13653
gambletan wants to merge 2 commits intomicrosoft:mainfrom
gambletan:fix/html-tags-in-prompt-templates

Conversation

@gambletan
Copy link

Summary

Fixes #13632

When a prompt contains HTML tags such as <p>, <div>, <b>, etc., the ChatHistory.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:

Translate: "<p>What is your name?</p>"

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 not message or chat_history), the element is now serialized back to its original text representation using tostring() 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 in from_rendered_prompt()
  • python/tests/unit/contents/test_chat_history.py -- added four regression tests covering:
    • HTML <p> tag (the reported issue)
    • Multiple HTML tags (<p> + <div>)
    • HTML tags with surrounding plain text
    • SK template tags mixed with HTML

Test plan

…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
@gambletan gambletan requested a review from a team as a code owner March 13, 2026 05:22
@moonbox3
Copy link
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.

@moonbox3 moonbox3 closed this Mar 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: Bug: HTML tag <p> getting blocked in Semantic Kernel version 1.39.4

2 participants