Skip to content

Python: Fix HTML tags silently dropped in prompt templates#13654

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

Python: Fix HTML tags silently dropped in prompt templates#13654
gambletan wants to merge 2 commits intomicrosoft:mainfrom
gambletan:fix/html-tag-in-prompt-template

Conversation

@gambletan
Copy link

Summary

Fixes #13632

When a prompt template contains HTML tags like <p>, the ChatHistory.from_rendered_prompt() method wraps the rendered prompt in a <root> element and parses it as XML. The XML parser treats <p> as a valid XML element, but since the code only recognizes <message> and <chat_history> tags, the <p> element (and its content) is silently dropped. This causes the model to receive an empty/truncated prompt.

Root cause

In python/semantic_kernel/contents/chat_history.py, the from_rendered_prompt() method iterates over child XML elements but only handles message and chat_history tags. Any other valid XML element (including HTML tags like <p>, <div>, <b>, etc.) is ignored, losing both the element and its text content.

Fix

When an unrecognized XML element is encountered, serialize it back to its string representation using tostring() and append it (along with any trailing text) to the preceding message's content. This preserves HTML tags in the prompt text sent to the model.

Changes

  • python/semantic_kernel/contents/chat_history.py: Added else branch in from_rendered_prompt() to handle unrecognized XML elements by converting them back to string form
  • python/tests/unit/contents/test_chat_history.py: Added regression tests for HTML tags (<p>, <b>, <i>) in prompt templates

Test plan

  • New test test_chat_history_from_rendered_prompt_with_html_tags verifies the exact scenario from Python: Bug: HTML tag <p> getting blocked in Semantic Kernel version 1.39.4 #13632
  • New test test_chat_history_from_rendered_prompt_with_multiple_html_tags verifies multiple HTML tags
  • New test test_chat_history_from_rendered_prompt_html_with_message_tags verifies HTML works alongside <message> tags
  • Existing tests should continue to pass (recognized <message> and <chat_history> tags unaffected)

🤖 Generated with Claude Code

When a prompt template contains HTML tags like <p>, the XML parser
in from_rendered_prompt() would silently drop them because they are
not recognized as 'message' or 'chat_history' elements.

This fix serializes unrecognized XML elements back to their string
form and appends them to the preceding message content, preserving
HTML tags like <p>, <div>, etc. in the final prompt sent to the model.

Fixes microsoft#13632
Add tests verifying that HTML tags like <p>, <b>, <i> in prompt
templates are preserved when parsed by from_rendered_prompt().

Covers the scenario reported in microsoft#13632.
@moonbox3
Copy link
Collaborator

Hi @gambletan, we appreciate the contribution. This issue is already fixed an open PR: #13633. Closing this as a duplicate.

@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