The agent engineering platform.
Python agentsaiai-agentsanthropicchatgptdeepagentsenterpriseframeworkgeminigenerative-ailangchainlanggraphllmmultiagentopen-sourceopenaipydanticpythonragtypescript
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.pyindex 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.pyindex 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, toolfrom langgraph.checkpoint.base import BaseCheckpointSaverfrom langgraph.checkpoint.memory import InMemorySaverfrom langgraph.runtime import Runtime+from langgraph.types import Commandfrom pydantic import BaseModel, Fieldfrom syrupy.assertion import SnapshotAssertionfrom typing_extensions import override@@ -18,6 +19,7 @@ from langchain.agents.middleware import InputAgentStatefrom 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.pyindex 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,