diff --git a/python/semantic_kernel/contents/chat_message_content.py b/python/semantic_kernel/contents/chat_message_content.py index 13376106e8b1..80c15defb539 100644 --- a/python/semantic_kernel/contents/chat_message_content.py +++ b/python/semantic_kernel/contents/chat_message_content.py @@ -294,11 +294,15 @@ def from_element(cls, element: Element) -> "ChatMessageContent": def to_prompt(self) -> str: """Convert the ChatMessageContent to a prompt. + The encoding attribute of the content is not used here, + this always returns a string, the encoding is only relevant + for the actual text content, not for the prompt representation. + Returns: str - The prompt from the ChatMessageContent. """ root = self.to_element() - return ElementTree.tostring(root, encoding=self.encoding or "unicode", short_empty_elements=False) + return ElementTree.tostring(root, encoding="unicode", short_empty_elements=False) def to_dict(self, role_key: str = "role", content_key: str = "content") -> dict[str, Any]: """Serialize the ChatMessageContent to a dictionary. diff --git a/python/tests/unit/contents/test_chat_message_content.py b/python/tests/unit/contents/test_chat_message_content.py index a4734dfe4614..5d409f06f073 100644 --- a/python/tests/unit/contents/test_chat_message_content.py +++ b/python/tests/unit/contents/test_chat_message_content.py @@ -152,6 +152,24 @@ def test_cmc_to_prompt(): assert prompt == 'Hello, world!' +@pytest.mark.parametrize( + "encoding", + [ + pytest.param("utf-8", id="utf8"), + pytest.param("ascii", id="ascii"), + pytest.param("latin-1", id="latin1"), + ], +) +def test_cmc_to_prompt_with_encoding(encoding: str): + """The encoding of the content should not change the type or shape of the prompt.""" + message = ChatMessageContent(role=AuthorRole.USER, content="Hello, wörld!", encoding=encoding) + prompt = message.to_prompt() + assert isinstance(prompt, str) + assert not prompt.startswith("