Skip to content

Commit 54ea620

Browse files
authored
chore(langchain_v1): move tool node to tools namespace (#33132)
* Move ToolNode to tools namespace * Expose injected variable as well in tools namespace * Update doc-strings throughout
1 parent 9863023 commit 54ea620

File tree

10 files changed

+38
-18
lines changed

10 files changed

+38
-18
lines changed

libs/langchain_v1/langchain/agents/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""langgraph.prebuilt exposes a higher-level API for creating and executing agents and tools."""
22

33
from langchain.agents.react_agent import AgentState, create_agent
4-
from langchain.agents.tool_node import ToolNode
4+
from langchain.tools import ToolNode
55

66
__all__ = [
77
"AgentState",

libs/langchain_v1/langchain/agents/middleware_agent.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
StructuredOutputValidationError,
3434
ToolStrategy,
3535
)
36-
from langchain.agents.tool_node import ToolNode
3736
from langchain.chat_models import init_chat_model
37+
from langchain.tools import ToolNode
3838

3939
STRUCTURED_OUTPUT_ERROR_TEMPLATE = "Error: {error}\n Please fix your mistakes."
4040

libs/langchain_v1/langchain/agents/react_agent.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@
5454
StructuredOutputValidationError,
5555
ToolStrategy,
5656
)
57-
from langchain.agents.tool_node import ToolNode
5857
from langchain.chat_models import init_chat_model
58+
from langchain.tools import ToolNode
5959

6060
if TYPE_CHECKING:
6161
from langchain_core.tools import BaseTool

libs/langchain_v1/langchain/tools/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,19 @@
88
tool,
99
)
1010

11+
from langchain.tools.tool_node import (
12+
InjectedState,
13+
InjectedStore,
14+
ToolNode,
15+
)
16+
1117
__all__ = [
1218
"BaseTool",
19+
"InjectedState",
20+
"InjectedStore",
1321
"InjectedToolArg",
1422
"InjectedToolCallId",
1523
"ToolException",
24+
"ToolNode",
1625
"tool",
1726
]

libs/langchain_v1/langchain/agents/tool_node.py renamed to libs/langchain_v1/langchain/tools/tool_node.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
Typical Usage:
2222
```python
2323
from langchain_core.tools import tool
24-
from langchain.agents import ToolNode
24+
from langchain.tools import ToolNode
2525
2626
2727
@tool
@@ -344,7 +344,7 @@ class ToolNode(RunnableCallable):
344344
Basic usage:
345345
346346
```python
347-
from langchain.agents import ToolNode
347+
from langchain.tools import ToolNode
348348
from langchain_core.tools import tool
349349
350350
@tool
@@ -359,7 +359,7 @@ def calculator(a: int, b: int) -> int:
359359
360360
```python
361361
from typing_extensions import Annotated
362-
from langgraph.agents.tool_node import InjectedState
362+
from langchain.tools import InjectedState
363363
364364
@tool
365365
def context_tool(query: str, state: Annotated[dict, InjectedState]) -> str:
@@ -885,7 +885,8 @@ def tools_condition(
885885
886886
```python
887887
from langgraph.graph import StateGraph
888-
from langgraph.agents.tool_node import ToolNode, tools_condition
888+
from langchain.tools import ToolNode
889+
from langchain.tools.tool_node import tools_condition
889890
from typing_extensions import TypedDict
890891
891892
@@ -950,9 +951,7 @@ class InjectedState(InjectedToolArg):
950951
from typing_extensions import Annotated, TypedDict
951952
952953
from langchain_core.messages import BaseMessage, AIMessage
953-
from langchain_core.tools import tool
954-
955-
from langgraph.agents.tool_node import InjectedState, ToolNode
954+
from langchain.tools import InjectedState, ToolNode, tool
956955
957956
958957
class AgentState(TypedDict):
@@ -1026,9 +1025,8 @@ class InjectedStore(InjectedToolArg):
10261025
Example:
10271026
```python
10281027
from typing_extensions import Annotated
1029-
from langchain_core.tools import tool
10301028
from langgraph.store.memory import InMemoryStore
1031-
from langgraph.agents.tool_node import InjectedStore, ToolNode
1029+
from langchain.tools import InjectedStore, ToolNode, tool
10321030
10331031
@tool
10341032
def save_preference(

libs/langchain_v1/pyproject.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,16 @@ ignore-var-parameters = true # ignore missing documentation for *args and **kwa
171171
"N801", # class name should use CapWords convention
172172
]
173173

174+
"langchain/tools/tool_node.py" = [
175+
"ANN401", # we use Any right now, need to narrow
176+
"A002", # input is shadowing builtin
177+
"A001", # input is shadowing builtin
178+
"B904", # use from for exceptions
179+
"PLR2004", # magic values are fine for this case
180+
"C901", # too complex
181+
"TRY004", # type error exception
182+
]
183+
174184
[tool.coverage.run]
175185
omit = ["tests/*"]
176186

libs/langchain_v1/tests/unit_tests/agents/test_middleware_agent.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from langchain_core.tools import tool, InjectedToolCallId
2222

2323
from langchain.agents.middleware_agent import create_agent
24-
from langchain.agents.tool_node import InjectedState
24+
from langchain.tools import InjectedState
2525
from langchain.agents.middleware.human_in_the_loop import (
2626
HumanInTheLoopMiddleware,
2727
ActionRequest,

libs/langchain_v1/tests/unit_tests/agents/test_react_agent.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@
3636
create_agent,
3737
)
3838
from langchain.agents.react_agent import _validate_chat_history
39-
from langchain.agents.tool_node import (
39+
from langchain.tools import (
4040
InjectedState,
4141
InjectedStore,
42+
)
43+
from langchain.tools.tool_node import (
4244
_get_state_args,
4345
_infer_handled_types,
4446
)

libs/langchain_v1/tests/unit_tests/agents/test_tool_node.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,11 @@
3535
from langchain.agents import (
3636
ToolNode,
3737
)
38-
from langchain.agents.tool_node import (
39-
TOOL_CALL_ERROR_TEMPLATE,
38+
from langchain.tools import (
4039
InjectedState,
4140
InjectedStore,
42-
ToolInvocationError,
43-
tools_condition,
4441
)
42+
from langchain.tools.tool_node import TOOL_CALL_ERROR_TEMPLATE, ToolInvocationError, tools_condition
4543

4644
from .messages import _AnyIdHumanMessage, _AnyIdToolMessage
4745
from .model import FakeToolCallingModel

libs/langchain_v1/tests/unit_tests/tools/test_imports.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
EXPECTED_ALL = {
44
"BaseTool",
5+
"InjectedState",
6+
"InjectedStore",
57
"InjectedToolArg",
68
"InjectedToolCallId",
79
"ToolException",
10+
"ToolNode",
811
"tool",
912
}
1013

0 commit comments

Comments
 (0)