1

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 ?

enter image description here

PS : For brevity I have not included the full code.

cottontail
25.8k26 gold badges187 silver badges178 bronze badges
asked Sep 24, 2024 at 11:40
4
  • what is tools_condition here? Commented Sep 24, 2024 at 16:40
  • @cottontail - langchain-ai.github.io/langgraph/reference/prebuilt/… its a prebuilt condition edge that can be used to connect a "chatbot" and "Tool Node". Commented Sep 25, 2024 at 14:16
  • There must be some other code to define the graph that is not shown here that is causing the conditional edge. Your code as is shouldn't have that because the prebuilt tools_condition can only return "tools" or "__end__". Commented Sep 25, 2024 at 18:40
  • @cottontail - i agree - and i look at the source code of that tools_condition - langchain-ai.github.io/langgraph/reference/prebuilt/… but no luck. also realised yesterday that someone else had the same qn asked 4 days earlier than mine.. - stackoverflow.com/questions/79003055/… Commented Sep 26, 2024 at 3:27

2 Answers 2

0

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]
)
answered Apr 15, 2025 at 10:41
Sign up to request clarification or add additional context in comments.

Comments

0

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
)
answered Apr 18, 2025 at 9:51

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.