{"record":{"id":"ed4e6681ade6a461","repo":"microsoft/semantic-kernel","slug":"failed-to-start-conversation","errorCode":null,"errorMessage":"Failed to start conversation.","messagePattern":"Failed to start conversation\\.","errorType":"exception","errorClass":"AgentInvokeException","httpStatus":null,"severity":"error","filePath":"python/samples/demos/copilot_studio_agent/src/direct_line_agent.py","lineNumber":188,"sourceCode":"           to fetch only the latest messages until an activity with type=\"event\"\n           and name=\"DynamicPlanFinished\" is found.\n        \"\"\"\n        await self._ensure_session()\n        if not self.directline_token:\n            await self._fetch_token_and_conversation()\n\n        headers = {\n            \"Authorization\": f\"Bearer {self.directline_token}\",\n            \"Content-Type\": \"application/json\",\n        }\n\n        # Step 2: Start a conversation if one hasn't already been started.\n        if not self.conversation_id:\n            start_conv_url = f\"{self.bot_endpoint}/conversations\"\n            async with self.session.post(start_conv_url, headers=headers) as resp:\n                if resp.status not in (200, 201):\n                    logger.error(\"Failed to start conversation. Status: %s\", resp.status)\n                    raise AgentInvokeException(\"Failed to start conversation.\")\n                conv_data = await resp.json()\n                self.conversation_id = conv_data.get(\"conversationId\")\n                if not self.conversation_id:\n                    raise AgentInvokeException(\"Conversation ID not found in start response.\")\n\n        # Step 3: Post the message payload.\n        activities_url = f\"{self.bot_endpoint}/conversations/{self.conversation_id}/activities\"\n        async with self.session.post(activities_url, json=payload, headers=headers) as resp:\n            if resp.status != 200:\n                logger.error(\"Failed to post activity. Status: %s\", resp.status)\n                raise AgentInvokeException(\"Failed to post activity.\")\n            _ = await resp.json()  # Response from posting activity is ignored.\n\n        # Step 4: Poll for new activities using watermark until DynamicPlanFinished event is found.\n        finished = False\n        collected_data = None\n        watermark = None\n        while not finished:","sourceCodeStart":170,"sourceCodeEnd":206,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/samples/demos/copilot_studio_agent/src/direct_line_agent.py#L170-L206","documentation":"An AgentInvokeException raised when starting a DirectLine conversation fails: POST {bot_endpoint}/conversations returns a status outside (200, 201). The status code is logged. Conversation creation is a prerequisite to posting activities, so the flow cannot proceed.","triggerScenarios":"First message in a session (no existing conversation_id); the conversations endpoint rejects with 401 (bad token), 403, 404 (wrong endpoint), or 5xx.","commonSituations":"DirectLine token expired between token-fetch and conversation-start; bot_endpoint wrong; service outage; token has insufficient scope to create conversations.","solutions":["Check the logged status: 401/403 => token issue (re-fetch token); 404 => endpoint; 5xx => upstream.","Ensure the token used in the Authorization header is valid and non-expired at conversation-start time.","Confirm bot_endpoint is the DirectLine v3 base URL."],"exampleFix":"// before\nasync with self.session.post(start_conv_url, headers=headers) as resp:\n    if resp.status not in (200, 201):\n        raise AgentInvokeException(\"Failed to start conversation.\")\n\n// after  # include status + body for diagnosis\nasync with self.session.post(start_conv_url, headers=headers) as resp:\n    if resp.status not in (200, 201):\n        body = await resp.text()\n        raise AgentInvokeException(f\"Failed to start conversation (status {resp.status}): {body}\")","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    await agent._send_message(payload)\nexcept AgentInvokeException as e:\n    if \"Failed to start conversation\" in str(e):\n        # token may have expired; refresh and retry once\n        await agent._refresh_token()\n        await agent._send_message(payload)\n    raise","preventionTips":["Refresh the DirectLine token shortly before starting a conversation.","Confirm bot_endpoint is the DirectLine v3 base URL."],"tags":["network","directline","copilot-studio","semantic-kernel"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}