Hi @riccardofresi , What is the expectation here?
I tried your graph, Below is compiled state graph where each node is a react agent (also a compiled state graph which can make multiple LLM calls, until it feels that a valid response is available ). This seems like a simple parent-subgraph architecture.
Code:
from typing import NotRequired, TypedDict, Literal, Annotated, Sequence
from langgraph.graph import START, END, StateGraph, add_messages
from langchain_core.messages import BaseMessage, HumanMessage
from langgraph.prebuilt import create_react_agent
def DecisionAgent(model=None, tools=[]):
if model is None:
model = llm
agent = create_react_agent(
model=model,
tools=tools,
)
return agent
def ScenarioAgent(model=None, tools=[]):
if model is None:
model = llm
agent = create_react_agent(
model=model,
tools=tools,
)
return agent
def ExecutionAgent(model=None, tools=[]):
if model is None:
model = llm
agent = create_react_agent(
model=model,
tools=tools,
)
return agent
def AnalysisAgent(model=None, tools=[]):
if model is None:
model = llm
agent = create_react_agent(
model=model,
tools=tools,
)
return agent
def PromotionAgent(model=None, tools=[]):
if model is None:
model = llm
agent = create_react_agent(
model=model,
tools=tools,
)
return agent
class State(TypedDict):
messages: Annotated[Sequence[BaseMessage], add_messages]
graph_builder = StateGraph(State)
def conditional_edge(state: State) -> Literal["Scenario", "Promotion"]:
last_message = state["messages"][-1].content if state["messages"] else ""
print("last_message in conditional_edge:", last_message)
if last_message == "retry":
return "Scenario"
return "Promotion"
#EVERY NODE IS A REACT AGENT
graph_builder.add_node("Scenario", ScenarioAgent())
graph_builder.add_node("Execution", ExecutionAgent())
graph_builder.add_node("Analysis", AnalysisAgent())
graph_builder.add_node("Decision", DecisionAgent())
graph_builder.add_node("Promotion", PromotionAgent())
graph_builder.add_edge(START, "Scenario")
graph_builder.add_edge("Scenario", "Execution")
graph_builder.add_edge("Execution", "Analysis")
graph_builder.add_edge("Analysis", "Decision")
graph_builder.add_conditional_edges("Decision", conditional_edge)
graph_builder.add_edge("Promotion", END)
app = graph_builder.compile()
On executing below
for msg in app.stream({"messages": [HumanMessage(content="hi, how’s going?")]}, stream_mode="updates"):
print(msg)
Response (As expected), it followed path Scenario → Execution → Analysis → Decision → Promotion
At each node, 1 LLM call was made and AIMessage added to the state passed to next agent in graph execution flow, Each Node have 1 additional AIMessage than previous in response.
It checked for last message at decision, since it was not “retry” it forwarded to Promotion agent
--- Node: Scenario ---
================================ Human Message =================================
hi, how’s going?
================================== Ai Message ==================================
Hello! I’m doing well, thanks for asking. How can I help you today?
--- Node: Execution ---
================================ Human Message =================================
hi, how’s going?
================================== Ai Message ==================================
Hello! I’m doing well, thanks for asking. How can I help you today?
================================== Ai Message ==================================
I’m here and ready to help! What can I do for you today?
--- Node: Analysis ---
================================ Human Message =================================
hi, how’s going?
================================== Ai Message ==================================
Hello! I’m doing well, thanks for asking. How can I help you today?
================================== Ai Message ==================================
I’m here and ready to help! What can I do for you today?
================================== Ai Message ==================================
Hi there! I’d love to help—could you tell me a bit more about what you need? For example, I can help with:
• Writing or editing emails, essays, reports
• Summarizing or explaining documents or articles
• Translating text between languages
• Brainstorming ideas or outlining plans
• Answering questions or doing research
• Writing or debugging code
Just let me know what you’re looking for, and we’ll get started!
last_message in conditional_edge: Hi there! I’m doing well—thanks for asking. What can I help you with today?
Here are a few examples of what I can do:
• Write, edit or proofread emails, essays, reports, etc.
• Summarize or explain articles and documents
• Translate text between languages
• Brainstorm ideas or outline plans
• Research topics and answer questions
• Write or debug code in various programming languages
Feel free to tell me more about your needs, and we’ll get started!
--- Node: Decision ---
================================ Human Message =================================
hi, how’s going?
================================== Ai Message ==================================
Hello! I’m doing well, thanks for asking. How can I help you today?
================================== Ai Message ==================================
I’m here and ready to help! What can I do for you today?
================================== Ai Message ==================================
Hi there! I’d love to help—could you tell me a bit more about what you need? For example, I can help with:
• Writing or editing emails, essays, reports
• Summarizing or explaining documents or articles
• Translating text between languages
• Brainstorming ideas or outlining plans
• Answering questions or doing research
• Writing or debugging code
Just let me know what you’re looking for, and we’ll get started!
================================== Ai Message ==================================
Hi there! I’m doing well—thanks for asking. What can I help you with today?
Here are a few examples of what I can do:
• Write, edit or proofread emails, essays, reports, etc.
• Summarize or explain articles and documents
• Translate text between languages
• Brainstorm ideas or outline plans
• Research topics and answer questions
• Write or debug code in various programming languages
Feel free to tell me more about your needs, and we’ll get started!
--- Node: Promotion ---
================================ Human Message =================================
hi, how’s going?
================================== Ai Message ==================================
Hello! I’m doing well, thanks for asking. How can I help you today?
================================== Ai Message ==================================
I’m here and ready to help! What can I do for you today?
================================== Ai Message ==================================
Hi there! I’d love to help—could you tell me a bit more about what you need? For example, I can help with:
• Writing or editing emails, essays, reports
• Summarizing or explaining documents or articles
• Translating text between languages
• Brainstorming ideas or outlining plans
• Answering questions or doing research
• Writing or debugging code
Just let me know what you’re looking for, and we’ll get started!
================================== Ai Message ==================================
Hi there! I’m doing well—thanks for asking. What can I help you with today?
Here are a few examples of what I can do:
• Write, edit or proofread emails, essays, reports, etc.
• Summarize or explain articles and documents
• Translate text between languages
• Brainstorm ideas or outline plans
• Research topics and answer questions
• Write or debug code in various programming languages
Feel free to tell me more about your needs, and we’ll get started!
================================== Ai Message ==================================
Hi there! I’m doing well, thanks for asking. What can I help you with today?