Skip to content

The agent engineering platform.

fix: don't trace injected args only found in signature (#34670)

Sydney Runkleed2aa9f74761
diff --git a/libs/core/langchain_core/tools/base.py b/libs/core/langchain_core/tools/base.py
index 983d5ab5a6..aa85afc0e6 100644
--- a/libs/core/langchain_core/tools/base.py
+++ b/libs/core/langchain_core/tools/base.py
@@ -805,6 +805,9 @@ class ChildTool(BaseTool):
# Start with filtered args from the constant
filtered_keys = set[str](FILTERED_ARGS)
+ # Add injected args from function signature (e.g., ToolRuntime parameters)
+ filtered_keys.update(self._injected_args_keys)
+
# If we have an args_schema, use it to identify injected args
if self.args_schema is not None:
try:
diff --git a/libs/core/tests/unit_tests/test_tools.py b/libs/core/tests/unit_tests/test_tools.py
index aca67d3eb9..649923296d 100644
--- a/libs/core/tests/unit_tests/test_tools.py
+++ b/libs/core/tests/unit_tests/test_tools.py
@@ -3184,6 +3184,112 @@ def test_filter_tool_runtime_directly_injected_arg() -> None:
assert "runtime" not in captured
+# Custom directly injected arg type (similar to ToolRuntime)
+class _CustomRuntime(_DirectlyInjectedToolArg):
+ """Custom runtime info injected at tool call time."""
+
+ def __init__(self, data: dict[str, Any]) -> None:
+ self.data = data
+
+
+# Schema that does NOT include the injected arg
+class _ToolArgsSchemaNoRuntime(BaseModel):
+ """Schema with only the non-injected args."""
+
+ query: str
+ limit: int
+
+
+def _tool_func_directly_injected(
+ query: str, limit: int, runtime: _CustomRuntime
+) -> str:
+ """Tool with directly injected runtime not in schema.
+
+ Args:
+ query: The search query.
+ limit: Max results.
+ runtime: Custom runtime (directly injected, not in schema).
+ """
+ return f"Query: {query}, Limit: {limit}"
+
+
+def _tool_func_annotated_injected(
+ query: str, limit: int, runtime: Annotated[Any, InjectedToolArg()]
+) -> str:
+ """Tool with Annotated injected runtime not in schema.
+
+ Args:
+ query: The search query.
+ limit: Max results.
+ runtime: Custom runtime (annotated as injected, not in schema).
+ """
+ return f"Query: {query}, Limit: {limit}"
+
+
+ ("tool_func", "runtime_value", "description"),
+ [
+ pytest.param(
+ _tool_func_directly_injected,
+ _CustomRuntime(data={"foo": "bar"}),
+ "directly injected (_DirectlyInjectedToolArg subclass)",
+ id="directly_injected",
+ ),
+ pytest.param(
+ _tool_func_annotated_injected,
+ {"foo": "bar"},
+ "annotated injected (Annotated[Any, InjectedToolArg()])",
+ id="annotated_injected",
+ ),
+ ],
+)
+def test_filter_injected_args_not_in_schema(
+ tool_func: Callable[..., str], runtime_value: Any, description: str
+) -> None:
+ """Test filtering injected args that are in function signature but not in schema.
+
+ This tests the case where an injected argument (like ToolRuntime) is in the
+ function signature but is not present in the args_schema. The fix ensures
+ we check _injected_args_keys from the function signature, not just the schema.
+
+ Args:
+ tool_func: The tool function with an injected arg.
+ runtime_value: The value to pass for the runtime arg.
+ description: Description of the injection style being tested.
+ """
+ # Create StructuredTool with explicit args_schema that excludes runtime
+ custom_tool = StructuredTool.from_function(
+ func=tool_func,
+ name="custom_tool",
+ description=f"Tool with {description} arg not in schema",
+ args_schema=_ToolArgsSchemaNoRuntime,
+ )
+
+ # Verify _injected_args_keys contains 'runtime'
+ assert "runtime" in custom_tool._injected_args_keys
+
+ handler = CallbackHandlerWithInputCapture(captured_inputs=[])
+
+ result = custom_tool.invoke(
+ {
+ "query": "test",
+ "limit": 5,
+ "runtime": runtime_value,
+ },
+ config={"callbacks": [handler]},
+ )
+
+ assert result == "Query: test, Limit: 5"
+ assert handler.tool_starts == 1
+ assert len(handler.captured_inputs) == 1
+
+ # Verify that runtime is filtered out even though it's not in args_schema
+ captured = handler.captured_inputs[0]
+ assert captured is not None
+ assert captured == {"query": "test", "limit": 5}
+ assert "runtime" not in captured
+
+
class CallbackHandlerWithToolCallIdCapture(FakeCallbackHandler):
"""Callback handler that captures `tool_call_id` passed to `on_tool_start`.