Commit graph

440 commits

Author SHA1 Message Date
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
xianghuijin
6ed0bee672 fix: prevent modifying the original agent's model_settings
This fixes the issue where the original agent's model_settings was being directly modified during the tool choice reset process. The original implementation caused the agent's tool_choice to unintentionally reset to "auto" for subsequent runs, which could be unexpected behavior.

The fix creates new copies of the agent and model settings objects using dataclasses.replace() instead of modifying the original objects. This ensures that the tool choice reset is limited to the current run only, maintaining the expected behavior for sequential runs with the same agent.

Addresses feedback from @baderalfahad about the agent instance being modified when it should maintain its original state between runs.
2025-03-23 17:20:23 +08:00
xianghuijin
8f2f76cb65 docs: Update tool_choice reset documentation to match implementation 2025-03-22 14:22:47 +08:00
xianghuijin
bbcda753df fix: optimize tool_choice reset logic and fix lint errors
- Refactor tool_choice reset to target only problematic edge cases
- Replace manual ModelSettings recreation with dataclasses.replace
- Fix line length and error handling lint issues in tests
2025-03-22 14:10:09 +08:00
Rohan Mehta
8d906f88f0
Fix code examples in voice pipeline quickstart docs (#277) 2025-03-21 21:44:14 -04:00
Raduan77
ab0d940f19 revert src/ change per request 2025-03-22 01:06:01 +01:00
Rohan Mehta
486b4e87e5
[docs] fix annotation numbering in context management (#293)
Fix inconsistent numbering between code and explanatory annotations in
the context management documentation.
2025-03-21 19:33:30 -04:00
Rohan Mehta
136e2b49ba
Fix circular dependency in voice streamed example by renaming agents #291 (#292)
Fix circular dependency in voice streamed example by renaming agents.py
to my_workflow.py

Fix circular dependency in voice streamed example by renaming agents
#291
2025-03-21 19:33:12 -04:00
Aviral Garg
fdf340495b Fix circular dependency in voice streamed example with cleaner import pattern 2025-03-21 16:26:19 -07:00
Aviral Garg
9473c788ba Fix type-checking for circular dependency in voice streamed example 2025-03-21 16:18:04 -07:00
Rohan Mehta
2439032f4c
Add Weights & Biases to tracing docs (#294) 2025-03-21 17:59:54 -04:00
Rohan Mehta
f4f0b85eb5
Read tracing API data lazily (#289) 2025-03-21 17:58:10 -04:00
Scott Condron
13eca63732
Add Weights & Biases to tracing docs 2025-03-21 21:37:54 +00:00
Han Hwang Lim
d56047be51 fix annotation numbering in context management
Fix inconsistent numbering between code and explanatory annotations in
the context management documentation.
2025-03-21 21:10:30 +00:00
Aviral Garg
7432347a94 Fix circular dependency in voice streamed example by renaming agents.py to my_workflow.py 2025-03-21 13:25:46 -07:00
Dmitry Pimenov
211ffb28c5
fix line in guardrails doc (#290) 2025-03-21 13:00:18 -07:00
James Hills
fa1c3f40a1
fix line in guardrails 2025-03-21 15:58:34 -04:00
Rohan Mehta
c0794a90ec Read tracing API data lazily 2025-03-21 15:32:37 -04:00
Rohan Mehta
090e79bdf4
Replace remaining uses of fetch_ordered_spans and fetch_traces for stronger tests (#288)
Following https://github.com/openai/openai-agents-python/pull/261
2025-03-21 13:25:43 -04:00
Alex Hall
dacbb9ba44 More fetch_normalized_spans 2025-03-21 18:31:06 +02:00
Alex Hall
6b509e33f6 empty assertions 2025-03-21 18:26:04 +02:00
Rohan Mehta
1d3d1ce3f7
Create py.typed (#285)
- Ensure library is properly typehinted
2025-03-21 12:19:48 -04:00