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 would like to react to exceptions that happen inside tools.
A minimal example of my situation is mostly copied from the ReAct Agent
importasynciofromlangchain_core.toolsimporttoolfromlangchain_openaiimportAzureChatOpenAIfromlanggraph.graphimportEND, START, MessagesState, StateGraphfromlanggraph.prebuiltimportToolNode@tooldefget_weather(location: str):
"""Call to get the current weather."""raiseValueError("Location not found")
return"It's 90 degrees and sunny."@tooldefget_coolest_cities():
"""Get a list of coolest cities"""return"nyc, sf"tools= [get_weather, get_coolest_cities]
tool_node=ToolNode(tools)
model_with_tools=AzureChatOpenAI(
...
).bind_tools(tools)
defshould_continue(state: MessagesState):
messages=state["messages"]
last_message=messages[-1]
iflast_message.tool_calls:
return"tools"returnENDdefcall_model(state: MessagesState):
messages=state["messages"]
response=model_with_tools.invoke(messages)
return {"messages": [response]}
workflow=StateGraph(MessagesState)
# Define the two nodes we will cycle betweenworkflow.add_node("agent", call_model)
workflow.add_node("tools", tool_node)
workflow.add_edge(START, "agent")
workflow.add_conditional_edges("agent", should_continue, ["tools", END])
workflow.add_edge("tools", "agent")
app=workflow.compile()
asyncdefrun():
asyncforeventinapp.astream_events(
input={"messages": [("human", "what's the weather in sf?")]}, version="v2"
):
if"tool"inevent["event"]:
print(event["event"])
asyncio.run(run())
When I run this code without the raise ValueError("Location not found") line inside the code, I see two events called on_tool_start and on_tool_end. However, if a add the Exception, I only see on_tool_start.
Reading the Callback events I would have guessed that a on_tool_error event was emitted, but this is not the case. Is there anything I'm doing wrong?
I also tried to dispatch a custom event from inside my tool call. While this basically works I do not know how I would find out the tool_call_id in this case as I think that the ToolNode does not pass this down to the tool implementations.
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.
-
I would like to react to exceptions that happen inside tools.
A minimal example of my situation is mostly copied from the ReAct Agent
When I run this code without the
raise ValueError("Location not found")
line inside the code, I see two events calledon_tool_start
andon_tool_end
. However, if a add the Exception, I only seeon_tool_start
.Reading the Callback events I would have guessed that a
on_tool_error
event was emitted, but this is not the case. Is there anything I'm doing wrong?I also tried to dispatch a custom event from inside my tool call. While this basically works I do not know how I would find out the
tool_call_id
in this case as I think that theToolNode
does not pass this down to the tool implementations.Beta Was this translation helpful? Give feedback.
All reactions