microsoft/semantic-kernel · error · AgentInvokeException

Failed to start conversation.

Error message

Failed to start conversation.

What it means

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.

Source

Thrown at python/samples/demos/copilot_studio_agent/src/direct_line_agent.py:188

           to fetch only the latest messages until an activity with type="event"
           and name="DynamicPlanFinished" is found.
        """
        await self._ensure_session()
        if not self.directline_token:
            await self._fetch_token_and_conversation()

        headers = {
            "Authorization": f"Bearer {self.directline_token}",
            "Content-Type": "application/json",
        }

        # Step 2: Start a conversation if one hasn't already been started.
        if not self.conversation_id:
            start_conv_url = f"{self.bot_endpoint}/conversations"
            async with self.session.post(start_conv_url, headers=headers) as resp:
                if resp.status not in (200, 201):
                    logger.error("Failed to start conversation. Status: %s", resp.status)
                    raise AgentInvokeException("Failed to start conversation.")
                conv_data = await resp.json()
                self.conversation_id = conv_data.get("conversationId")
                if not self.conversation_id:
                    raise AgentInvokeException("Conversation ID not found in start response.")

        # Step 3: Post the message payload.
        activities_url = f"{self.bot_endpoint}/conversations/{self.conversation_id}/activities"
        async with self.session.post(activities_url, json=payload, headers=headers) as resp:
            if resp.status != 200:
                logger.error("Failed to post activity. Status: %s", resp.status)
                raise AgentInvokeException("Failed to post activity.")
            _ = await resp.json()  # Response from posting activity is ignored.

        # Step 4: Poll for new activities using watermark until DynamicPlanFinished event is found.
        finished = False
        collected_data = None
        watermark = None
        while not finished:

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Check the logged status: 401/403 => token issue (re-fetch token); 404 => endpoint; 5xx => upstream.
  2. Ensure the token used in the Authorization header is valid and non-expired at conversation-start time.
  3. Confirm bot_endpoint is the DirectLine v3 base URL.

Example fix

// before
async with self.session.post(start_conv_url, headers=headers) as resp:
    if resp.status not in (200, 201):
        raise AgentInvokeException("Failed to start conversation.")

// after  # include status + body for diagnosis
async with self.session.post(start_conv_url, headers=headers) as resp:
    if resp.status not in (200, 201):
        body = await resp.text()
        raise AgentInvokeException(f"Failed to start conversation (status {resp.status}): {body}")
Defensive patterns

Strategy: retry

Try / catch

try:
    await agent._send_message(payload)
except AgentInvokeException as e:
    if "Failed to start conversation" in str(e):
        # token may have expired; refresh and retry once
        await agent._refresh_token()
        await agent._send_message(payload)
    raise

Prevention

When it happens

Trigger: First message in a session (no existing conversation_id); the conversations endpoint rejects with 401 (bad token), 403, 404 (wrong endpoint), or 5xx.

Common situations: DirectLine token expired between token-fetch and conversation-start; bot_endpoint wrong; service outage; token has insufficient scope to create conversations.

Related errors


AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13). Data as JSON: /api/errors/ed4e6681ade6a461. Report an issue: GitHub.