Skip to content

Python: fix prompt template engine blocking HTML tags in content#13633

Open
OiPunk wants to merge 1 commit intomicrosoft:mainfrom
OiPunk:codex/semantic-kernel-13632-html-tag-prompt
Open

Python: fix prompt template engine blocking HTML tags in content#13633
OiPunk wants to merge 1 commit intomicrosoft:mainfrom
OiPunk:codex/semantic-kernel-13632-html-tag-prompt

Conversation

@OiPunk
Copy link

@OiPunk OiPunk commented Mar 5, 2026

Motivation and Context

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.

Description

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 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 XML
    tags as literal text in from_rendered_prompt()
  • python/tests/unit/contents/test_chat_history.py -- added four regression
    tests covering HTML <p>, multiple HTML tags, HTML surrounded by text,
    and SK tags mixed with HTML

Contribution Checklist

  • The code builds clean without any errors or warnings
  • The PR follows the SK Contribution Guidelines
  • The existing tests pass and new tests have been added
  • I didn't break any existing functionality

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
@OiPunk OiPunk requested a review from a team as a code owner March 5, 2026 04:40
@moonbox3 moonbox3 added the python Pull requests for the Python Semantic Kernel label Mar 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Pull requests for the Python Semantic Kernel

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