Home / Function/ _make_tools_to_model_edge() — langchain Function Reference

_make_tools_to_model_edge() — langchain Function Reference

Architecture documentation for the _make_tools_to_model_edge() function in factory.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  876bb633_dd26_1b0a_98e8_1ce27a9ed076["_make_tools_to_model_edge()"]
  fd7a28b1_3772_169b_6524_1342f35143b1["factory.py"]
  876bb633_dd26_1b0a_98e8_1ce27a9ed076 -->|defined in| fd7a28b1_3772_169b_6524_1342f35143b1
  f4b66c38_651c_807a_caca_41f73fbbe516["create_agent()"]
  f4b66c38_651c_807a_caca_41f73fbbe516 -->|calls| 876bb633_dd26_1b0a_98e8_1ce27a9ed076
  895a4733_2a72_a5d8_7ab0_f205dfec9cd6["_fetch_last_ai_and_tool_messages()"]
  876bb633_dd26_1b0a_98e8_1ce27a9ed076 -->|calls| 895a4733_2a72_a5d8_7ab0_f205dfec9cd6
  style 876bb633_dd26_1b0a_98e8_1ce27a9ed076 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/langchain_v1/langchain/agents/factory.py lines 1731–1764

def _make_tools_to_model_edge(
    *,
    tool_node: ToolNode,
    model_destination: str,
    structured_output_tools: dict[str, OutputToolBinding[Any]],
    end_destination: str,
) -> Callable[[dict[str, Any]], str | None]:
    def tools_to_model(state: dict[str, Any]) -> str | None:
        last_ai_message, tool_messages = _fetch_last_ai_and_tool_messages(state["messages"])

        # 1. If no AIMessage exists (e.g., messages were cleared), route to model
        if last_ai_message is None:
            return model_destination

        # 2. Exit condition: All executed tools have return_direct=True
        # Filter to only client-side tools (provider tools are not in tool_node)
        client_side_tool_calls = [
            c for c in last_ai_message.tool_calls if c["name"] in tool_node.tools_by_name
        ]
        if client_side_tool_calls and all(
            tool_node.tools_by_name[c["name"]].return_direct for c in client_side_tool_calls
        ):
            return end_destination

        # 3. Exit condition: A structured output tool was executed
        if any(t.name in structured_output_tools for t in tool_messages):
            return end_destination

        # 4. Default: Continue the loop
        #    Tool execution completed successfully, route back to the model
        #    so it can process the tool results and decide the next action.
        return model_destination

    return tools_to_model

Domain

Subdomains

Called By

Frequently Asked Questions

What does _make_tools_to_model_edge() do?
_make_tools_to_model_edge() is a function in the langchain codebase, defined in libs/langchain_v1/langchain/agents/factory.py.
Where is _make_tools_to_model_edge() defined?
_make_tools_to_model_edge() is defined in libs/langchain_v1/langchain/agents/factory.py at line 1731.
What does _make_tools_to_model_edge() call?
_make_tools_to_model_edge() calls 1 function(s): _fetch_last_ai_and_tool_messages.
What calls _make_tools_to_model_edge()?
_make_tools_to_model_edge() is called by 1 function(s): create_agent.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free