Commit graph

411 commits

Author SHA1 Message Date
Dmitry Pimenov
0944390c3f adding Git MCP server example 2025-03-25 16:30:50 -07:00
Rohan Mehta
010022777b [5/n] MCP tracing
## Summary:

Adds tracing and tests for tracing.
- Tools are added to the agents
- Theres a span for the mcp tools lookup
- Functions have MCP data

## Test Plan:

Unit tests
.
2025-03-25 19:28:48 -04:00
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
Rohan Mehta
a0c5abce8f
fix(examples): make sure audio playback finishes (#340)
Previously the stream was closing as soon as all the audio was added but
didn't wait for it to be finished. Additionally the audio might seem
chopped off if there is no additional silence so this PR also adds one
second of silence before exiting the program.
2025-03-25 15:22:55 -04:00
Dominik Kundel
2104f5400b fix(examples): make sure audio playback finishes 2025-03-25 12:11:33 -07:00
Martín Bravo
c16deb22a1 Remove Jupyter Notebook files from .gitignore 2025-03-25 19:31:32 +01:00
Martín Bravo
70aff1d39d linting 2025-03-25 19:18:57 +01:00
Martín Bravo
3068e42029 Fix type ignore comment for agent check in get_all_edges function 2025-03-25 19:17:35 +01:00
Martín Bravo
351b6074e5 Refactor visualization functions to improve formatting and streamline edge generation 2025-03-25 19:12:40 +01:00
Martín Bravo
5ad53d8000 Add start and end nodes to graph visualization and update edge generation 2025-03-25 19:11:43 +01:00
Martín Bravo
29103caba9 Add Jupyter Notebook files to .gitignore 2025-03-25 19:03:39 +01:00
Martín Bravo
b9d32cda0f Refactor get_all_edges function to remove unused parent parameter 2025-03-25 19:02:21 +01:00
Martín Bravo
d8922ff472 Add visualization.md to navigation in mkdocs.yml 2025-03-25 18:56:01 +01:00
Martín Bravo
2f2606e5ea Add graphviz as a dependency and update import statements 2025-03-25 18:46:23 +01:00
Rohan Mehta
59aed3490d
Update pyproject.toml 2025-03-25 13:37:01 -04:00
Rohan Mehta
6be9b2a222
Update visualization.md 2025-03-25 13:34:45 -04:00
Rohan Mehta
ad020b73b5
Make the reset behavior on tool use configurable (#335)
## Summary:

#263 added this behavior. The goal was to prevent infinite loops when
tool choice was set. The key change I'm making is:
1. Making it configurable on the agent.
2. Doing bookkeeping in the Runner to track this, to prevent mutating
agents.
3. Not resetting the global tool choice in RunConfig.

## Test Plan:
Unit tests.
.
2025-03-25 13:30:32 -04:00
Martín Bravo
698fd69eb4 Update installation instructions for visualization dependency to use 'viz' 2025-03-25 18:29:55 +01:00
Rohan Mehta
6fb5792b77 Make the reset behavior on tool use configurable
## Summary:

#263 added this behavior. The goal was to prevent infinite loops when tool choice was set. The key change I'm making is:
1. Making it configurable on the agent.
2. Doing bookkeeping in the Runner to track this, to prevent mutating agents.
3. Not resetting the global tool choice in RunConfig.

## Test Plan:
Unit tests.
.
2025-03-25 13:29:32 -04:00
Rohan Mehta
362a9dc078
[4/n] Add docs for MCP (#338)
Just adding docs.

(Repeat of #324)
2025-03-25 13:27:15 -04:00
Rohan Mehta
86618494a9
[3/n] Add an MCP stdio example (#337)
### Summary:
Spins up a stdio server with some local files, then asks the model
questions.

### Test Plan:
Run the example, see it work.

(repeat of #322)
2025-03-25 13:27:05 -04:00
Rohan Mehta
b98a6bd4ac [4/n] Add docs for MCP
Just adding docs.
-
2025-03-25 13:25:50 -04:00
Rohan Mehta
de1e9a754d [3/n] Add an MCP stdio example
### Summary:
Spins up a stdio server with some local files, then asks the model questions.

### Test Plan:
Run the example, see it work.
2025-03-25 13:24:18 -04:00
Martín Bravo
57ecebfe35 Refactor visualization node label formatting in get_all_nodes function 2025-03-25 18:16:29 +01:00
Martín Bravo
48fad9e2d3 Merge branch 'main' of https://github.com/openai/openai-agents-python into feat/draw_graph 2025-03-25 18:02:51 +01:00
Martín Bravo
9d04671095 Rename visualization dependency to viz in pyproject.toml 2025-03-25 18:01:45 +01:00
Rohan Mehta
4f8cbfa676
[2/n] Add MCP support to Runner (#321)
### Summary:
This enables users to **use** MCP inside the SDK.
1. You add a list of MCP servers to `Agent`, via `mcp_server=[...]`
2. When an agent runs, we look up its MCP tools and add them to the list
of tools.
3. When a tool call occurs, we call the relevant MCP server.

Notes:
1. There's some refactoring to make sure we send the full list of tools
to the Runner/Model etc.
2. Right now, you could have a locally defined tool that conflicts with
an MCP defined tool. I didn't add errors for that, will do in a
followup.

### Test Plan:
See unit tests. Also has an end to end example next PR.

---
[//]: # (BEGIN SAPLING FOOTER)
* #324
* #322
* __->__ #321
* #320
2025-03-25 12:53:29 -04:00
Rohan Mehta
91feb7dd18
[1/n] Add MCP types to the SDK (#320)
### Summary:
1. Add the MCP dep for python 3.10, since it doesn't support 3.9 and
below
2. Create MCPServer, which is the agents SDK representation of an MCP
server
3. Create implementations for HTTP-SSE and StdIO servers, directly
copying the [MCP SDK
example](https://github.com/modelcontextprotocol/python-sdk/blob/main/examples/clients/simple-chatbot/mcp_simple_chatbot/main.py)
4. Add a util to transform MCP tools into Agent SDK tools

Note: I added optional caching support to the servers. That way, if you
happen to know a server's tools don't change, you can just cache them.

### Test Plan:

Checks pass. I added tests at the end of the stack.

--- 

#324
#322
#321
-> #320
#319
2025-03-25 12:53:10 -04:00
Rohan Mehta
97e3dc3c76 [1/n] Add MCP types to the SDK
### Summary:
1. Add the MCP dep for python 3.10, since it doesn't support 3.9 and below
2. Create MCPServer, which is the agents SDK representation of an MCP server
3. Create implementations for HTTP-SSE and StdIO servers, directly copying the [MCP SDK example](https://github.com/modelcontextprotocol/python-sdk/blob/main/examples/clients/simple-chatbot/mcp_simple_chatbot/main.py)
4. Add a util to transform MCP tools into Agent SDK tools

Note: I added optional caching support to the servers. That way, if you happen to know a server's tools don't change, you can just cache them.

### Test Plan:

Checks pass. I added tests at the end of the stack.
2025-03-25 12:51:40 -04:00
Martín Bravo
b3addcff13 Add visualization optional dependency for graphviz 2025-03-25 16:59:17 +01:00
Martín Bravo
900a97fa55 Merge branch 'main' of https://github.com/openai/openai-agents-python into feat/draw_graph 2025-03-25 16:58:01 +01:00
Rohan Mehta
923a3547eb
Fix parallel_tool_calls when False (#333)
Currently, when we set `parallel_tool_calls=False` in the
`model_settings`, the responses API is called with `parallel_tool_calls
== NotGiven`, which defaults to true on the Response API side
(https://platform.openai.com/docs/api-reference/responses/create#responses-create-parallel_tool_calls).

This PR attempts to fix that.

```
agent = Agent(
    ...,
    model_settings=ModelSettings(
        parallel_tool_calls=False,
    ),
)
```
2025-03-25 11:31:06 -04:00
Rohan Mehta
927a29c56b
Fix potential infinite tool call loop by resetting tool_choice after … (#263)
# Fix potential infinite tool call loop by resetting tool_choice after
tool execution

## Summary

This PR fixes an issue where setting `tool_choice` to "required" or a
specific function name could cause models to get stuck in an infinite
tool call loop.

When `tool_choice` is set to force tool usage, this setting persists
across model invocations. This PR automatically resets `tool_choice` to
"auto" after tool execution, allowing the model to decide whether to
make additional tool calls in subsequent turns.

Unlike using `tool_use_behavior="stop_on_first_tool"`, this approach
lets the model continue processing tool results while preventing forced
repeated tool calls.

## Test plan

- Added tests to verify tool_choice reset behavior for both agent and
run_config settings
- Added integration test to verify the solution prevents infinite loops
- All tests pass

## Checks

- [x] I've added new tests for the fix
- [x] I've updated the relevant documentation (added comment in code)
- [x] I've run `make lint` and `make format`
- [x] I've made sure tests pass
2025-03-25 11:30:53 -04:00
apeccaud
326ff09127 Fix parallel_tool_calls when False 2025-03-25 14:52:48 +00:00
Rohan Mehta
13abb6826d
[0/n] Only run tests on py3.9, not mypy (#319)
We don't really need mypy on 3.9 (unit tests would catch any real
issues), and it causes issues with the rest of this stack.


---
[//]: # (BEGIN SAPLING FOOTER)
* #324
* #322
* #321
* #320
* __->__ #319
2025-03-24 23:19:21 -04:00
Rohan Mehta
68c800d2a3 [2/n] Add MCP support to Runner
### Summary:
This enables users to **use** MCP inside the SDK.
1. You add a list of MCP servers to `Agent`, via `mcp_server=[...]`
2. When an agent runs, we look up its MCP tools and add them to the list of tools.
3. When a tool call occurs, we call the relevant MCP server.

Notes:
1. There's some refactoring to make sure we send the full list of tools to the Runner/Model etc.
2. Right now, you could have a locally defined tool that conflicts with an MCP defined tool. I didn't add errors for that, will do in a followup.

### Test Plan:
See unit tests. Also has an end to end example next PR.
2025-03-24 15:08:02 -04:00
Rohan Mehta
a7a6fe715f [0/n] Only run tests on py3.9, not mypy
### Summary:
We don't need to run mypy on 3.9 anyway. Also it causes issues with the rest of this stack.

### Test plan:

run checks
2025-03-24 15:08:02 -04:00
Rohan Mehta
300e12c198 [1/n] Add MCP types to the SDK
### Summary:
1. Add the MCP dep for python 3.10, since it doesn't support 3.9 and below
2. Create MCPServer, which is the agents SDK representation of an MCP server
3. Create implementations for HTTP-SSE and StdIO servers, directly copying the [MCP SDK example](https://github.com/modelcontextprotocol/python-sdk/blob/main/examples/clients/simple-chatbot/mcp_simple_chatbot/main.py)
4. Add a util to transform MCP tools into Agent SDK tools

Note: I added optional caching support to the servers. That way, if you happen to know a server's tools don't change, you can just cache them.

### Test Plan:

Checks pass. I added tests at the end of the stack.
2025-03-24 15:08:02 -04:00
Rohan Mehta
a34f34dc2e
chore: ignore PyCharm .idea/ directory (#317)
Uncomment .idea/ directory in .gitignore to ensure PyCharm IDE project
configuration files are excluded from version control.
2025-03-24 10:08:07 -04:00
madroid
e96e364f08
chore: ignore PyCharm .idea/ directory
Uncomment .idea/ directory in .gitignore to ensure PyCharm IDE
project configuration files are excluded from version control.
2025-03-24 18:30:14 +08:00
Martín Bravo
623063b633 refactor: clean up visualization functions by removing unused nodes and improving type hints 2025-03-24 09:47:21 +01:00
Martín Bravo
a5b7abe8b4 feat: enhance visualization functions with optional type hints and improved handling of agents and handoffs 2025-03-24 09:30:13 +01:00
Martín Bravo
9f7d596d14 feat: add optional dependency for visualization using Graphviz 2025-03-24 09:08:29 +01:00
Rohan Mehta
4cb011c4d2
Fix Typos (#258)
Noticed a bunch of typos when reading code, fixing.
2025-03-23 20:28:14 -04:00
Rohan Mehta
8282bc6369
Improve tracing error messages (#311)
1. Make it clear it's non-fatal
2. Make it clear it's a tracing error
2025-03-23 18:19:45 -04:00
Rohan Mehta
668fac0f74 Improve tracing error messages 2025-03-23 18:14:10 -04:00
Rohan Mehta
791a6f6812
Update quickstart.md 2025-03-23 17:56:55 -04:00
Rohan Mehta
ca49991fe8
Update issues.yml 2025-03-23 12:19:07 -04:00
xianghuijin
07a4af1fe2 refactor: improve comments for clarity in tool_choice reset logic 2025-03-23 20:41:18 +08:00
xianghuijin
0c747af743 refactor: streamline tool_choice reset logic
This update moves the tool_choice reset logic to a more appropriate location within the RunImpl class, ensuring that the original agent's model_settings remains unmodified during the reset process. The logic now checks for problematic scenarios before creating a modified copy of the agent's settings, maintaining expected behavior across sequential runs. This change enhances clarity and efficiency in handling tool choices.

Addresses previous feedback regarding the modification of the agent instance and improves the overall structure of the reset logic.
2025-03-23 20:04:39 +08:00