Skip to content
Merged
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
6 changes: 6 additions & 0 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 @@ -1761,6 +1761,12 @@
origin_ = get_origin(type_arg)
if origin_ is Union or origin_ is Annotated:
return any(_is_injection(ta, injection_type) for ta in get_args(type_arg))

if origin_ is not None and (
origin_ is injection_type
or (isinstance(origin_, type) and issubclass(origin_, injection_type))
):
return True
return False


Expand Down
42 changes: 42 additions & 0 deletions libs/prebuilt/tests/test_tool_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -1860,3 +1860,45 @@ async def comprehensive_async_tool(
"foo_from_runtime=foo_value, "
"tool_call_id=test_call_789"
)


async def test_tool_node_tool_runtime_generic() -> None:
"""Test that ToolRuntime with generic type arguments is correctly injected."""

@dataclasses.dataclass
class MyContext:
some_info: str

@dec_tool
def get_info(rt: ToolRuntime[MyContext]):
"""This tool returns info from context."""
return rt.context.some_info

# Create a mock runtime with context
mock_runtime = _create_mock_runtime()
mock_runtime.context = MyContext(some_info="test_info")

config = {"configurable": {"__pregel_runtime": mock_runtime}}

result = await ToolNode([get_info]).ainvoke(
{
"messages": [
AIMessage(
"call tool",
tool_calls=[
{
"name": "get_info",
"args": {},
"id": "call_1",
}
],
)
]
},
config=config,
)

tool_message = result["messages"][-1]
assert tool_message.type == "tool"
assert tool_message.content == "test_info"
assert tool_message.tool_call_id == "call_1"