microsoft/semantic-kernel · error · AgentInvokeException

No token received from token generation.

Error message

No token received from token generation.

What it means

An AgentInvokeException raised in the bot_secret branch of DirectLine token acquisition: the POST to {bot_endpoint}/tokens/generate returned HTTP 200 but the JSON body had no "token" field. The library treats a 200 without a token as a malformed response and refuses to proceed, logging the raw body before raising.

Source

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

            self.session = aiohttp.ClientSession()

    async def _fetch_token_and_conversation(self) -> None:
        """
        Retrieve the DirectLine token either by using the bot_secret or by querying the token_endpoint.
        If bot_secret is provided, it posts to "https://directline.botframework.com/v3/directline/tokens/generate".
        """
        await self._ensure_session()
        try:
            if self.bot_secret:
                url = f"{self.bot_endpoint}/tokens/generate"
                headers = {"Authorization": f"Bearer {self.bot_secret}"}
                async with self.session.post(url, headers=headers) as resp:
                    if resp.status == 200:
                        data = await resp.json()
                        self.directline_token = data.get("token")
                        if not self.directline_token:
                            logger.error("Token generation response missing token: %s", data)
                            raise AgentInvokeException("No token received from token generation.")
                    else:
                        logger.error("Token generation endpoint error status: %s", resp.status)
                        raise AgentInvokeException("Failed to generate token using bot_secret.")
            else:
                async with self.session.get(self.token_endpoint) as resp:
                    if resp.status == 200:
                        data = await resp.json()
                        self.directline_token = data.get("token")
                        if not self.directline_token:
                            logger.error("Token endpoint returned no token: %s", data)
                            raise AgentInvokeException("No token received.")
                    else:
                        logger.error("Token endpoint error status: %s", resp.status)
                        raise AgentInvokeException("Failed to fetch token from token endpoint.")
        except Exception as ex:
            logger.exception("Exception fetching token: %s", ex)
            raise AgentInvokeException("Exception occurred while fetching token.") from ex

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Check the logged response body (logger.error prints it) to see what key actually holds the token.
  2. Confirm the bot_endpoint points at the correct DirectLine token-generation URL for your Copilot Studio bot version.
  3. If the key differs, adjust the parsing or ensure the endpoint returns the standard {"token": "..."} shape.

Example fix

// before
data = await resp.json()
self.directline_token = data.get("token")

// after  # inspect and adapt to actual schema
import json
logger.error("Token generation raw body: %s", json.dumps(data))
self.directline_token = data.get("token") or data.get("access_token")
Defensive patterns

Strategy: try-catch

Try / catch

try:
    await agent._ensure_session_and_token()  # or the public token method
except AgentInvokeException as e:
    if "No token received from token generation" in str(e):
        logger.error("Token endpoint 200 but no token field; check response schema.")
    raise

Prevention

When it happens

Trigger: self.bot_secret is set; the token-generate endpoint responds 200 with JSON lacking a 'token' key (e.g. returns {'access_token': ...} or an error object with a 200 status).

Common situations: The bot endpoint returned an unexpected schema (version drift); the secret authenticated but the response wrapper changed; a proxy/gateway rewrote the body; the endpoint returned a success-shaped error envelope.

Related errors


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