I am new to langgraph (python) and trying to go through some examples found in Langgraph pages.
My code looks like below:
class State(TypedDict):
summary: str
messages: list
def summarize_conversation(state: State):
# First, we summarize the conversation
summary = state.get("summary", "")
if summary:
# If a summary already exists, we use a different system prompt
# to summarize it than if one didn't
summary_message = (
f"This is summary of the conversation to date: {summary}\n\n"
"Extend the summary by taking into account the new messages above:"
)
else:
summary_message = "Create a summary of the conversation above:"
messages = state["messages"] + [HumanMessage(content=summary_message)]
response = model.invoke(messages)
return {"summary": response.content, "messages": messages}
graph_builder = StateGraph(State)
graph_builder.add_node("tools", ToolNode(tool_set))
graph_builder.add_node("chatbot", lambda state: {"messages":assistant_runnable.invoke(state)})
graph_builder.add_node("summarize_conversation", summarize_conversation)
graph_builder.add_edge("tools", "summarize_conversation")
graph_builder.add_edge("summarize_conversation", "chatbot")
graph_builder.add_conditional_edges(
"chatbot", tools_condition
)
graph_builder.set_entry_point("chatbot")
graph = graph_builder.compile()
The trouble is I get this extra conditional edge from Chatbot node to "summarise_conversation" node. Why is this and how do i remove this ?
PS : For brevity I have not included the full code.
2 Answers 2
Probably already solved, but I noticed that if you don't specify the third parameter in the conditional edge, where you provide the mapping or the list of potential next nodes, this is what happens. And it makes sense as the graph has no idea which nodes can come next, so just puts a conditional edge to all states.
To solve, you can add the list of nodes that are expected to be conditionally reached next:
graph_builder.add_conditional_edges(
"chatbot", tools_condition, [tools, END]
)
Comments
you need specific 'edges' in 'add_conditional_edges'
Method 1:
graph_builder.add_conditional_edges(
"node", routing_function, path_map
)
Method 2:
def routing_function(state: State) -> typing.Literal['node_1', 'node_2]:
pass
graph_builder.add_conditional_edges(
"node", routing_function
)
Comments
Explore related questions
See similar questions with these tags.
tools_conditionhere?tools_conditioncan only return"tools"or"__end__".