The agent engineering platform.
Python agentsaiai-agentsanthropicchatgptdeepagentsenterpriseframeworkgeminigenerative-ailangchainlanggraphllmmultiagentopen-sourceopenaipydanticpythonragtypescript
fix(core): handle injected args for subclasses of BaseTool (#39202)
ccurmed6b0c82fdeb5
diff --git a/libs/core/langchain_core/tools/base.py b/libs/core/langchain_core/tools/base.pyindex 614c3d6eaf..56ea214518 100644--- a/libs/core/langchain_core/tools/base.py+++ b/libs/core/langchain_core/tools/base.py@@ -713,7 +713,28 @@ class ChildTool(BaseTool):@functools.cached_propertydef _injected_args_keys(self) -> frozenset[str]:- # Base implementation doesn't manage injected args+ # Inspect the tool's `_run` (falling back to `_arun` for async-only+ # subclasses) for directly injected args like `ToolRuntime` or args+ # annotated with `InjectedToolArg`. These are supplied at call time+ # rather than by the model, so they must be excluded from the schema and+ # re-injected during execution. `StructuredTool` overrides this to+ # inspect its wrapped `func`/`coroutine` instead.+ #+ # Resolve annotations via `get_type_hints` (rather than reading raw+ # `signature` annotations) so postponed annotations -- e.g. from+ # `from __future__ import annotations` or quoted forward references --+ # are recognized. Fall back to the raw annotation per-parameter when a+ # hint can't be resolved.+ for method in (self._run, self._arun):+ params = signature(method).parameters+ hints = _get_type_hints(method, include_extras=True) or {}+ keys = frozenset(+ name+ for name, param in params.items()+ if _is_injected_arg_type(hints.get(name, param.annotation))+ )+ if keys:+ return keysreturn _EMPTY_SET# --- Runnable ---@@ -1458,11 +1479,15 @@ def _stringify(content: Any) -> str:return str(content)-def _get_type_hints(func: Callable[..., Any]) -> dict[str, type] | None:+def _get_type_hints(+ func: Callable[..., Any], *, include_extras: bool = False+) -> dict[str, type] | None:"""Get type hints from a function, handling partial functions.Args:func: The function to get type hints from.+ include_extras: Whether to preserve `Annotated` metadata in the resolved+ hints (e.g. to detect `Annotated[..., InjectedToolArg]`).Returns:`dict` of type hints, or `None` if extraction fails.@@ -1470,7 +1495,7 @@ def _get_type_hints(func: Callable[..., Any]) -> dict[str, type] | None:if isinstance(func, functools.partial):func = func.functry:- return get_type_hints(func)+ return get_type_hints(func, include_extras=include_extras)except Exception:return Nonediff --git a/libs/core/tests/unit_tests/test_tools.py b/libs/core/tests/unit_tests/test_tools.pyindex 297e73c11e..522500815f 100644--- a/libs/core/tests/unit_tests/test_tools.py+++ b/libs/core/tests/unit_tests/test_tools.py@@ -3592,6 +3592,144 @@ def test_filter_injected_args_not_in_schema(assert "runtime" not in captured+def test_base_tool_subclass_injects_directly_injected_runtime() -> None:+ """Directly injected args on a `BaseTool` subclass `_run` must be injected.++ Regression test for https://github.com/langchain-ai/langchain/issues/34558:+ `BaseTool._injected_args_keys` returned an empty set, so a directly injected+ arg (e.g. `ToolRuntime`) declared on a subclass `_run` was dropped before+ `_run` was called, raising `TypeError: _run() missing 1 required positional+ argument: 'runtime'`.+ """++ class MultiplyInput(BaseModel):+ a: int+ b: int++ captured: dict[str, Any] = {}++ class Multiplier(BaseTool):+ name: str = "Multiplier"+ description: str = "Multiply two numbers."+ args_schema: type[BaseModel] = MultiplyInput++ def _run(self, a: int, b: int, runtime: _CustomRuntime) -> int:+ captured["runtime"] = runtime+ return a * b++ tool_ = Multiplier()++ # The directly injected runtime arg is detected from the `_run` signature...+ assert "runtime" in tool_._injected_args_keys+ # ...and does not leak into the model-facing schema.+ tool_call_schema = tool_.tool_call_schema+ assert not isinstance(tool_call_schema, dict)+ properties = model_json_schema(tool_call_schema)["properties"]+ assert "runtime" not in properties++ runtime = _CustomRuntime(data={"scale": 10})+ result = tool_.invoke({"a": 2, "b": 3, "runtime": runtime})+ assert result == 6+ assert captured["runtime"] is runtime+++async def test_base_tool_subclass_injects_runtime_async_only() -> None:+ """`_injected_args_keys` falls back to `_arun` for async-only subclasses."""++ class MultiplyInput(BaseModel):+ a: int+ b: int++ captured: dict[str, Any] = {}++ class AsyncMultiplier(BaseTool):+ name: str = "AsyncMultiplier"+ description: str = "Multiply two numbers."+ args_schema: type[BaseModel] = MultiplyInput++ def _run(self, *args: Any, **kwargs: Any) -> int:+ raise NotImplementedError++ async def _arun(self, a: int, b: int, runtime: _CustomRuntime) -> int:+ captured["runtime"] = runtime+ return a * b++ tool_ = AsyncMultiplier()+ assert "runtime" in tool_._injected_args_keys++ runtime = _CustomRuntime(data={"scale": 10})+ result = await tool_.ainvoke({"a": 2, "b": 3, "runtime": runtime})+ assert result == 6+ assert captured["runtime"] is runtime+++def test_base_tool_subclass_injects_postponed_annotation_runtime() -> None:+ """Postponed / forward-ref annotations for injected args must be resolved.++ `signature()` exposes raw (string) annotations under+ `from __future__ import annotations` or quoted forward references, which+ `_is_injected_arg_type` cannot classify. `_injected_args_keys` resolves the+ hints first, so injection still works. A quoted forward reference reproduces+ the same string-annotation behavior without a module-wide `__future__`+ import. Follow-up hardening for #34558.+ """++ class MultiplyInput(BaseModel):+ a: int+ b: int++ captured: dict[str, Any] = {}++ class Multiplier(BaseTool):+ name: str = "Multiplier"+ description: str = "Multiply two numbers."+ args_schema: type[BaseModel] = MultiplyInput++ # Quoted forward reference -> raw string annotation "_CustomRuntime".+ def _run(self, a: int, b: int, runtime: "_CustomRuntime") -> int:+ captured["runtime"] = runtime+ return a * b++ tool_ = Multiplier()+ assert "runtime" in tool_._injected_args_keys++ runtime = _CustomRuntime(data={"scale": 10})+ result = tool_.invoke({"a": 2, "b": 3, "runtime": runtime})+ assert result == 6+ assert captured["runtime"] is runtime+++def test_base_tool_subclass_injects_postponed_annotated_arg() -> None:+ """Postponed `Annotated[..., InjectedToolArg]` args must be resolved too.++ `include_extras=True` when resolving hints preserves the `InjectedToolArg`+ metadata, so annotated injected args are still detected. Follow-up hardening+ for #34558.+ """++ class QueryInput(BaseModel):+ query: str++ captured: dict[str, Any] = {}++ class Echo(BaseTool):+ name: str = "Echo"+ description: str = "Echo the query."+ args_schema: type[BaseModel] = QueryInput++ # Quoted forward reference to a postponed `Annotated` injected arg.+ def _run(self, query: str, injected: "Annotated[str, InjectedToolArg]") -> str:+ captured["injected"] = injected+ return query++ tool_ = Echo()+ assert "injected" in tool_._injected_args_keys++ result = tool_.invoke({"query": "hi", "injected": "value"})+ assert result == "hi"+ assert captured["injected"] == "value"++class CallbackHandlerWithToolCallIdCapture(FakeCallbackHandler):"""Callback handler that captures `tool_call_id` passed to `on_tool_start`.