Skip to content

The agent engineering platform.

fix(core): respect include_injected=False with filter_args (#39200)

ccurme576b6fa06007
diff --git a/libs/core/langchain_core/tools/base.py b/libs/core/langchain_core/tools/base.py
index 2bf1f13187..614c3d6eaf 100644
--- a/libs/core/langchain_core/tools/base.py
+++ b/libs/core/langchain_core/tools/base.py
@@ -319,7 +319,8 @@ def create_schema_from_function(
inferred_model = validated.model
if filter_args:
- filter_args_ = filter_args
+ # Copy to avoid mutating the caller's sequence below.
+ filter_args_ = list(filter_args)
else:
# Handle classmethods and instance methods
existing_params: list[str] = list(sig.parameters.keys())
@@ -328,9 +329,15 @@ def create_schema_from_function(
else:
filter_args_ = list(FILTERED_ARGS)
- for existing_param in existing_params:
- if not include_injected and _is_injected_arg_type(
- sig.parameters[existing_param].annotation
+ # Exclude injected args from the schema regardless of whether `filter_args`
+ # was provided. Injected args (e.g. `InjectedToolArg`, `ToolRuntime`) are
+ # supplied at runtime rather than by the model, so they should not appear in
+ # the generated schema.
+ if not include_injected:
+ for existing_param in sig.parameters:
+ if (
+ _is_injected_arg_type(sig.parameters[existing_param].annotation)
+ and existing_param not in filter_args_
):
filter_args_.append(existing_param)
diff --git a/libs/core/tests/unit_tests/test_tools.py b/libs/core/tests/unit_tests/test_tools.py
index 9a881e3cf4..297e73c11e 100644
--- a/libs/core/tests/unit_tests/test_tools.py
+++ b/libs/core/tests/unit_tests/test_tools.py
@@ -64,6 +64,7 @@ from langchain_core.tools.base import (
_format_output,
_is_message_content_block,
_normalize_message_content,
+ create_schema_from_function,
get_all_basemodel_annotations,
)
from langchain_core.utils.function_calling import (
@@ -2645,6 +2646,48 @@ def test_injected_arg_with_complex_type() -> None:
assert injected_tool.invoke({"x": 5, "foo": Foo()}) == "bar"
+def test_create_schema_from_function_include_injected_with_filter_args() -> None:
+ """`include_injected=False` must be honored even when `filter_args` is passed.
+
+ Regression test for https://github.com/langchain-ai/langchain/issues/35831:
+ the injected-arg filtering was nested inside the `else` branch of the
+ `filter_args` check, so injected args ended up in the generated schema when a
+ caller also supplied `filter_args`.
+ """
+
+ def my_tool(
+ query: str,
+ injected: Annotated[str, InjectedToolArg],
+ runtime: _DirectlyInjectedToolArg,
+ ) -> None:
+ """Tool with an annotated and a directly injected arg."""
+
+ # filter_args is provided *and* include_injected is False.
+ schema = create_schema_from_function(
+ "MyTool",
+ my_tool,
+ filter_args=["query"],
+ include_injected=False,
+ )
+ properties = model_json_schema(schema)["properties"]
+ assert "injected" not in properties
+ assert "runtime" not in properties
+ assert properties == {}
+
+
+def test_create_schema_from_function_does_not_mutate_filter_args() -> None:
+ """`filter_args` passed by the caller must not be mutated. See #35831."""
+
+ def my_tool(query: str, injected: Annotated[str, InjectedToolArg]) -> None:
+ """Tool with an injected arg."""
+
+ filter_args = ["query"]
+ create_schema_from_function(
+ "MyTool", my_tool, filter_args=filter_args, include_injected=False
+ )
+ assert filter_args == ["query"]
+
+
@pytest.mark.parametrize("schema_format", ["model", "json_schema"])
def test_tool_allows_extra_runtime_args_with_custom_schema(
schema_format: Literal["model", "json_schema"],