Home / Function/ _make_model_to_tools_edge() — langchain Function Reference

_make_model_to_tools_edge() — langchain Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  20046200_0f69_48b9_d47c_7a3ea7d0358f["_make_model_to_tools_edge()"]
  fd7a28b1_3772_169b_6524_1342f35143b1["factory.py"]
  20046200_0f69_48b9_d47c_7a3ea7d0358f -->|defined in| fd7a28b1_3772_169b_6524_1342f35143b1
  f4b66c38_651c_807a_caca_41f73fbbe516["create_agent()"]
  f4b66c38_651c_807a_caca_41f73fbbe516 -->|calls| 20046200_0f69_48b9_d47c_7a3ea7d0358f
  63280d36_c373_21de_7184_3a4b4f8606bb["_resolve_jump()"]
  20046200_0f69_48b9_d47c_7a3ea7d0358f -->|calls| 63280d36_c373_21de_7184_3a4b4f8606bb
  895a4733_2a72_a5d8_7ab0_f205dfec9cd6["_fetch_last_ai_and_tool_messages()"]
  20046200_0f69_48b9_d47c_7a3ea7d0358f -->|calls| 895a4733_2a72_a5d8_7ab0_f205dfec9cd6
  style 20046200_0f69_48b9_d47c_7a3ea7d0358f fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/langchain_v1/langchain/agents/factory.py lines 1643–1701

def _make_model_to_tools_edge(
    *,
    model_destination: str,
    structured_output_tools: dict[str, OutputToolBinding[Any]],
    end_destination: str,
) -> Callable[[dict[str, Any]], str | list[Send] | None]:
    def model_to_tools(
        state: dict[str, Any],
    ) -> str | list[Send] | None:
        # 1. If there's an explicit jump_to in the state, use it
        if jump_to := state.get("jump_to"):
            return _resolve_jump(
                jump_to,
                model_destination=model_destination,
                end_destination=end_destination,
            )

        last_ai_message, tool_messages = _fetch_last_ai_and_tool_messages(state["messages"])

        # 2. if no AIMessage exists (e.g., messages were cleared), exit the loop
        if last_ai_message is None:
            return end_destination

        tool_message_ids = [m.tool_call_id for m in tool_messages]

        # 3. If the model hasn't called any tools, exit the loop
        # this is the classic exit condition for an agent loop
        if len(last_ai_message.tool_calls) == 0:
            return end_destination

        pending_tool_calls = [
            c
            for c in last_ai_message.tool_calls
            if c["id"] not in tool_message_ids and c["name"] not in structured_output_tools
        ]

        # 4. If there are pending tool calls, jump to the tool node
        if pending_tool_calls:
            return [
                Send(
                    "tools",
                    ToolCallWithContext(
                        __type="tool_call_with_context",
                        tool_call=tool_call,
                        state=state,
                    ),
                )
                for tool_call in pending_tool_calls
            ]

        # 5. If there is a structured response, exit the loop
        if "structured_response" in state:
            return end_destination

        # 6. AIMessage has tool calls, but there are no pending tool calls which suggests
        # the injection of artificial tool messages. Jump to the model node
        return model_destination

    return model_to_tools

Domain

Subdomains

Called By

Frequently Asked Questions

What does _make_model_to_tools_edge() do?
_make_model_to_tools_edge() is a function in the langchain codebase, defined in libs/langchain_v1/langchain/agents/factory.py.
Where is _make_model_to_tools_edge() defined?
_make_model_to_tools_edge() is defined in libs/langchain_v1/langchain/agents/factory.py at line 1643.
What does _make_model_to_tools_edge() call?
_make_model_to_tools_edge() calls 2 function(s): _fetch_last_ai_and_tool_messages, _resolve_jump.
What calls _make_model_to_tools_edge()?
_make_model_to_tools_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