openai-agents-python/docs
Rohan Mehta dd881eed9a
feat: Add Graphviz-based agent visualization functionality (#147)
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:

```python
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:

<img width="614" alt="Screenshot 2025-03-13 at 18 36 23"
src="https://github.com/user-attachments/assets/d01fe502-6886-4efb-aaf8-c92e4524b0fe"
/>
2025-03-25 19:22:58 -04:00
..
assets Add documentation and example for agent visualization using Graphviz 2025-03-13 18:48:12 +01:00
ref [4/n] Add docs for MCP 2025-03-25 13:25:50 -04:00
stylesheets Initial commit 2025-03-11 09:42:28 -07:00
voice Update quickstart.md 2025-03-23 17:56:55 -04:00
agents.md Make the reset behavior on tool use configurable 2025-03-25 13:29:32 -04:00
config.md fix typo 2025-03-12 11:24:12 +08:00
context.md fix annotation numbering in context management 2025-03-21 21:10:30 +00:00
examples.md formatting updates to examples doc 2025-03-19 14:23:28 -07:00
guardrails.md fix line in guardrails 2025-03-21 15:58:34 -04:00
handoffs.md Initial commit 2025-03-11 09:42:28 -07:00
index.md fix: hyphenated compound adjective real world -> real-world for clarity 2025-03-12 10:48:29 +05:45
mcp.md [4/n] Add docs for MCP 2025-03-25 13:25:50 -04:00
models.md Update model docs with common issues 2025-03-13 13:43:18 -04:00
multi_agent.md docs: clarify multi-agent orchestration description 2025-03-11 23:57:03 +01:00
quickstart.md Update quickstart.md 2025-03-12 00:48:29 +00:00
results.md docs: Fix typos in documentation files 2025-03-12 00:12:31 +01:00
running_agents.md fix: replace undefined variable name in documentation code snippet 2025-03-12 15:07:56 +03:00
streaming.md Initial commit 2025-03-11 09:42:28 -07:00
tools.md Initial commit 2025-03-11 09:42:28 -07:00
tracing.md Add Weights & Biases to tracing docs 2025-03-21 21:37:54 +00:00
visualization.md Update visualization.md 2025-03-25 13:34:45 -04:00