Replies: 7 comments 22 replies
-
To address the issue of stopping the LangGraph workflow as soon as the tool returns a response, you can modify the workflow to explicitly handle the tool's response and break the loop when a valid response is detected. Here's a refined approach based on your current setup:
Here's an example of how you can modify your code: class Assistant:
def __init__(self, runnable: Runnable):
self.runnable = runnable
def __call__(self, state, config: RunnableConfig):
while True:
configuration = config.get("configurable", {})
context = configuration.get("contexto", {})
user_id = configuration.get("user_id", None)
state = {**state, "user_info": user_id, **context}
result = self.runnable.invoke(state)
# Check if the tool has returned a response
if result.tool_calls:
print("*** break: Tool response received")
break
elif not result.content or (isinstance(result.content, list) and not result.content[0].get("text")):
messages = state["messages"] + [("user", "Respond with a real output.")]
state = {**state, "messages": messages}
else:
print("*** break: " + str(result))
break
return {"messages": result} Key Changes:
Ensure that your tool is correctly configured to return a response that can be detected by this condition. If To continue talking to Dosu, mention @dosu. Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Other |
Beta Was this translation helpful? Give feedback.
-
@dosu, I give up. It is still not working. |
Beta Was this translation helpful? Give feedback.
-
Maybe this log could help you understand the issue. Ypu can see that the tool was called and returned a response: User input=> 'qual o clima? [0:writes] Finished step 0 with writes to 1 channel:
Thank you |
Beta Was this translation helpful? Give feedback.
-
Hello @dosu, I appreciate your suggestions, but as I already explained, I am running the standard code provided in the LangGraph documentation. Below are the relevant parts of the code. So, when you suggest things like "Check tools_condition," it doesn’t seem to make sense because none of the examples I’ve seen do anything different from what my code does. I am executing a single tool that simply returns a string. It’s way too simple, far too basic. Thank you. class Assistant:
def handle_tool_error(state) -> dict: def create_tool_node_with_fallback(tools: list) -> RunnableWithFallbacks[Any, dict]:
|
Beta Was this translation helpful? Give feedback.
-
Just to tell you, I created a Jupyter Notebook with my isolated project to reproduce the issue. Here you are the link: https://github.com/mrctito/tests/blob/master/langgraph_tools.ipynb Thank you! |
Beta Was this translation helpful? Give feedback.
-
@dosu, ok. Maybe this could help a maintainer? |
Beta Was this translation helpful? Give feedback.
-
Hi @dosu,
I’m running an assistant with only one tool, which is being triggered correctly. However, result.tool_calls always returns False, and the process eventually reaches the maximum recursion limit.
I don’t want to increase this limit. Instead, I need the workflow to stop as soon as the tool returns a response.
Below, I’ve provided the details of the tool and the relevant code snippet for review.
Beta Was this translation helpful? Give feedback.
All reactions