microsoft/autogen · error · ValueError
Assistant not initialized
Error message
Assistant not initialized
What it means
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.
Source
Thrown at python/packages/autogen-ext/src/autogen_ext/agents/openai/_openai_assistant_agent.py:375
"""The types of messages that the assistant agent produces."""
return (TextMessage,)
@property
def threads(self) -> AsyncThreads:
return self._client.beta.threads
@property
def runs(self) -> AsyncRuns:
return self._client.beta.threads.runs
@property
def messages(self) -> AsyncMessages:
return self._client.beta.threads.messages
@property
def _get_assistant_id(self) -> str:
if self._assistant is None:
raise ValueError("Assistant not initialized")
return self._assistant.id
@property
def _thread_id(self) -> str:
if self._thread is None:
raise ValueError("Thread not initialized")
return self._thread.id
async def _execute_tool_call(self, tool_call: FunctionCall, cancellation_token: CancellationToken) -> str:
"""Execute a tool call and return the result."""
if not self._original_tools:
raise ValueError("No tools are available.")
tool = next((t for t in self._original_tools if t.name == tool_call.name), None)
if tool is None:
raise ValueError(f"The tool '{tool_call.name}' is not available.")
arguments = json.loads(tool_call.arguments)
result = await tool.run_json(arguments, cancellation_token, call_id=tool_call.id)
return tool.return_value_as_string(result)View on GitHub (pinned to 027ecf0a37)
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.
Example fix
# before
agent = OpenAIAssistantAgent(name="a", instructions="...", model="gpt-4o",
client=cl, assistant_id="asst_123")
assistant_id = agent._get_assistant_id # ValueError: Assistant not initialized
# after
resp = await agent.on_messages(
[TextMessage(source="user", content="hi")], CancellationToken()
) # lazy init completed inside on_messages
assistant_id = agent._get_assistant_id Defensive patterns
Strategy: validation
Validate before calling
resp = await agent.on_messages([TextMessage(source="user", content="init")], ct) # _assistant is now set; assistant-dependent members are safe to use
Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- No response was generated
- Incorrect client passed to OpenAIAssistantAgent. Please use
- Unsupported tool type: {type(tool)}
- No tools are available.
- The tool '{tool_call.name}' is not available.
AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15).
Data as JSON: /api/errors/17eaa5e10a7bd197.
Report an issue: GitHub.