Skip to content

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

Open
weiguangli-io wants to merge 5 commits intomicrosoft:mainfrom
weiguangli-io:codex/semantic-kernel-13632-html-tag-prompt
Open

Python: fix prompt template engine blocking HTML tags in content#13633
weiguangli-io wants to merge 5 commits intomicrosoft:mainfrom
weiguangli-io:codex/semantic-kernel-13632-html-tag-prompt

Conversation

@weiguangli-io
Copy link

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
@weiguangli-io weiguangli-io 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
@wagmihodl0
Copy link

@moonbox3 / Anyone - Can you please review and merge this? We have a blocker on this issue. Thank you.

@moonbox3
Copy link
Collaborator

We need to get a separate SK PR in so these tests can pass. Working on it now.

@moonbox3 moonbox3 enabled auto-merge March 12, 2026 22:06
@moonbox3
Copy link
Collaborator

Have a look at the failing pre-commit. Please make sure you have it installed locally, so it runs when you commit changes: uv run pre-commit install -c python/.pre-commit-config.yaml

See here for more details: https://github.com/microsoft/semantic-kernel/blob/main/python/DEV_SETUP.md#using-uv

auto-merge was automatically disabled March 13, 2026 03:16

Head branch was pushed to by a user without write access

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

4 participants