Skip to content

GraphFlow doesn't follow conditions, ends after a single node step #6716

Open
@smpurkis

Description

@smpurkis

What happened?

Describe the bug
The GraphFlow terminates early, seemingly ignoring edges and edge conditions.

To Reproduce
Steps to reproduce the behavior.

The below is a slightly modified version of the example code from https://microsoft.github.io/autogen/stable/user-guide/agentchat-user-guide/graph-flow.html#advanced-example-conditional-loop-filtered-summary.

The changes have comments by them, essentially an additional node is added. Image

from autogen_agentchat.agents import (
    AssistantAgent,
    MessageFilterAgent,
    MessageFilterConfig,
    PerSourceFilter,
)
from autogen_agentchat.teams import (
    DiGraphBuilder,
    GraphFlow,
)
from autogen_agentchat.ui import Console
from autogen_ext.models.openai import OpenAIChatCompletionClient
from autogen_core.tools import FunctionTool

from duckduckgo_search import DDGS


model_client = OpenAIChatCompletionClient(model="gpt-4o")

# Agents
generator = AssistantAgent(
    "generator",
    model_client=model_client,
    system_message="Generate a list of creative ideas.",
)
reviewer = AssistantAgent(
    "reviewer",
    model_client=model_client,
    system_message="Review ideas and provide feedbacks, or just 'APPROVE' for final approval.",
)
summarizer_core = AssistantAgent(
    "summary",
    model_client=model_client,
    system_message="Summarize the user request and the final feedback.",
)

# additional agent
# basic web surfer agent to search the web for information using duckduckgo_search


def web_search(search: str) -> str:
    return DDGS().text(search, max_results=5)


web_search_tool = FunctionTool(
    web_search, description="Search the web for information."
)

web_surfer = AssistantAgent(
    "web_surfer",
    model_client,
    system_message="You are a web surfer agent. Your task is to search the web for information based on the instructions provided by the planner agent. You will gather information and present it to the fact-finder agent. If you need more specific details, ask the planner agent for clarification.",
    tools=[web_search_tool],
)

# Filtered summarizer
filtered_summarizer = MessageFilterAgent(
    name="summary",
    wrapped_agent=summarizer_core,
    filter=MessageFilterConfig(
        per_source=[
            PerSourceFilter(source="user", position="first", count=1),
            PerSourceFilter(source="reviewer", position="last", count=1),
        ]
    ),
)


async def main() -> None:
    builder = DiGraphBuilder()
    builder.add_node(generator).add_node(reviewer).add_node(
        filtered_summarizer
    ).add_node(web_surfer)  # add new web surfer agent

    # add web surfer node between generator and reviewer
    builder.add_edge(generator, web_surfer)
    # builder.add_edge(generator, reviewer) # original connection
    builder.add_edge(web_surfer, reviewer)
    builder.add_edge(
        reviewer,
        filtered_summarizer,
        condition=lambda msg: "APPROVE" in msg.to_model_text(),
    )
    builder.add_edge(
        reviewer,
        web_surfer,  # was generator
        condition=lambda msg: "APPROVE" not in msg.to_model_text(),
    )
    builder.set_entry_point(generator)
    graph = builder.build()
    flow = GraphFlow(
        participants=builder.get_participants(),
        graph=graph,
    )

    await Console(flow.run_stream(task="Brainstorm ways to reduce plastic waste."))


if __name__ == "__main__":
    import asyncio

    asyncio.run(main())

Expected behavior
The graph is followed, i.e.

  1. Generator
  2. Web search
  3. Reviewer, decides to go back to 2 or continue to 4.
  4. Filtered summarizer

Screenshots
Image

Additional context
A run showing it not working

➜ python main_graph_autogen.py 
---------- TextMessage (user) ----------
Brainstorm ways to reduce plastic waste.
/Users/user/demo_1/llm/deep-researchers/local-deep-research/.venv/lib/python3.10/site-packages/autogen_agentchat/agents/_assistant_agent.py:981: UserWarning: Resolved model mismatch: gpt-4o-2024-08-06 != gpt-4o-2024-11-20. Model mapping in autogen_ext.models.openai may be incorrect. Set the model to gpt-4o-2024-11-20 to enhance token/cost estimation and suppress this warning.
  model_result = await model_client.create(
---------- TextMessage (planner) ----------
To brainstorm ways to reduce plastic waste, I need to gather different strategies, examples, and practices from various sources, including environmental organizations, case studies, and expert advice. Here's a breakdown of smaller tasks:

1. Research individual actions to reduce plastic waste (e.g., reusable alternatives, recycling methods, DIY solutions).  
2. Investigate government and policy initiatives aimed at reducing plastic waste (e.g., bans, taxes, regulations, incentives).  
3. Explore business and corporate solutions that focus on plastic waste reduction (e.g., sustainable packaging, innovation in materials).  
4. Find global examples of successful community-led campaigns or projects that have significantly reduced plastic waste.  
5. Look into new technological advancements and innovations that minimize plastic waste or substitute plastic with eco-friendly materials.  
6. Identify educational and awareness programs that influence people to adopt plastic waste reduction practices.  

Web surfer, please start searching for information about this topic.
---------- StopMessage (DiGraphStopAgent) ----------
Digraph execution is complete

Which packages was the bug in?

Python Extensions (autogen-ext), Python AgentChat (autogen-agentchat>=0.4.0)

AutoGen library version.

Python 0.6.1

Other library version.

No response

Model used

gpt-4o-2024-08-06

Model provider

Azure OpenAI

Other model provider

No response

Python version

3.10

.NET version

None

Operating system

Ubuntu

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions