{"record":{"id":"8f23926e88d046fc","repo":"microsoft/semantic-kernel","slug":"failed-to-retrieve-messages-for-thread-thread-id","errorCode":null,"errorMessage":"Failed to retrieve messages for thread `{thread_id}`.","messagePattern":"Failed to retrieve messages for thread `(.+?)`\\.","errorType":"exception","errorClass":"AgentThreadOperationException","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/agents/azure_ai/agent_thread_actions.py","lineNumber":905,"sourceCode":"        Yields:\n            An AsyncIterable of ChatMessageContent that includes the thread messages.\n\n        Raises:\n            AgentThreadOperationException: If the messages cannot be retrieved.\n        \"\"\"\n        try:\n            async for message in client.agents.messages.list(\n                thread_id=thread_id,\n                run_id=None,\n                limit=None,\n                order=sort_order,\n                before=None,\n            ):\n                agent_id = (message.agent_id or message.metadata.get(\"agent_id\") or \"\").strip() or \"agent\"\n                yield generate_message_content(agent_id, message)\n        except Exception as e:\n            logger.error(f\"Failed to retrieve messages for thread {thread_id}: {e}\")\n            raise AgentThreadOperationException(f\"Failed to retrieve messages for thread `{thread_id}`.\") from e\n\n    # endregion\n\n    # region Internal Methods\n\n    @classmethod\n    def _merge_options(\n        cls: type[_T],\n        *,\n        agent: \"AzureAIAgent\",\n        model: str | None = None,\n        response_format: ResponseFormatJsonSchemaType | None = None,\n        temperature: float | None = None,\n        top_p: float | None = None,\n        metadata: dict[str, str] | None = None,\n        **kwargs: Any,\n    ) -> dict[str, Any]:\n        \"\"\"Merge run-time options with the agent-level options.","sourceCodeStart":887,"sourceCodeEnd":923,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/agents/azure_ai/agent_thread_actions.py#L887-L923","documentation":"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.","triggerScenarios":"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.","commonSituations":"Thread id stale/expired on the Azure side; expired auth credentials; network interruption while paginating messages; throttling (429) from the messages endpoint.","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)."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"from semantic_kernel.exceptions.agent_exceptions import AgentThreadOperationException\ntry:\n    messages = [m async for m in AgentThreadActions.get_messages(client, thread_id, ...)]\nexcept AgentThreadOperationException as e:\n    logger.error('get_messages failed: %r', e.__cause__)\n    messages = []","preventionTips":["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."],"tags":["azure-ai","messages","network","auth","retry","semantic-kernel"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}