Skip to content

30_tom_agent.py example fails: tools_map accessed before agent initialization #1831

@xingyaoww

Description

@xingyaoww

Problem

The example examples/01_standalone_sdk/30_tom_agent.py fails in CI with:

RuntimeError: Agent not initialized; call initialize() before use

Root Cause

Line 73 accesses conversation.agent.tools_map before the agent is initialized:

sleeptime_compute_tool = conversation.agent.tools_map.get("sleeptime_compute")

The tools_map property in agent/base.py (line 505) requires the agent to be initialized first:

@property
def tools_map(self) -> dict[str, ToolDefinition]:
    if not self._initialized:
        raise RuntimeError("Agent not initialized; call initialize() before use")
    return self._tools

The agent is only initialized when conversation.run() is called (via init_state() -> _initialize()), but the example tries to access tools_map before calling run().

Possible Fixes

  1. Move the sleeptime_compute invocation after conversation.run() - This would require restructuring the example flow

  2. Call init_state() manually - Add explicit initialization before accessing tools_map:

    # Manually initialize agent to access tools
    from openhands.sdk.conversation import ConversationState
    state = ConversationState(...)
    conversation.agent.init_state(state, lambda x: None)
    sleeptime_compute_tool = conversation.agent.tools_map.get("sleeptime_compute")
  3. Remove the pre-run tool invocation - Since the code block is meant to be optional (it checks if tool exists), simply remove it and only run sleeptime_compute as part of the conversation

  4. Add a public method to access tools before initialization - Expose a method that returns tool definitions without requiring full initialization

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions