Skip to content

feat: Add Graphviz-based agent visualization functionality#147

Merged
rm-openai merged 35 commits intoopenai:mainfrom
MartinEBravo:feat/draw_graph
Mar 25, 2025
Merged

feat: Add Graphviz-based agent visualization functionality#147
rm-openai merged 35 commits intoopenai:mainfrom
MartinEBravo:feat/draw_graph

Conversation

@MartinEBravo
Copy link
Copy Markdown
Contributor

This pull request introduces functionality for visualizing agent structures using Graphviz. The changes include adding a new dependency, implementing functions to generate and draw graphs, and adding tests for these functions.

New functionality for visualizing agent structures:

  • Added graphviz as a new dependency in pyproject.toml.
  • Implemented functions in src/agents/visualizations.py to generate and draw graphs for agents using Graphviz. These functions include get_main_graph, get_all_nodes, get_all_edges, and draw_graph.

Testing the new visualization functionality:

  • Added tests in tests/test_visualizations.py to verify the correctness of the graph generation and drawing functions. The tests cover get_main_graph, get_all_nodes, get_all_edges, and draw_graph.

For example, given the following code:

from agents import Agent, function_tool
from agents.visualizations import draw_graph


@function_tool
def get_weather(city: str) -> str:
    return f"The weather in {city} is sunny."


spanish_agent = Agent(
    name="Spanish agent",
    instructions="You only speak Spanish.",
)

english_agent = Agent(
    name="English agent",
    instructions="You only speak English",
)

triage_agent = Agent(
    name="Triage agent",
    instructions="Handoff to the appropriate agent based on the language of the request.",
    handoffs=[spanish_agent, english_agent],
    tools=[get_weather],
)


draw_graph(triage_agent)

Generates the following image:

Screenshot 2025-03-13 at 18 36 23

Copy link
Copy Markdown
Collaborator

@rm-openai rm-openai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is such a great PR/idea. Couple of small things to get it in:

  1. Could you please add this to the agents/src/extensions directory? So the import would be from agents.extensions import visualization
  2. Could you please add an optional dependency group? So you can do pip install openai-agents[visualization] to install it, and if you don't, there's an import error.

Thank you!

Comment thread tests/test_visualizations.py Outdated
import graphviz
import pytest

from src.agents.agent import Agent
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
from src.agents.agent import Agent
from agents import Agent

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed!

@MartinEBravo MartinEBravo requested a review from rm-openai March 18, 2025 08:58
Copy link
Copy Markdown
Collaborator

@rm-openai rm-openai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like tests are failing. I think you can add an optional dependency to pyproject.toml

[project.optional-dependencies]
visualization=["graphviz"]

Comment thread src/agents/extensions/visualization.py Outdated
# Bigger handoffs (rounded box, yellow)
for handoff in agent.handoffs:
parts.append(
f'"{handoff.name}" [label="{handoff.name}", shape=box, style=filled, style=rounded, '
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you want handoff.agent_name not handoff.name, if it's a Handoff instance

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added isinstance check.

Comment thread src/agents/extensions/visualization.py Outdated
f'"{handoff.name}" [label="{handoff.name}", shape=box, style=filled, style=rounded, '
f"fillcolor=lightyellow, width=1.5, height=0.8];"
)
parts.append(get_all_nodes(handoff))
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is right - the handoffs list is list[Handoff | Agent]. So the node might be an Agent, but it might also be a Handoff object, where you don't statically know which agent is being handed off to. You can use handoff.agent_name for that

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added isinstance check.

@MartinEBravo MartinEBravo requested a review from rm-openai March 24, 2025 08:47
Copy link
Copy Markdown
Collaborator

@rm-openai rm-openai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

THink you just need a rebase and should be good to go

Comment thread pyproject.toml Outdated
Comment on lines +53 to +57
[project.optional-dependencies]
visualization = [
"graphviz>=0.17",
]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you might need a rebase, as [project.optional-dependencies] exists already. Also perhaps viz is a shorter optional dependency heh

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah looks like this lack of rebase is causing all the checks to fail.

@MartinEBravo MartinEBravo requested a review from rm-openai March 25, 2025 17:04
@rm-openai
Copy link
Copy Markdown
Collaborator

Oof unfortunately checks still failing. I tried to update them but you may need to run make tests, make mypy etc locally.

@MartinEBravo
Copy link
Copy Markdown
Contributor Author

Ok, now it cannot fail.

@rm-openai rm-openai merged commit dd881ee into openai:main Mar 25, 2025
5 checks passed
@lbug lbug mentioned this pull request Mar 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants