Skip to content

The agent engineering platform.

fix(langchain): include model destination in agent tool routing (#38355)

Sourav1e1e23870369
diff --git a/libs/langchain_v1/langchain/agents/factory.py b/libs/langchain_v1/langchain/agents/factory.py
index b8712a1d55..c01479ad84 100644
--- a/libs/langchain_v1/langchain/agents/factory.py
+++ b/libs/langchain_v1/langchain/agents/factory.py
@@ -1698,14 +1698,10 @@ def create_agent(
tools_to_model_destinations,
)
- # base destinations are tools and exit_node
- # we add the loop_entry node to edge destinations if:
- # - there is an after model hook(s) -- allows jump_to to model
- # potentially artificially injected tool messages, ex HITL
- # - there is a response format -- to allow for jumping to model to handle
- # regenerating structured output tool calls
+ # Include loop_entry_node when middleware can inject synthetic tool
+ # messages, or when structured output or after-model hooks can reroute there.
model_to_tools_destinations = ["tools", exit_node]
- if response_format or loop_exit_node != "model":
+ if response_format or loop_exit_node != "model" or middleware_w_wrap_model_call:
model_to_tools_destinations.append(loop_entry_node)
graph.add_conditional_edges(
diff --git a/libs/langchain_v1/tests/unit_tests/agents/middleware/core/test_framework.py b/libs/langchain_v1/tests/unit_tests/agents/middleware/core/test_framework.py
index 9e170caf48..8d4c37d9dc 100644
--- a/libs/langchain_v1/tests/unit_tests/agents/middleware/core/test_framework.py
+++ b/libs/langchain_v1/tests/unit_tests/agents/middleware/core/test_framework.py
@@ -9,6 +9,7 @@ from langchain_core.tools import InjectedToolCallId, tool
from langgraph.checkpoint.base import BaseCheckpointSaver
from langgraph.checkpoint.memory import InMemorySaver
from langgraph.runtime import Runtime
+from langgraph.types import Command
from pydantic import BaseModel, Field
from syrupy.assertion import SnapshotAssertion
from typing_extensions import override
@@ -18,6 +19,7 @@ from langchain.agents.middleware import InputAgentState
from langchain.agents.middleware.types import (
AgentMiddleware,
AgentState,
+ ExtendedModelResponse,
ModelCallResult,
ModelRequest,
ModelResponse,
@@ -143,6 +145,54 @@ def test_create_agent_invoke(
]
+def test_create_agent_synthetic_tool_messages_reroute_to_model() -> None:
+ class SyntheticToolMessageMiddleware(AgentMiddleware):
+ def wrap_model_call(
+ self,
+ request: ModelRequest,
+ handler: Callable[[ModelRequest], ModelResponse],
+ ) -> ModelCallResult:
+ response = handler(request)
+ message = response.result[0]
+ if isinstance(message, AIMessage) and message.tool_calls:
+ synthetic_messages = [
+ ToolMessage(
+ content="synthetic result",
+ name=tool_call["name"],
+ tool_call_id=tool_call["id"],
+ )
+ for tool_call in message.tool_calls
+ ]
+ return ExtendedModelResponse(
+ model_response=response,
+ command=Command(update={"messages": synthetic_messages}),
+ )
+ return response
+
+ @tool
+ def my_tool(value: str) -> str:
+ """A great tool."""
+ return value.upper()
+
+ agent = create_agent(
+ model=FakeToolCallingModel(
+ tool_calls=[
+ [ToolCall(id="1", name="my_tool", args={"value": "yo"})],
+ [],
+ ],
+ ),
+ tools=[my_tool],
+ middleware=[SyntheticToolMessageMiddleware()],
+ )
+
+ result = agent.invoke({"messages": [HumanMessage(content="hello")]})
+
+ assert any(
+ isinstance(message, ToolMessage) and message.tool_call_id == "1"
+ for message in result["messages"]
+ )
+
+
def test_create_agent_jump(
snapshot: SnapshotAssertion,
sync_checkpointer: BaseCheckpointSaver[str],
diff --git a/libs/langchain_v1/tests/unit_tests/chat_models/test_chat_models.py b/libs/langchain_v1/tests/unit_tests/chat_models/test_chat_models.py
index 7083681afd..8b13d1340c 100644
--- a/libs/langchain_v1/tests/unit_tests/chat_models/test_chat_models.py
+++ b/libs/langchain_v1/tests/unit_tests/chat_models/test_chat_models.py
@@ -400,7 +400,6 @@ def test_configurable_with_default() -> None:
"container": None,
"anthropic_api_key": SecretStr("bar"),
"betas": None,
- "container": None,
"default_headers": None,
"model_kwargs": {},
"reuse_last_container": None,