Azure Communication Chat Client Library for Python

raw JSON →
1.3.0 verified Fri May 01 auth: no python

Microsoft Azure Communication Chat Client Library for Python enables developers to integrate real-time chat capabilities into applications. Current version is 1.3.0, requires Python >=3.8, and follows a monthly release cadence aligned with the Azure SDK for Python.

pip install azure-communication-chat
error AttributeError: module 'azure.communication.chat' has no attribute 'ChatThreadClient'
cause In older versions (pre-1.1.0), ChatThreadClient was accessed differently. In 1.2.0+, ChatThreadClient is obtained via ChatClient.get_chat_thread_client().
fix
Use chat_client.get_chat_thread_client(thread_id) to obtain a ChatThreadClient instance.
error TypeError: Object of type 'list' is not JSON serializable
cause Passing a plain Python list for 'members' in CreateChatThreadRequest instead of a list of ChatParticipant objects.
fix
Create ChatParticipant objects: from azure.communication.chat import ChatParticipant; members = [ChatParticipant(user=..., display_name=...)]
error HttpRequestError: (InvalidURL) Invalid URL
cause The endpoint URL is missing a trailing slash or is incorrectly formatted.
fix
Ensure the endpoint ends with '/', e.g., 'https://myresource.communication.azure.com/'.
gotcha ChatClient constructor requires a CommunicationTokenCredential, not an AzureKeyCredential or DefaultAzureCredential. Using the wrong credential type will raise a TypeError.
fix Use CommunicationTokenCredential with a valid user access token obtained from the Communication Identity API.
gotcha The endpoint URL must include the trailing forward slash (e.g., 'https://<resource>.communication.azure.com/'). Missing the slash will cause an InvalidURL error.
fix Ensure the endpoint string ends with a '/'.
breaking In version 1.2.0, the 'members' parameter in CreateChatThreadRequest changed from a list of dicts to a list of ChatParticipant objects. Using plain dicts will raise an AttributeError.
fix Use azure.communication.chat.ChatParticipant objects with 'user', 'display_name', and 'share_history_time'.
gotcha The CommunicationTokenCredential does not auto-refresh tokens. For long-lived applications, implement token refresh logic and wrap it in a callback.
fix Use the overloaded constructor that accepts a callable for token refresh: CommunicationTokenCredential(token_refresh_callback).

Creates a chat thread with two participants using an endpoint and user access token.

import os
from azure.communication.chat import ChatClient, CommunicationTokenCredential

endpoint = os.environ.get("COMMUNICATION_SERVICES_ENDPOINT", "")
user_access_token = os.environ.get("USER_ACCESS_TOKEN", "")

credential = CommunicationTokenCredential(user_access_token)
chat_client = ChatClient(endpoint, credential)

# Create a chat thread
from azure.communication.chat import CreateChatThreadRequest

create_chat_thread_request = CreateChatThreadRequest(
    topic="Hello World!",
    members=[
        {"user": "8:acs:...", "display_name": "User1"},
        {"user": "8:acs:...", "display_name": "User2"}
    ]
)

result = chat_client.create_chat_thread(create_chat_thread_request)
print(f"Chat thread created with ID: {result.chat_thread.id}")