Skip to content

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

Merged
moonbox3 merged 3 commits intomicrosoft:mainfrom
weiguangli-io:codex/semantic-kernel-13632-html-tag-prompt
Mar 18, 2026
Merged

Python: fix prompt template engine blocking HTML tags in content#13633
moonbox3 merged 3 commits intomicrosoft:mainfrom
weiguangli-io:codex/semantic-kernel-13632-html-tag-prompt

Conversation

@weiguangli-io
Copy link
Contributor

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

@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

@wagmihodl0
Copy link

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

@weiguangli-io - Is this something you can help with?

@moonbox3
Copy link
Collaborator

Yes, it is the dev's responsibility to handle failing pre-commit. It is simple to install them, and fix the items based on the output while running git commit -m "<some message" which triggers the pre-commit locally. Please see my last message above.

weiguangli-io and others added 2 commits March 17, 2026 22:19
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 force-pushed the codex/semantic-kernel-13632-html-tag-prompt branch from 7790755 to 69a421b Compare March 17, 2026 14:23
@moonbox3 moonbox3 enabled auto-merge March 18, 2026 02:15
@moonbox3 moonbox3 added this pull request to the merge queue Mar 18, 2026
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Mar 18, 2026
@moonbox3 moonbox3 added this pull request to the merge queue Mar 18, 2026
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Mar 18, 2026
@moonbox3 moonbox3 added this pull request to the merge queue Mar 18, 2026
Merged via the queue into microsoft:main with commit e500f72 Mar 18, 2026
36 checks passed
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