From 1029260dcc9e338c25398cc52e0dd31b5eda5e88 Mon Sep 17 00:00:00 2001 From: Annyfee <2287551746@qq.com> Date: Sun, 23 Nov 2025 16:27:30 +0800 Subject: [PATCH] refactor:use truthy check instead of len() for tool_calls --- 08_langgraph_basics/03_conditional_router.py | 2 +- 08_langgraph_basics/04_agent_with_memory.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/08_langgraph_basics/03_conditional_router.py b/08_langgraph_basics/03_conditional_router.py index a835e31..46aef7d 100644 --- a/08_langgraph_basics/03_conditional_router.py +++ b/08_langgraph_basics/03_conditional_router.py @@ -41,7 +41,7 @@ tool_node = ToolNode(tools) # 工具节点函数,langgraph已封装 # ReAct Step4:Loop Controller(是否循环) def should_continue(state:MessagesState): last_msg = state["messages"][-1] - if hasattr(last_msg,"tool_calls") and len(last_msg.tool_calls) > 0: + if hasattr(last_msg,"tool_calls") and last_msg.tool_calls: return "tools" # 有工具调用 -> 执行工具 return END # 无工具调用 -> 返回答案 diff --git a/08_langgraph_basics/04_agent_with_memory.py b/08_langgraph_basics/04_agent_with_memory.py index 101e09f..542f902 100644 --- a/08_langgraph_basics/04_agent_with_memory.py +++ b/08_langgraph_basics/04_agent_with_memory.py @@ -49,7 +49,7 @@ tool_node = ToolNode(tools) # ReAct Step4:Loop Controller(是否循环) def should_continue(state:MessagesState): last_msg = state["messages"][-1] - if hasattr(last_msg,"tool_calls") and len(last_msg.tool_calls) > 0: + if hasattr(last_msg,"tool_calls") and last_msg.tool_calls: return "tools" # 有工具调用 -> 执行工具 return END # 无工具调用 -> 返回答案