microsoft/semantic-kernel · error · AgentThreadOperationException
Failed to retrieve messages for thread `{thread_id}`.
Error message
Failed to retrieve messages for thread `{thread_id}`. What it means
Thrown by AgentThreadActions.get_messages when listing messages from the Azure AI client raises any exception; the broad except wraps it into AgentThreadOperationException and chains the cause via `from e`. The original exception is logged at error level before re-raising, so the server/transport error detail is in the logs.
Source
Thrown at python/semantic_kernel/agents/azure_ai/agent_thread_actions.py:905
Yields:
An AsyncIterable of ChatMessageContent that includes the thread messages.
Raises:
AgentThreadOperationException: If the messages cannot be retrieved.
"""
try:
async for message in client.agents.messages.list(
thread_id=thread_id,
run_id=None,
limit=None,
order=sort_order,
before=None,
):
agent_id = (message.agent_id or message.metadata.get("agent_id") or "").strip() or "agent"
yield generate_message_content(agent_id, message)
except Exception as e:
logger.error(f"Failed to retrieve messages for thread {thread_id}: {e}")
raise AgentThreadOperationException(f"Failed to retrieve messages for thread `{thread_id}`.") from e
# endregion
# region Internal Methods
@classmethod
def _merge_options(
cls: type[_T],
*,
agent: "AzureAIAgent",
model: str | None = None,
response_format: ResponseFormatJsonSchemaType | None = None,
temperature: float | None = None,
top_p: float | None = None,
metadata: dict[str, str] | None = None,
**kwargs: Any,
) -> dict[str, Any]:
"""Merge run-time options with the agent-level options.View on GitHub (pinned to c028a0c7dc)
Solutions
- Confirm the thread_id is valid and still exists (it was created and not deleted/expired) before fetching messages.
- Check logs for the chained exception (__cause__) to see the real Azure error (auth, 429, 404) and address it.
- Refresh credentials / retry with backoff for throttling or transient network errors.
- Wrap the call in try/except AgentThreadOperationException and handle gracefully (e.g. return empty history).
Defensive patterns
Strategy: try-catch
Try / catch
from semantic_kernel.exceptions.agent_exceptions import AgentThreadOperationException
try:
messages = [m async for m in AgentThreadActions.get_messages(client, thread_id, ...)]
except AgentThreadOperationException as e:
logger.error('get_messages failed: %r', e.__cause__)
messages = [] Prevention
- Confirm thread_id validity before fetching messages.
- Inspect the chained __cause__ (and logs) for the real Azure error.
- Refresh credentials and apply backoff for throttling.
- Handle missing/expired threads gracefully with empty history.
When it happens
Trigger: Calling get_messages for a thread_id that does not exist, has expired, or when the Azure AI client call fails (auth, network, throttling) during messages.list iteration.
Common situations: Thread id stale/expired on the Azure side; expired auth credentials; network interruption while paginating messages; throttling (429) from the messages endpoint.
Related errors
- Run failed with status: `{run.status}` for agent `{agent.nam
- Run failed with status: `{run_failed.status}` for agent `{ag
- The thread could not be created due to an error response fro
- The thread could not be deleted due to an error response fro
- Astra DB not available. Status : {response}
AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13).
Data as JSON: /api/errors/8f23926e88d046fc.
Report an issue: GitHub.