第七章:常见报错和坑
坑 1:忘记 compile()
python
# 错误
graph = builder # 这不是可执行的图
graph.invoke(...) # 报错
# 正确
graph = builder.compile()
坑 2:节点返回值不是字典
python
# 错误
def node(state):
return "hello" # 返回字符串会出错
# 正确
def node(state):
return {"answer": "hello"}
坑 3:条件边的映射不完整
add_conditional_edges 的映射字典必须覆盖所有可能的返回值。如果 should_continue 可能返回 "a" 和 "b",映射里必须同时有这两个键。
坑 4:API Key 没配置
如果报 AuthenticationError,检查 .env 文件是否在项目根目录,以及 Key 是否正确。