{"record":{"id":"389ecf799f228a07","repo":"microsoft/semantic-kernel","slug":"no-token-received-from-token-generation","errorCode":null,"errorMessage":"No token received from token generation.","messagePattern":"No token received from token generation\\.","errorType":"exception","errorClass":"AgentInvokeException","httpStatus":null,"severity":"error","filePath":"python/samples/demos/copilot_studio_agent/src/direct_line_agent.py","lineNumber":64,"sourceCode":"            self.session = aiohttp.ClientSession()\n\n    async def _fetch_token_and_conversation(self) -> None:\n        \"\"\"\n        Retrieve the DirectLine token either by using the bot_secret or by querying the token_endpoint.\n        If bot_secret is provided, it posts to \"https://directline.botframework.com/v3/directline/tokens/generate\".\n        \"\"\"\n        await self._ensure_session()\n        try:\n            if self.bot_secret:\n                url = f\"{self.bot_endpoint}/tokens/generate\"\n                headers = {\"Authorization\": f\"Bearer {self.bot_secret}\"}\n                async with self.session.post(url, headers=headers) as resp:\n                    if resp.status == 200:\n                        data = await resp.json()\n                        self.directline_token = data.get(\"token\")\n                        if not self.directline_token:\n                            logger.error(\"Token generation response missing token: %s\", data)\n                            raise AgentInvokeException(\"No token received from token generation.\")\n                    else:\n                        logger.error(\"Token generation endpoint error status: %s\", resp.status)\n                        raise AgentInvokeException(\"Failed to generate token using bot_secret.\")\n            else:\n                async with self.session.get(self.token_endpoint) as resp:\n                    if resp.status == 200:\n                        data = await resp.json()\n                        self.directline_token = data.get(\"token\")\n                        if not self.directline_token:\n                            logger.error(\"Token endpoint returned no token: %s\", data)\n                            raise AgentInvokeException(\"No token received.\")\n                    else:\n                        logger.error(\"Token endpoint error status: %s\", resp.status)\n                        raise AgentInvokeException(\"Failed to fetch token from token endpoint.\")\n        except Exception as ex:\n            logger.exception(\"Exception fetching token: %s\", ex)\n            raise AgentInvokeException(\"Exception occurred while fetching token.\") from ex\n","sourceCodeStart":46,"sourceCodeEnd":82,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/samples/demos/copilot_studio_agent/src/direct_line_agent.py#L46-L82","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Check the logged response body (logger.error prints it) to see what key actually holds the token.","Confirm the bot_endpoint points at the correct DirectLine token-generation URL for your Copilot Studio bot version.","If the key differs, adjust the parsing or ensure the endpoint returns the standard {\"token\": \"...\"} shape."],"exampleFix":"// before\ndata = await resp.json()\nself.directline_token = data.get(\"token\")\n\n// after  # inspect and adapt to actual schema\nimport json\nlogger.error(\"Token generation raw body: %s\", json.dumps(data))\nself.directline_token = data.get(\"token\") or data.get(\"access_token\")","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    await agent._ensure_session_and_token()  # or the public token method\nexcept AgentInvokeException as e:\n    if \"No token received from token generation\" in str(e):\n        logger.error(\"Token endpoint 200 but no token field; check response schema.\")\n    raise","preventionTips":["Log the raw token-endpoint body during development to learn the schema.","Pin to a known-good DirectLine/Copilot version to avoid schema drift."],"tags":["network","authentication","directline","copilot-studio","semantic-kernel"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}