Python SDK: Initiating cobrowse session via Conversations API returns 400 Bad Request

How should I properly to initiate a cobrowse session programmatically through the Conversations API using the Python SDK? I am building an automation script to trigger a cobrowse session when a specific ticket status changes, but every attempt results in a 400 Bad Request with the message ‘Invalid conversation type’. I have verified that my OAuth2 token has the necessary ‘cobrowse’ and ‘conversation:write’ scopes, and the user ID I am passing is valid and currently online. Here is the code snippet I am using:

import purecloud_platform_client as pc
from purecloud_platform_client.rest import ApiException

client = pc.PureCloudPlatformClientV2()
client.authenticate(client_credentials=pc.ClientCredentials(client_id=‘my_client_id’, client_secret=‘my_client_secret’))

api_instance = pc.ConversationsApi(client)
body = pc.CobrowseConversationCreateRequest(
to=[{‘id’: ‘agent_user_id_123’, ‘type’: ‘user’}],
type=‘cobrowse’,
wrap_up_code=‘NoWrapUp’
)

try:
api_response = api_instance.post_conversations_cobrowse(body)
print(api_response)
except ApiException as e:
print(“Exception when calling ConversationsApi->post_conversations_cobrowse: %s\n” % e)

The error traceback points to the payload validation. I suspect the structure of the ‘to’ array or the ‘type’ field might be incorrect for the cobrowse endpoint specifically, as standard outbound calls work fine with similar payloads. Is there a specific JSON schema or additional parameter required for cobrowse initiation that is not documented in the standard Conversations API reference? I need to know the exact payload structure that successfully creates a cobrowse session without manual intervention.

Have you tried isolating the payload structure to ensure the conversationType field explicitly matches the enum value expected by the Conversations API? The 400 Bad Request error often stems from mismatched type definitions rather than authentication failures. When using the Python SDK (PureCloudPlatformClientV2), the CreateConversationRequest object requires strict adherence to the schema defined in the OpenAPI specification.

The issue likely arises from passing a string that does not exactly match the cobrowse type identifier or omitting required participant data. Below is the correct pattern for initiating a cobrowse session. Ensure you are using the ConversationsApi client and constructing the request body with the precise type and participants array.

from platformclientv2 import Configuration, ApiClient, ConversationsApi, CreateConversationRequest

def initiate_cobrowse(user_id, agent_id):
 config = Configuration()
 api_client = ApiClient(configuration=config)
 conversations_api = ConversationsApi(api_client)

 # Construct the request body explicitly
 request = CreateConversationRequest()
 request.type = 'cobrowse'
 
 # Participants must include both the agent and the user
 request.participants = [
 {'userId': agent_id, 'role': 'agent'},
 {'userId': user_id, 'role': 'user'}
 ]

 try:
 response = conversations_api.post_conversations(request=request)
 return response
 except Exception as e:
 # Log the specific 400 error details for New Relic instrumentation
 log_error(e)
 return None

Verify that the userId for the user is indeed active in the platform. If the user is not in an active voice or chat context, the cobrowse session may fail validation. Refer to the internal documentation on Conversational Session Lifecycle Management for state requirements.