agents.discovery.dynamic_tool_selector

Dynamic Tool Selector implementing LangGraph-style tool management patterns.

This module implements sophisticated tool selection and management patterns inspired by LangGraph’s many-tools approach, providing dynamic tool binding, context-aware selection, and intelligent tool routing.

Key Features: - Dynamic tool selection and binding like LangGraph - Context-aware tool recommendation - Intelligent tool routing and management - State-aware tool selection - Tool usage learning and optimization

Classes

ContextAwareSelector

Context-aware tool selector that considers conversation history.

ContextAwareState

State information for context-aware tool selection.

DynamicToolSelector

Dynamic tool selector implementing LangGraph-style patterns.

LangGraphStyleSelector

LangGraph-style tool selector with state-based selection.

SelectionMode

Tool selection modes.

ToolBindingStrategy

Strategies for binding tools to LLM.

ToolSelectionResult

Result of tool selection process.

ToolSelectionStrategy

Protocol for tool selection strategies.

ToolUsageStats

Statistics for tool usage and performance.

Functions

create_context_aware_selector([max_tools, min_confidence])

Create a context-aware tool selector.

create_dynamic_tool_selector([selection_mode, ...])

Create a dynamic tool selector with sensible defaults.

create_langgraph_style_selector([max_tools, ...])

Create a LangGraph-style tool selector.

Module Contents

class agents.discovery.dynamic_tool_selector.ContextAwareSelector(**kwargs)

Bases: DynamicToolSelector

Context-aware tool selector that considers conversation history.

Init .

Returns:

Add return description]

Return type:

[TODO

async select_with_conversation_context(query, conversation_history, user_preferences=None)

Select tools considering full conversation context.

Parameters:
  • query (str)

  • conversation_history (list[langchain_core.messages.BaseMessage])

  • user_preferences (dict[str, Any] | None)

Return type:

ToolSelectionResult

class agents.discovery.dynamic_tool_selector.ContextAwareState(/, **data)

Bases: pydantic.BaseModel

State information for context-aware tool selection.

Create a new model by parsing and validating input data from keyword arguments.

Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

Parameters:

data (Any)

class agents.discovery.dynamic_tool_selector.DynamicToolSelector(/, **data)

Bases: haive.core.common.mixins.tool_route_mixin.ToolRouteMixin

Dynamic tool selector implementing LangGraph-style patterns.

This class provides sophisticated tool selection capabilities that adapt to query content, context, and usage patterns, similar to LangGraph’s approach to handling many tools.

Create a new model by parsing and validating input data from keyword arguments.

Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

Parameters:

data (Any)

async analyze_tool_performance()

Analyze tool performance and provide insights.

Return type:

dict[str, Any]

async bind_tools_to_llm(llm_instance, selected_tools, strategy=None)

Bind selected tools to LLM instance using specified strategy.

This implements the LangGraph pattern of dynamically binding tools to the language model based on the current query context.

Parameters:
Return type:

Any

async iterative_tool_refinement(initial_query, llm_response, execution_results, max_iterations=3)

Iteratively refine tool selection based on execution feedback.

This implements an advanced pattern where tool selection is refined based on the results of previous tool executions, similar to LangGraph’s iterative approaches.

Parameters:
  • initial_query (str)

  • llm_response (str)

  • execution_results (dict[str, Any])

  • max_iterations (int)

Return type:

ToolSelectionResult

async select_tools_for_query(query, available_tools=None, context=None, force_refresh=False)

Select optimal tools for a given query using LangGraph-style selection.

This is the main entry point for tool selection, implementing the LangGraph pattern of dynamically selecting relevant tools based on query content and context.

Parameters:
  • query (str)

  • available_tools (list[langchain_core.tools.BaseTool] | None)

  • context (dict[str, Any] | None)

  • force_refresh (bool)

Return type:

ToolSelectionResult

setup_selector()

Setup the tool selector with default components.

Return type:

DynamicToolSelector

class agents.discovery.dynamic_tool_selector.LangGraphStyleSelector(/, **data)

Bases: DynamicToolSelector

LangGraph-style tool selector with state-based selection.

This class specifically implements the LangGraph pattern of using state to determine tool selection and binding.

Create a new model by parsing and validating input data from keyword arguments.

Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

Parameters:

data (Any)

create_tool_selection_node()

Create a node function for LangGraph that selects tools.

This returns a function that can be used as a node in a LangGraph workflow for dynamic tool selection.

Return type:

collections.abc.Callable

async select_tools_with_state(state, available_tools=None)

Select tools based on LangGraph-style state.

This method implements the LangGraph pattern where tool selection is based on the current state of the conversation/workflow.

Parameters:
  • state (dict[str, Any])

  • available_tools (list[langchain_core.tools.BaseTool] | None)

Return type:

ToolSelectionResult

class agents.discovery.dynamic_tool_selector.SelectionMode

Bases: str, enum.Enum

Tool selection modes.

Initialize self. See help(type(self)) for accurate signature.

class agents.discovery.dynamic_tool_selector.ToolBindingStrategy

Bases: str, enum.Enum

Strategies for binding tools to LLM.

Initialize self. See help(type(self)) for accurate signature.

class agents.discovery.dynamic_tool_selector.ToolSelectionResult(/, **data)

Bases: pydantic.BaseModel

Result of tool selection process.

Create a new model by parsing and validating input data from keyword arguments.

Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

Parameters:

data (Any)

class agents.discovery.dynamic_tool_selector.ToolSelectionStrategy

Bases: Protocol

Protocol for tool selection strategies.

async select_tools(query, available_tools, context, max_tools=5)

Select tools based on strategy.

Parameters:
Return type:

ToolSelectionResult

class agents.discovery.dynamic_tool_selector.ToolUsageStats(/, **data)

Bases: pydantic.BaseModel

Statistics for tool usage and performance.

Create a new model by parsing and validating input data from keyword arguments.

Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

Parameters:

data (Any)

agents.discovery.dynamic_tool_selector.create_context_aware_selector(max_tools=5, min_confidence=0.7)

Create a context-aware tool selector.

Parameters:
  • max_tools (int)

  • min_confidence (float)

Return type:

ContextAwareSelector

agents.discovery.dynamic_tool_selector.create_dynamic_tool_selector(selection_mode=SelectionMode.DYNAMIC, max_tools=5, semantic_discovery=None)

Create a dynamic tool selector with sensible defaults.

Parameters:
Return type:

DynamicToolSelector

agents.discovery.dynamic_tool_selector.create_langgraph_style_selector(max_tools=5, learning_enabled=True)

Create a LangGraph-style tool selector.

Parameters:
  • max_tools (int)

  • learning_enabled (bool)

Return type:

LangGraphStyleSelector