microsoft/semantic-kernel · error · AgentInvokeException
Exception occurred while fetching token.
Error message
Exception occurred while fetching token.
What it means
A catch-all AgentInvokeException wrapping any unexpected Exception during the token-fetch flow (both branches). It logs the original via logger.exception and re-raises with 'from ex' to preserve the chain. This handles non-HTTP failures like connection errors, timeouts, JSON decode errors, or DNS failures that escape the status checks.
Source
Thrown at python/samples/demos/copilot_studio_agent/src/direct_line_agent.py:81
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
@trace_agent_get_response
@override
async def get_response(
self,
history: ChatHistory,
arguments: dict[str, Any] | None = None,
**kwargs: Any,
) -> ChatMessageContent:
"""
Get a response from the DirectLine Bot.
"""
responses = []
async for response in self.invoke(history, arguments, **kwargs):
responses.append(response)
if not responses:
raise AgentInvokeException("No response from DirectLine Bot.")View on GitHub (pinned to c028a0c7dc)
Solutions
- Read the chained exception (the `from ex` original) — it carries the real cause (timeout, connection reset, etc.).
- Fix the underlying network/connectivity issue; verify DNS and TLS to the endpoint.
- Add a timeout to the session/requests to fail fast and distinguish timeouts from other errors.
Example fix
// before
except Exception as ex:
logger.exception("Exception fetching token: %s", ex)
raise AgentInvokeException("Exception occurred while fetching token.") from ex
// after # surface the specific cause for diagnosis
except (aiohttp.ClientError, asyncio.TimeoutError) as ex:
logger.exception("Network error fetching token: %s", ex)
raise AgentInvokeException(f"Network error fetching token: {ex}") from ex Defensive patterns
Strategy: try-catch
Try / catch
try:
await fetch_token()
except AgentInvokeException as e:
logger.exception("Token fetch failed: %s", e.__cause__)
raise Prevention
- Always inspect e.__cause__ — the wrapped exception carries the real network/decode failure.
- Set aiohttp timeouts so connection stalls surface as TimeoutError, not hangs.
When it happens
Trigger: Any exception other than the handled status/missing-token cases: aiohttp ClientError, asyncio.TimeoutError, JSONDecodeError on a non-JSON 200 body, DNS resolution failure, etc.
Common situations: Network unreachable; proxy/firewall blocking the endpoint; response wasn't valid JSON; SSL certificate problem; the aiohttp session was closed.
Related errors
- No token received from token generation.
- Failed to generate token using bot_secret.
- No token received.
- Failed to fetch token from token endpoint.
- Failed to start conversation.
AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13).
Data as JSON: /api/errors/69c9c9fd5cf3c796.
Report an issue: GitHub.