{"record":{"id":"17eaa5e10a7bd197","repo":"microsoft/autogen","slug":"assistant-not-initialized","errorCode":null,"errorMessage":"Assistant not initialized","messagePattern":"Assistant not initialized","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/packages/autogen-ext/src/autogen_ext/agents/openai/_openai_assistant_agent.py","lineNumber":375,"sourceCode":"        \"\"\"The types of messages that the assistant agent produces.\"\"\"\n        return (TextMessage,)\n\n    @property\n    def threads(self) -> AsyncThreads:\n        return self._client.beta.threads\n\n    @property\n    def runs(self) -> AsyncRuns:\n        return self._client.beta.threads.runs\n\n    @property\n    def messages(self) -> AsyncMessages:\n        return self._client.beta.threads.messages\n\n    @property\n    def _get_assistant_id(self) -> str:\n        if self._assistant is None:\n            raise ValueError(\"Assistant not initialized\")\n        return self._assistant.id\n\n    @property\n    def _thread_id(self) -> str:\n        if self._thread is None:\n            raise ValueError(\"Thread not initialized\")\n        return self._thread.id\n\n    async def _execute_tool_call(self, tool_call: FunctionCall, cancellation_token: CancellationToken) -> str:\n        \"\"\"Execute a tool call and return the result.\"\"\"\n        if not self._original_tools:\n            raise ValueError(\"No tools are available.\")\n        tool = next((t for t in self._original_tools if t.name == tool_call.name), None)\n        if tool is None:\n            raise ValueError(f\"The tool '{tool_call.name}' is not available.\")\n        arguments = json.loads(tool_call.arguments)\n        result = await tool.run_json(arguments, cancellation_token, call_id=tool_call.id)\n        return tool.return_value_as_string(result)","sourceCodeStart":357,"sourceCodeEnd":393,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/python/packages/autogen-ext/src/autogen_ext/agents/openai/_openai_assistant_agent.py#L357-L393","documentation":"The _get_assistant_id property on OpenAIAssistantAgent throws ValueError until the OpenAI Assistant object has been created/loaded. Initialization is lazy (create_assistant/get_assistant and thread creation happen on first use, e.g. inside on_messages or an explicit ensure/init call). Accessing assistant-dependent members before that completes hits the None guard.","triggerScenarios":"Constructing OpenAIAssistantAgent(..., assistant_id=\"asst_...\") and immediately reading agent._get_assistant_id or otherwise touching _assistant-dependent state before any on_messages/on_messages_stream call; an async init task not awaited; API errors during lazy init leaving _assistant None.","commonSituations":"Inspecting/testing agent internals right after construction; calling helper methods (upload, runs) that assume initialization without triggering it; initialization that failed silently due to a network/API error so later property access fails instead.","solutions":["Trigger lazy initialization before touching assistant state: call await agent.on_messages(...) or the agent's documented init/ensure method (e.g. _ensure_initialized) first.","If on_messages itself raises this, inspect for an earlier swallowed exception during create_assistant (network, invalid API key) and fix the root cause.","Avoid relying on private members (_get_assistant_id); use the public API surface which initializes on demand."],"exampleFix":"# before\nagent = OpenAIAssistantAgent(name=\"a\", instructions=\"...\", model=\"gpt-4o\",\n                            client=cl, assistant_id=\"asst_123\")\nassistant_id = agent._get_assistant_id  # ValueError: Assistant not initialized\n\n# after\nresp = await agent.on_messages(\n    [TextMessage(source=\"user\", content=\"hi\")], CancellationToken()\n)  # lazy init completed inside on_messages\nassistant_id = agent._get_assistant_id","handlingStrategy":"validation","validationCode":"resp = await agent.on_messages([TextMessage(source=\"user\", content=\"init\")], ct)\n# _assistant is now set; assistant-dependent members are safe to use","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Trigger one on_messages call before reading assistant/thread internals.","Prefer public API over private members like _get_assistant_id.","Wrap first-use in try/except to surface init-time API errors early."],"tags":["openai","assistants-api","lifecycle","lazy-init"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}