You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I encounter a problem that the workflow cannot route to END with the model is binded with a tool.
Here is a simple example to replicate my errors:
class State(TypedDict):
# Messages have the type "list". The `add_messages` function
# in the annotation defines how this state key should be updated
# (in this case, it appends messages to the list, rather than overwriting them)
messages: Annotated[list, add_messages]
@tool
def get_today_menu(input:str) -> str:
"""Get the menu for today."""
return "Today menu is: \n1. Pizza\n2. Burger\n3. Salad\n4. Pasta\n5. Sushi"
def should_continue(state: State):
messages = state["messages"]
last_message = messages[-1]
if last_message.tool_calls:
return "tools"
return END
tools = [get_today_menu]
tool_node = ToolNode(tools)
llm= ChatVertexAI(model="gemini-2.0-flash", temperature=0)
llm_with_tools=llm.bind_tools(tools, tool_choice="any")
def chatbot(state: State):
return {"messages": [llm_with_tools.invoke(state["messages"])]}
graph_builder = StateGraph(State)
graph_builder.add_node("chatbot", chatbot)
graph_builder.add_node("tools", tool_node)
graph_builder.add_conditional_edges("chatbot",should_continue,["tools",END])
graph_builder.add_edge(START, "chatbot")
graph_builder.add_edge("tools", "chatbot")
graph = graph_builder.compile()
try:
display(Image(graph.get_graph().draw_mermaid_png()))
except Exception:
# This requires some extra dependencies and is optional
pass
onfig = {
"recursion_limit": 4,
"configurable": {
"thread_id": "1"
}
}
for idx, event in enumerate(graph.stream(
{ "messages": [HumanMessage(content="What is the menu for today?" ),]},
config
)):
for value in event.values():
print(f"Event {idx}")
msg = value["messages"][-1]
if msg.content:
print("Assistant:", msg.content)
elif hasattr(msg, "additional_kwargs") and "function_call" in msg.additional_kwargs:
fn = msg.additional_kwargs["function_call"]
print("Function Call:", fn["name"])
print("Arguments:", fn["arguments"])
else:
print("Empty response or unexpected format.")
The error message are as following:
Event 0
Function Call: get_today_menu
Arguments: {"input": "today"}
Event 1
Assistant: Today menu is:
1. Pizza
2. Burger
3. Salad
4. Pasta
5. Sushi
Event 2
Function Call: get_today_menu
Arguments: {"input": "today"}
Event 3
Assistant: Today menu is:
1. Pizza
2. Burger
3. Salad
4. Pasta
5. Sushi
---------------------------------------------------------------------------
GraphRecursionError Traceback (most recent call last)
Cell In[5], [line 11](vscode-notebook-cell:?execution_count=5&line=11)
[8](vscode-notebook-cell:?execution_count=5&line=8) with open('job_description.py', "r", encoding="utf-8") as f:
[9](vscode-notebook-cell:?execution_count=5&line=9) jd = f.read()
---> [11](vscode-notebook-cell:?execution_count=5&line=11) for idx, event in enumerate(graph.stream(
[12](vscode-notebook-cell:?execution_count=5&line=12) { "messages": [HumanMessage(content="What is the menu for today?" ),]},
[13](vscode-notebook-cell:?execution_count=5&line=13) config
[14](vscode-notebook-cell:?execution_count=5&line=14) )):
[15](vscode-notebook-cell:?execution_count=5&line=15) for value in event.values():
[17](vscode-notebook-cell:?execution_count=5&line=17) print(f"Event {idx}")
File ~/Projects/cover-letter-tool/venv/lib/python3.12/site-packages/langgraph/pregel/__init__.py:2481, in Pregel.stream(self, input, config, stream_mode, output_keys, interrupt_before, interrupt_after, checkpoint_during, debug, subgraphs)
[2472](https://file+.vscode-resource.vscode-cdn.net/Users/heodi/Projects/cover-letter-tool/~/Projects/cover-letter-tool/venv/lib/python3.12/site-packages/langgraph/pregel/__init__.py:2472) if loop.status == "out_of_steps":
[2473](https://file+.vscode-resource.vscode-cdn.net/Users/heodi/Projects/cover-letter-tool/~/Projects/cover-letter-tool/venv/lib/python3.12/site-packages/langgraph/pregel/__init__.py:2473) msg = create_error_message(
[2474](https://file+.vscode-resource.vscode-cdn.net/Users/heodi/Projects/cover-letter-tool/~/Projects/cover-letter-tool/venv/lib/python3.12/site-packages/langgraph/pregel/__init__.py:2474) message=(
[2475](https://file+.vscode-resource.vscode-cdn.net/Users/heodi/Projects/cover-letter-tool/~/Projects/cover-letter-tool/venv/lib/python3.12/site-packages/langgraph/pregel/__init__.py:2475) f"Recursion limit of {config['recursion_limit']} reached "
(...)
[2479](https://file+.vscode-resource.vscode-cdn.net/Users/heodi/Projects/cover-letter-tool/~/Projects/cover-letter-tool/venv/lib/python3.12/site-packages/langgraph/pregel/__init__.py:2479) error_code=ErrorCode.GRAPH_RECURSION_LIMIT,
[2480](https://file+.vscode-resource.vscode-cdn.net/Users/heodi/Projects/cover-letter-tool/~/Projects/cover-letter-tool/venv/lib/python3.12/site-packages/langgraph/pregel/__init__.py:2480) )
-> [2481](https://file+.vscode-resource.vscode-cdn.net/Users/heodi/Projects/cover-letter-tool/~/Projects/cover-letter-tool/venv/lib/python3.12/site-packages/langgraph/pregel/__init__.py:2481) raise GraphRecursionError(msg)
[2482](https://file+.vscode-resource.vscode-cdn.net/Users/heodi/Projects/cover-letter-tool/~/Projects/cover-letter-tool/venv/lib/python3.12/site-packages/langgraph/pregel/__init__.py:2482) # set final channel values as run output
[2483](https://file+.vscode-resource.vscode-cdn.net/Users/heodi/Projects/cover-letter-tool/~/Projects/cover-letter-tool/venv/lib/python3.12/site-packages/langgraph/pregel/__init__.py:2483) run_manager.on_chain_end(loop.output)
GraphRecursionError: Recursion limit of 4 reached without hitting a stop condition. You can increase the limit by setting the `recursion_limit` config key.
For troubleshooting, visit: https://python.langchain.com/docs/troubleshooting/errors/GRAPH_RECURSION_LIMIT
I have test other routing functions from different tutorials but the workflow still keeps looping. I am not sure if it is caused by wrong routing function or other settings. Thanks for your time to review my questions.
Example 1:
def route_tools(
state: State,
):
"""
Use in the conditional_edge to route to the ToolNode if the last message
has tool calls. Otherwise, route to the end.
"""
if isinstance(state, list):
ai_message = state[-1]
elif messages := state.get("messages", []):
ai_message = messages[-1]
else:
raise ValueError(f"No messages found in input state to tool_edge: {state}")
if hasattr(ai_message, "tool_calls") and len(ai_message.tool_calls) > 0:
return "tools"
return END
graph_builder.add_conditional_edges(
"chatbot",
route_tools,
{"tools": "tools", END: END},
)
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello everyone,
I encounter a problem that the workflow cannot route to END with the model is binded with a tool.
Here is a simple example to replicate my errors:
The error message are as following:
I have test other routing functions from different tutorials but the workflow still keeps looping. I am not sure if it is caused by wrong routing function or other settings. Thanks for your time to review my questions.
Example 1:
Beta Was this translation helpful? Give feedback.
All reactions