Html formatting gets stripped off when using chatMessage for Teams graph

Rajen Jani40Reputation points
2025-04-25T09:28:19.5166667+00:00

I am trying to send a HTML formatted message on a OneOnOne chat using Teams graph in Python. So far it is working with plaintext but if I try to use HTML tags it does not format correctly. Here is the excerpt of the code:

chat_request_body = ChatMessage( body = ItemBody( content="<h2>This is a test.</h2>", content_type="BodyType.Html", ), 

The result:

User's image

If I write a oneOnone chat message in Teams with the same header h2 formatting and observe in a Fiddler capture I see that the same tags are being used. So I don't think it is the tags themselves that are the problem but something that I am not doing correctly.

When I set up the application I mostly used the "Mobile and desktops applications" platform. I also used the "web platform" as well but this made no difference either.

Any help with this much appreciated.

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
13,518 questions
0 commentsNo comments
{count} votes

1 answer

Sort by: Most helpful
  1. SrideviM2,460Reputation pointsMicrosoft External Staff
    2025-04-27T04:08:39.6566667+00:00

    Hello Rajen Jani,

    I understand you are trying to send an HTML-formatted message in a one-on-one Teams chat using Microsoft Graph Python SDK.

    Initially, I also faced the same issue where the HTML tags did not render properly and the message appeared as plain text when running your code, where content_type was passed as a string:

     chat_request_body = ChatMessage( body = ItemBody( content="<h2>This is a test.</h2>", content_type="BodyType.Html", ), 

    enter image description here

    Teams chat message:

    enter image description here

    The issue occurs because content_type must be set using the BodyType enum, not by passing a plain text value. Updating your code like this should fix it:

    import asyncio from azure.identity import InteractiveBrowserCredential from msgraph import GraphServiceClient from msgraph.generated.models.chat_message import ChatMessage from msgraph.generated.models.item_body import ItemBody from msgraph.generated.models.body_type import BodyType # <-- Import BodyType enum async def main(): credential = InteractiveBrowserCredential( client_id="appId", tenant_id="tennatId", redirect_uri="http://localhost:8400" ) client = GraphServiceClient(credentials=credential, scopes=["https://graph.microsoft.com/.default"]) chat_request_body = ChatMessage( body=ItemBody( content="<h2>This is a test.</h2>", content_type=BodyType.Html ) ) chat_id = "chatID" response = await client.chats.by_chat_id(chat_id).messages.post(chat_request_body) print(response) asyncio.run(main()) 

    enter image description here

    After making this change, the message is sent successfully, and Teams displays the heading properly in bold and larger font.

    enter image description here

    Let me know if you have any other questions or need any further assistance.

    Hope this helps!


    If this answer was helpful, please click "Accept the answer" and mark Yes, as this can help other community members.

    User's image

    If you have any other questions or are still experiencing issues, feel free to ask in the "comments" section, and I'd be happy to help.


Your answer