Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 28 additions & 34 deletions libs/prebuilt/langgraph/prebuilt/tool_node.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""Tool execution node for LangGraph workflows.

This module provides prebuilt functionality for executing tools in LangGraph.
Expand Down Expand Up @@ -249,29 +249,6 @@

Tool calls can also be passed directly as a list of `ToolCall` dicts.

Args:
tools: A sequence of tools that can be invoked by this node. Tools can be
BaseTool instances or plain functions that will be converted to tools.
name: The name identifier for this node in the graph. Used for debugging
and visualization. Defaults to "tools".
tags: Optional metadata tags to associate with the node for filtering
and organization. Defaults to None.
handle_tool_errors: Configuration for error handling during tool execution.
Defaults to True. Supports multiple strategies:

- True: Catch all errors and return a ToolMessage with the default
error template containing the exception details.
- str: Catch all errors and return a ToolMessage with this custom
error message string.
- tuple[type[Exception], ...]: Only catch exceptions of the specified
types and return default error messages for them.
- Callable[..., str]: Catch exceptions matching the callable's signature
and return the string result of calling it with the exception.
- False: Disable error handling entirely, allowing exceptions to propagate.

messages_key: The key in the state dictionary that contains the message list.
This same key will be used for the output ToolMessages. Defaults to "messages".

Example:
Basic usage with simple tools:

Expand Down Expand Up @@ -334,11 +311,26 @@
"""Initialize the ToolNode with the provided tools and configuration.

Args:
tools: Sequence of tools to make available for execution.
name: Node name for graph identification.
tags: Optional metadata tags.
handle_tool_errors: Error handling configuration.
messages_key: State key containing messages.
tools: A sequence of tools that can be invoked by this node. Tools can be
BaseTool instances or plain functions that will be converted to tools.
name: The name identifier for this node in the graph. Used for debugging
and visualization. Defaults to "tools".
tags: Optional metadata tags to associate with the node for filtering
and organization. Defaults to None.
handle_tool_errors: Configuration for error handling during tool execution.
Defaults to True. Supports multiple strategies:

- True: Catch all errors and return a ToolMessage with the default
error template containing the exception details.
- str: Catch all errors and return a ToolMessage with this custom
error message string.
- tuple[type[Exception], ...]: Only catch exceptions of the specified
types and return default error messages for them.
- Callable[..., str]: Catch exceptions matching the callable's signature
and return the string result of calling it with the exception.
- False: Disable error handling entirely, allowing exceptions to propagate.
messages_key: The key in the state dictionary that contains the message list.
This same key will be used for the output ToolMessages. Defaults to "messages".
"""
super().__init__(self._func, self._afunc, name=name, tags=tags, trace=False)
self.tools_by_name: dict[str, BaseTool] = {}
Expand Down Expand Up @@ -867,12 +859,6 @@
receive state data automatically during execution while remaining invisible
to the model's tool-calling interface.

Args:
field: Optional key to extract from the state dictionary. If None, the entire
state is injected. If specified, only that field's value is injected.
This allows tools to request specific state components rather than
processing the full state structure.

Example:
```python
from typing import List
Expand Down Expand Up @@ -930,6 +916,14 @@
""" # noqa: E501

def __init__(self, field: Optional[str] = None) -> None:
"""Initialize InjectedState annotation.

Args:
field: Optional key to extract from the state dictionary. If None, the entire
state is injected. If specified, only that field's value is injected.
This allows tools to request specific state components rather than
processing the full state structure.
"""
self.field = field


Expand Down
26 changes: 14 additions & 12 deletions libs/prebuilt/langgraph/prebuilt/tool_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,6 @@ class ValidationNode(RunnableCallable):
structured output that conforms to a complex schema without losing the original
messages and tool IDs (for use in multi-turn conversations).

Args:
schemas: A list of schemas to validate the tool calls with. These can be
any of the following:
- A pydantic BaseModel class
- A BaseTool instance (the args_schema will be used)
- A function (a schema will be created from the function signature)
format_error: A function that takes an exception, a ToolCall, and a schema
and returns a formatted error string. By default, it returns the
exception repr and a message to respond after fixing validation errors.
name: The name of the node.
tags: A list of tags to add to the node.

Returns:
(Union[Dict[str, List[ToolMessage]], Sequence[ToolMessage]]): A list of ToolMessages with the validated content or error messages.

Expand Down Expand Up @@ -140,6 +128,20 @@ def __init__(
name: str = "validation",
tags: Optional[list[str]] = None,
) -> None:
"""Initialize the ValidationNode.

Args:
schemas: A list of schemas to validate the tool calls with. These can be
any of the following:
- A pydantic BaseModel class
- A BaseTool instance (the args_schema will be used)
- A function (a schema will be created from the function signature)
format_error: A function that takes an exception, a ToolCall, and a schema
and returns a formatted error string. By default, it returns the
exception repr and a message to respond after fixing validation errors.
name: The name of the node.
tags: A list of tags to add to the node.
"""
super().__init__(self._func, None, name=name, tags=tags, trace=False)
self._format_error = format_error or _default_format_error
self.schemas_by_name: Dict[str, Type[BaseModel]] = {}
Expand Down