Commit Graph

64 Commits

Author SHA1 Message Date
Rohan Mehta 944b730da9 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 a93ad02688 [4/n] Add docs for MCP
Just adding docs.
-
2025-03-25 13:25:50 -04:00
Rohan Mehta f5eb773663 [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 f28848e850 [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
Rohan Mehta 03e1e00023 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 69907b8eed 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 fc31f992f5 Fix parallel_tool_calls when False 2025-03-25 14:52:48 +00:00
Rohan Mehta e71d2b6fd7 [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 1e3d2c7307 [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 21a61969d9 Fix Typos (#258)
Noticed a bunch of typos when reading code, fixing.
2025-03-23 20:28:14 -04:00
Rohan Mehta 49514f2859 Improve tracing error messages 2025-03-23 18:14:10 -04:00
xianghuijin 93462b0f76 refactor: improve comments for clarity in tool_choice reset logic 2025-03-23 20:41:18 +08:00
xianghuijin 02e329526f 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 2a61d474e4 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 0746325a67 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
Raduan77 db2de55dff revert src/ change per request 2025-03-22 01:06:01 +01:00
Rohan Mehta 9e9ad165ab Read tracing API data lazily 2025-03-21 15:32:37 -04:00
Richie Caputo 353d5eab77 Create py.typed
- Ensure library is properly typehinted
2025-03-21 11:16:24 -04:00
Kazuhiro Sera 4bc92307d8 Make the optional dependency installation compatible with zsh 2025-03-21 10:01:24 +09:00
Rohan Mehta ec66b71419 update tests 2025-03-20 13:08:38 -04:00
Dominik Kundel 20f0793862 feat: add voice pipeline support
> Co-authored-by: rm@openai.com
2025-03-20 09:43:13 -07:00
xianghuijin 20271a3591 Fix potential infinite tool call loop by resetting tool_choice after tool execution 2025-03-20 21:22:27 +08:00
Raduan77 e172dd8891 fix typos in src/ 2025-03-20 11:24:08 +01:00
Rohan Mehta e7caa14c30 Fix breaking changes from openai 1.66.2 2025-03-19 12:30:45 -04:00
Rohan Mehta c91db09d3c Introduce tool_use_behavior on agents 2025-03-18 21:55:12 -04:00
Rohan Mehta 8b643537a0 Update tests and docs for strict mode decorator (#205)
As titled. Test plan: unit tests/docs.
2025-03-18 21:35:00 -04:00
Rohan Mehta 7253030b0c Fix typo on Agent documentation (#151)
Argument name is not description but handoff_description
2025-03-17 16:51:47 -04:00
Carlos Souza 268ea2f80d Fix lint 2025-03-17 16:14:28 -04:00
Rohan Mehta ecf4f92801 Add TracingProcessor export to __init__.py (#192)
# Summary
This adds the missing TracingProcessor export to  __init__.py.

# Behavior
When trying to add a custom tracing processor, the TracingProcessor
importing fails with not found error when trying the example usage
proposed in issue #164

Specifically this line throws the error:
`add_trace_processor(MyTracingProcessor("output"))`

# Expected Behavior
Inspecting the init file, simply the import/export was missing. Adding
these made the example code work for me

# Test plan
Local dev of example code in #164

# Issue number
#164

# Checks
None
2025-03-17 15:39:04 -04:00
Rohan Mehta 8d14695f51 Update tests and docs for strict mode decorator 2025-03-17 15:06:57 -04:00
Rohan Mehta 417cdc5efe Pretty print result classes 2025-03-17 11:11:39 -04:00
Vincenzo Domina f6e59277b1 Add TracingProcessor export to __init__.py 2025-03-17 10:37:43 +01:00
Rohan Mehta 4719471cc3 utils directory 2025-03-16 18:48:45 -04:00
Rohan Mehta d60a413c14 feat: Add strict_mode option to function_schema and function_tool (#60)
This PR introduces a `strict_mode: bool = True` option to
`@function_tool`, allowing optional parameters when set to False. This
change enables more flexibility while maintaining strict JSON schema
validation by default.

resolves #43 

## Changes:

- Added `strict_mode` parameter to `@function_tool` and passed it to
`function_schema` and `FunctionTool`.
- Updated `function_schema.py` to respect `strict_mode` and allow
optional parameters when set to False.
- Added unit tests to verify optional parameters work correctly,
including multiple optional params with different types.

## Tests:

- Verified function calls with missing optional parameters behave as
expected.
- Added async tests to validate behavior under different configurations.
2025-03-16 17:43:46 -04:00
Carlos Souza d6e4bd12e0 Fix typo on Agent documentation
Argument name is not description but handoff_description
2025-03-13 16:18:40 -04:00
Rohan Mehta 94792b7d0d Consolidate to one logger (#149)
Results in double logging if we dont. Closes #141.
2025-03-13 11:38:18 -07:00
Rohan Mehta e863ef255d Consolidate to one logger 2025-03-13 13:55:15 -04:00
Rohan Mehta 83503d85a6 Update tracing docs to be correct 2025-03-13 13:20:27 -04:00
Rohan Mehta b2dc3e7ac9 Add examples and documentation for using custom model providers (#110) 2025-03-13 08:17:25 -07:00
Rohan Mehta 9428ed0c67 Add request ID 2025-03-12 18:07:59 -07:00
Rohan Mehta e881d72d38 Add examples and documentation for using custom model providers 2025-03-12 17:34:38 -07:00
Rohan Mehta a9f717b220 Fix streaming in chat completions 2025-03-12 17:17:07 -07:00
Rohan Mehta ca8a5bea15 Merge pull request #105 from openai/pr105
Add max_tokens and documentation to model settings
2025-03-12 13:45:21 -07:00
Rohan Mehta d737ad0e1b Add max_tokens and documentation to model settings 2025-03-12 13:35:10 -07:00
Rohan Mehta 94bdad6291 Merge pull request #89 from eltociear/patch-1
chore: update guardrail.py
2025-03-12 13:30:56 -07:00
Rohan Mehta 4f6276c8c7 Remove duplicated code 2025-03-12 13:08:00 -07:00
Muhammad Junaid b5bbc060d3 fix: support assistant role in message conversion
- The _Converter.items_to_messages method was incorrectly rejecting 'assistant'
as a valid role in conversation messages, causing runtime errors when processing
standard chat completion message formats.
- This fix enables proper handling of
complete conversation contexts that include both user and assistant messages.
2025-03-12 21:10:03 +05:00
Ikko Eltociear Ashimine cafa77fc75 chore: update guardrail.py
minor fix
2025-03-12 22:45:17 +09:00
Jai0401 644d8afa1b fix: resolve linting issues 2025-03-12 16:00:20 +05:30
Jaimin Godhani 7715cb0d5e Update src/agents/tool.py
Co-authored-by: Adrian Cole <64215+codefromthecrypt@users.noreply.github.com>
2025-03-12 14:56:19 +05:30