ToolNode
Represents a tool node in a directed graph. This node can be used to execute specific tools or functions within a workflow.
YAML example:
kind: ToolNode
name: get_weather
description:
en: |
This tool retrieves the current weather for a specified location.
ru: |
Эта функция получает текущую погоду для указанного местоположения.
func: lib.tools.get_weather
arguments:
- name: lat
type: float
description:
en: latitude of the location
ru: широта местоположения (latitude)
- name: lon
type: float
description:
en: longitude of the location
ru: долгота местоположения (longitude)
# Optionally, you can specify example triggers for the tool.
triggers:
en:
- What's the weather in New York?
ru:
- Какая погода в Нью-Йорке?
# Optionally, you can specify a template for the tool prompt.
# It will allow to improve tool execution accuracy.
# supported only \{name\}, \{description\} and \{triggers\} variables.
tool_prompt_template:
en: |
\{name\} - \{description\}
Examples:
\{triggers\}
ru: |
\{name\} - \{description\}
Примеры:
\{triggers\}
Usage:
kind: LLMNode
---
tools:
- get_weather
- another_tool
Attributes
attribute
spec_type= ToolNodeSpec
attribute
state_type= ToolNodeState
attribute
registry= registry
attribute
funcCallable[..., Any] | None
= None
Functions
func
__init____init__(self, /, spec, registry, *, initial_data=None, yaml_path=None, strict=False, default_lang='en', fallback_lang='en') -> None
Initialize tool node with specification and registry.
Args: spec: Tool node specification defining function and arguments registry: Component registry for dependency resolution initial_data: Optional initial data for the component yaml_path: Optional path to the YAML file this node was loaded from strict: Whether to enforce strict validation and imports default_lang: Default language code for description selection fallback_lang: Fallback language code when default is unavailable
param
selfparam
specToolNodeSpec
param
registryRegistry
param
initial_datadict[str, Any] | None
= None
param
yaml_pathstr | None
= None
param
strictbool
= False
param
default_langstr
= 'en'
param
fallback_langstr
= 'en'
Returns
None
func
compilecompile(self) -> None
Compile the tool node by loading the target function.
Loads the function specified in spec.func and prepares the node for execution. Must be called before invoke().
param
selfReturns
None
func
set_funcset_func(self, /, func) -> None
Manually set the function for this tool node.
Args: func: Callable function that matches the tool specification signature
param
selfparam
funcCallable[..., Any]
Returns
None
func
invokeinvoke(self, /, tool_call, execution_context=None) -> ToolMessage
Execute the tool function with provided arguments.
Calls the underlying function with extracted arguments and returns the result wrapped in a ToolMessage. Handles both sync and async functions.
Args: tool_call: Tool call containing name, arguments, and call ID execution_context: Optional execution context for dependency injection
Returns: ToolMessage containing function result and call metadata
Raises: ValueError: If no function is set or required parameters are missing
param
selfparam
tool_callToolCall
param
execution_contextExecutionContext[ToolNodeState] | None
= None
Returns
ToolMessage
func
_extract_function_args_extract_function_args(self, /, args_dict, execution_context=None) -> dict[str, Any]
Extract function arguments based on function signature from provided args dict. Automatically inject Liman dependency if function parameter is typed as Liman.
Args: args_dict: Dictionary containing all available arguments execution_context: Optional ExecutionContext
Returns: Dictionary with only the arguments that match function signature
param
selfparam
args_dictdict[str, Any]
param
execution_contextExecutionContext[ToolNodeState] | None
= None
Returns
dict[str, Any]
func
_load_func_load_func(self) -> Callable[..., Any]
param
selfReturns
Callable[..., Any]
func
get_tool_descriptionget_tool_description(self, /, lang) -> str
Generate tool description for specified language.
Creates a formatted description using the tool prompt template, description, and example triggers for the given language.
Args: lang: Language code for description generation
Returns: Formatted tool description string
param
selfparam
langLanguageCode
Returns
str
func
get_json_schemaget_json_schema(self, /, lang=None) -> dict[str, Any]
Generate JSON Schema representation for LLM function calling.
Creates OpenAI-compatible function schema including name, description, and parameter definitions for the specified language.
Args: lang: Language code for schema generation (uses default_lang if None)
Returns: JSON Schema dict compatible with LLM function calling
Raises: InvalidSpecError: If description is missing or invalid
param
selfparam
langLanguageCode | None
= None
Returns
dict[str, Any]
func
get_new_stateget_new_state(self) -> ToolNodeState
Create new state instance for this tool node.
Returns: Fresh ToolNodeState for execution
param
selfReturns
ToolNodeState
func
_get_tool_prompt_template_get_tool_prompt_template(self, /, lang) -> str
Get the tool prompt template from the declaration or use the default template.
param
selfparam
langLanguageCode
Returns
str
Last updated on