[Python] Langgraph tools_condition prebuilt method routing to other nodes instead of END node

Discussão em 'Python' iniciado por Stack, Agosto 24, 2025.

  1. Stack

    Stack Membro Participativo

    I was building quite a simple graph using Langgraph. I decided the pre-built tools_condition function from langgraph. So I imported it and used it as follows:

    graph_builder = StateGraph(State)
    graph_builder.add_node("chatbot", assistant)
    graph_builder.add_node("tools", lambda state: run_tool(state, tools={"athena_query":athena_query}))
    graph_builder.add_node("report_checker", report_check)

    graph_builder.add_edge(START, "chatbot")
    graph_builder.add_conditional_edges(
    "chatbot",
    tools_condition
    )
    # graph_builder.add_edge("chatbot", "tools")
    graph_builder.add_edge("tools", "chatbot")
    graph_builder.add_edge("chatbot", "report_checker")

    graph = graph_builder.compile(checkpointer=memory)


    The resulting graph is the following:

    [​IMG]

    It does not make sense that there is a conditional edge pointing from assistant to report_checker when I specify that it is a regular edge. Why is the default behaviour of tools_condition to make conditional edges to everything else if its not a tool call and not just go to END?

    I know this because if I pass the routing arguments of where it is supposed to go, the extra conditional edges disappear:

    graph_builder = StateGraph(State)
    graph_builder.add_node("chatbot", assistant)
    graph_builder.add_node("tools", lambda state: run_tool(state, tools={"athena_query":athena_query}))
    graph_builder.add_node("report_checker", report_check)

    graph_builder.add_edge(START, "chatbot")
    graph_builder.add_conditional_edges(
    "chatbot",
    tools_condition,
    {
    "END": END,
    "tools": "tools",
    }
    )
    graph_builder.add_edge("tools", "chatbot")
    graph_builder.add_edge("chatbot", "report_checker")

    graph = graph_builder.compile(checkpointer=memory)


    The unwanted conditional edge is now gone:

    [​IMG]

    Why is default tools_condition routing the main node somewhere other than the END node?

    Continue reading...

Compartilhe esta Página