Skip to content

The agent engineering platform.

chore(langchain_v1): change modifyModelRequest back to tools (#33270)

Eugene Yurtsev46b87e435cb6
diff --git a/libs/langchain_v1/langchain/agents/middleware/types.py b/libs/langchain_v1/langchain/agents/middleware/types.py
index b3101ddb42..ac028429b6 100644
--- a/libs/langchain_v1/langchain/agents/middleware/types.py
+++ b/libs/langchain_v1/langchain/agents/middleware/types.py
@@ -63,7 +63,7 @@ class ModelRequest:
system_prompt: str | None
messages: list[AnyMessage] # excluding system prompt
tool_choice: Any | None
- tools: list[str]
+ tools: list[BaseTool]
response_format: ResponseFormat | None
model_settings: dict[str, Any] = field(default_factory=dict)
diff --git a/libs/langchain_v1/langchain/agents/middleware_agent.py b/libs/langchain_v1/langchain/agents/middleware_agent.py
index ac2a0042a8..fb14560850 100644
--- a/libs/langchain_v1/langchain/agents/middleware_agent.py
+++ b/libs/langchain_v1/langchain/agents/middleware_agent.py
@@ -395,11 +395,11 @@ def create_agent( # noqa: PLR0915
"""
# Validate requested tools are available
tools_by_name = {t.name: t for t in default_tools}
- unknown_tools = [name for name in request.tools if name not in tools_by_name]
- if unknown_tools:
+ unknown_tool_names = [t.name for t in request.tools if t.name not in tools_by_name]
+ if unknown_tool_names:
available_tools = sorted(tools_by_name.keys())
msg = (
- f"Middleware returned unknown tool names: {unknown_tools}\n\n"
+ f"Middleware returned unknown tool names: {unknown_tool_names}\n\n"
f"Available tools: {available_tools}\n\n"
"To fix this issue:\n"
"1. Ensure the tools are passed to create_agent() via "
@@ -411,8 +411,6 @@ def create_agent( # noqa: PLR0915
)
raise ValueError(msg)
- requested_tools = [tools_by_name[name] for name in request.tools]
-
# Determine effective response format (auto-detect if needed)
effective_response_format: ResponseFormat | None = request.response_format
if (
@@ -432,7 +430,7 @@ def create_agent( # noqa: PLR0915
kwargs = effective_response_format.to_model_kwargs()
return (
request.model.bind_tools(
- requested_tools, strict=True, **kwargs, **request.model_settings
+ request.tools, strict=True, **kwargs, **request.model_settings
),
effective_response_format,
)
@@ -442,16 +440,16 @@ def create_agent( # noqa: PLR0915
tool_choice = "any" if structured_output_tools else request.tool_choice
return (
request.model.bind_tools(
- requested_tools, tool_choice=tool_choice, **request.model_settings
+ request.tools, tool_choice=tool_choice, **request.model_settings
),
effective_response_format,
)
# No structured output - standard model binding
- if requested_tools:
+ if request.tools:
return (
request.model.bind_tools(
- requested_tools, tool_choice=request.tool_choice, **request.model_settings
+ request.tools, tool_choice=request.tool_choice, **request.model_settings
),
None,
)
@@ -461,7 +459,7 @@ def create_agent( # noqa: PLR0915
"""Sync model request handler with sequential middleware processing."""
request = ModelRequest(
model=model,
- tools=[t.name for t in default_tools],
+ tools=default_tools,
system_prompt=system_prompt,
response_format=initial_response_format,
messages=state["messages"],
@@ -498,7 +496,7 @@ def create_agent( # noqa: PLR0915
"""Async model request handler with sequential middleware processing."""
request = ModelRequest(
model=model,
- tools=[t.name for t in default_tools],
+ tools=default_tools,
system_prompt=system_prompt,
response_format=initial_response_format,
messages=state["messages"],
diff --git a/libs/langchain_v1/tests/unit_tests/agents/test_middleware_tools.py b/libs/langchain_v1/tests/unit_tests/agents/test_middleware_tools.py
index e22a9ad222..9f65134d30 100644
--- a/libs/langchain_v1/tests/unit_tests/agents/test_middleware_tools.py
+++ b/libs/langchain_v1/tests/unit_tests/agents/test_middleware_tools.py
@@ -10,8 +10,8 @@ from .model import FakeToolCallingModel
from langgraph.runtime import Runtime
-def test_model_request_tools_are_strings() -> None:
- """Test that ModelRequest.tools contains tool names as strings, not tool objects."""
+def test_model_request_tools_are_base_tools() -> None:
+ """Test that ModelRequest.tools contains BaseTool objects."""
captured_requests: list[ModelRequest] = []
@tool
@@ -43,16 +43,15 @@ def test_model_request_tools_are_strings() -> None:
# Verify that at least one request was captured
assert len(captured_requests) > 0
- # Check that tools in the request are strings (tool names)
+ # Check that tools in the request are BaseTool objects
request = captured_requests[0]
assert isinstance(request.tools, list)
assert len(request.tools) == 2
- assert all(isinstance(tool_name, str) for tool_name in request.tools)
- assert set(request.tools) == {"search_tool", "calculator"}
+ assert {t.name for t in request.tools} == {"search_tool", "calculator"}
-def test_middleware_can_modify_tool_names() -> None:
- """Test that middleware can modify the list of tool names in ModelRequest."""
+def test_middleware_can_modify_tools() -> None:
+ """Test that middleware can modify the list of tools in ModelRequest."""
@tool
def tool_a(input: str) -> str:
@@ -74,7 +73,7 @@ def test_middleware_can_modify_tool_names() -> None:
self, request: ModelRequest, state: AgentState, runtime: Runtime
) -> ModelRequest:
# Only allow tool_a and tool_b
- request.tools = ["tool_a", "tool_b"]
+ request.tools = [t for t in request.tools if t.name in ["tool_a", "tool_b"]]
return request
# Model will try to call tool_a
@@ -98,20 +97,26 @@ def test_middleware_can_modify_tool_names() -> None:
assert tool_messages[0].name == "tool_a"
-def test_unknown_tool_name_raises_error() -> None:
- """Test that using an unknown tool name in ModelRequest raises a clear error."""
+def test_unknown_tool_raises_error() -> None:
+ """Test that using an unknown tool in ModelRequest raises a clear error."""
+ from langchain_core.tools import BaseTool
@tool
def known_tool(input: str) -> str:
"""A known tool."""
return "result"
+ @tool
+ def unknown_tool(input: str) -> str:
+ """An unknown tool not passed to create_agent."""
+ return "unknown"
+
class BadMiddleware(AgentMiddleware):
def modify_model_request(
self, request: ModelRequest, state: AgentState, runtime: Runtime
) -> ModelRequest:
- # Add an unknown tool name
- request.tools = ["known_tool", "unknown_tool"]
+ # Add an unknown tool
+ request.tools = request.tools + [unknown_tool]
return request
agent = create_agent(
@@ -149,7 +154,7 @@ def test_middleware_can_add_and_remove_tools() -> None:
) -> ModelRequest:
# Remove admin_tool if not admin
if not state.get("is_admin", False):
- request.tools = [name for name in request.tools if name != "admin_tool"]
+ request.tools = [t for t in request.tools if t.name != "admin_tool"]
return request
model = FakeToolCallingModel()
@@ -224,20 +229,20 @@ def test_tools_preserved_across_multiple_middleware() -> None:
def modify_model_request(
self, request: ModelRequest, state: AgentState, runtime: Runtime
) -> ModelRequest:
- modification_order.append(request.tools.copy())
+ modification_order.append([t.name for t in request.tools])
# Remove tool_c
- request.tools = [name for name in request.tools if name != "tool_c"]
+ request.tools = [t for t in request.tools if t.name != "tool_c"]
return request
class SecondMiddleware(AgentMiddleware):
def modify_model_request(
self, request: ModelRequest, state: AgentState, runtime: Runtime
) -> ModelRequest:
- modification_order.append(request.tools.copy())
+ modification_order.append([t.name for t in request.tools])
# Should not see tool_c here
- assert "tool_c" not in request.tools
+ assert all(t.name != "tool_c" for t in request.tools)
# Remove tool_b
- request.tools = [name for name in request.tools if name != "tool_b"]
+ request.tools = [t for t in request.tools if t.name != "tool_b"]
return request
agent = create_agent(