Skip to content

The agent engineering platform.

chore(langchain): allow injection of `ToolRuntime` and generic `ToolRuntime[ContextT, StateT]` (#33546)

Sydney Runkle055cccde2889
diff --git a/libs/core/langchain_core/tools/base.py b/libs/core/langchain_core/tools/base.py
index e016344ba9..3849acfefa 100644
--- a/libs/core/langchain_core/tools/base.py
+++ b/libs/core/langchain_core/tools/base.py
@@ -1210,6 +1210,26 @@ class InjectedToolArg:
"""
+class _DirectlyInjectedToolArg:
+ """Annotation for tool arguments that are injected at runtime.
+
+ Injected via direct type annotation, rather than annotated metadata.
+
+ For example, ToolRuntime is a directly injected argument.
+ Note the direct annotation rather than the verbose alternative:
+ Annotated[ToolRuntime, InjectedRuntime]
+ ```python
+ from langchain_core.tools import tool, ToolRuntime
+
+
+ @tool
+ def foo(x: int, runtime: ToolRuntime) -> str:
+ # use runtime.state, runtime.context, runtime.store, etc.
+ ...
+ ```
+ """
+
+
class InjectedToolCallId(InjectedToolArg):
"""Annotation for injecting the tool call ID.
@@ -1237,6 +1257,24 @@ class InjectedToolCallId(InjectedToolArg):
"""
+def _is_directly_injected_arg_type(type_: Any) -> bool:
+ """Check if a type annotation indicates a directly injected argument.
+
+ This is currently only used for ToolRuntime.
+ Checks if either the annotation itself is a subclass of _DirectlyInjectedToolArg
+ or the origin of the annotation is a subclass of _DirectlyInjectedToolArg.
+
+ Ex: ToolRuntime or ToolRuntime[ContextT, StateT] would both return True.
+ """
+ return (
+ isinstance(type_, type) and issubclass(type_, _DirectlyInjectedToolArg)
+ ) or (
+ (origin := get_origin(type_)) is not None
+ and isinstance(origin, type)
+ and issubclass(origin, _DirectlyInjectedToolArg)
+ )
+
+
def _is_injected_arg_type(
type_: type | TypeVar, injected_type: type[InjectedToolArg] | None = None
) -> bool:
@@ -1249,7 +1287,15 @@ def _is_injected_arg_type(
Returns:
`True` if the type is an injected argument, `False` otherwise.
"""
- injected_type = injected_type or InjectedToolArg
+ if injected_type is None:
+ # if no injected type is specified,
+ # check if the type is a directly injected argument
+ if _is_directly_injected_arg_type(type_):
+ return True
+ injected_type = InjectedToolArg
+
+ # if the type is an Annotated type, check if annotated metadata
+ # is an intance or subclass of the injected type
return any(
isinstance(arg, injected_type)
or (isinstance(arg, type) and issubclass(arg, injected_type))
diff --git a/libs/langchain_v1/langchain/tools/tool_node.py b/libs/langchain_v1/langchain/tools/tool_node.py
index 636dce084e..bd17127438 100644
--- a/libs/langchain_v1/langchain/tools/tool_node.py
+++ b/libs/langchain_v1/langchain/tools/tool_node.py
@@ -75,6 +75,7 @@ from langchain_core.tools import tool as create_tool
from langchain_core.tools.base import (
TOOL_MESSAGE_BLOCK_TYPES,
ToolException,
+ _DirectlyInjectedToolArg,
get_all_basemodel_annotations,
)
from langgraph._internal._runnable import RunnableCallable
@@ -1349,7 +1350,7 @@ def tools_condition(
@dataclass
-class ToolRuntime(InjectedToolArg, Generic[ContextT, StateT]):
+class ToolRuntime(_DirectlyInjectedToolArg, Generic[ContextT, StateT]):
"""Runtime context automatically injected into tools.
When a tool function has a parameter named 'tool_runtime' with type hint