Commit Graph

66 Commits

Author SHA1 Message Date
pakrym-oai 0fa5849839 Mark handoff span as errored when multiple handoffs are requested (#344)
Also includes the set of requested agents in the error data.
<img width="968" alt="image"
src="https://github.com/user-attachments/assets/0c5c2e81-08f7-445c-bbb0-3e169ef744a5"
/>
2025-03-26 10:44:59 -04:00
Rohan Mehta b870b90471 [5/n] MCP tracing (#342)
## 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:54:28 -04:00
Rohan Mehta 6cb8b1ceaa [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 4c93a5d9b1 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
Martín Bravo dc3093bab3 Refactor visualization functions to improve formatting and streamline edge generation 2025-03-25 19:12:40 +01:00
Martín Bravo b5f6544f6f Add start and end nodes to graph visualization and update edge generation 2025-03-25 19:11:43 +01:00
Martín Bravo c988fd4aaf Add graphviz as a dependency and update import statements 2025-03-25 18:46:23 +01:00
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
Martín Bravo 692dfe0327 Merge branch 'main' of https://github.com/openai/openai-agents-python into feat/draw_graph 2025-03-25 18:02:51 +01: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
Martín Bravo 1ec5c2e233 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 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
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
Martín Bravo 62a50266eb refactor: clean up visualization functions by removing unused nodes and improving type hints 2025-03-24 09:47:21 +01:00
Rohan Mehta 21a61969d9 Fix Typos (#258)
Noticed a bunch of typos when reading code, fixing.
2025-03-23 20:28:14 -04: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
Rohan Mehta 9e9ad165ab Read tracing API data lazily 2025-03-21 15:32:37 -04:00
Alex Hall e16c38a6e3 More fetch_normalized_spans 2025-03-21 18:31:06 +02:00
Alex Hall b7df4e470e empty assertions 2025-03-21 18:26:04 +02:00
Alex Hall 99d6bc5199 More fetch_normalized_spans 2025-03-21 18:15:52 +02:00
Alex Hall 0d0f35787f More fetch_normalized_spans 2025-03-21 18:14:59 +02:00
Alex Hall b1cfcfd18b More fetch_normalized_spans 2025-03-21 18:13:04 +02:00
Alex Hall 5cea0dffc6 More fetch_normalized_spans 2025-03-21 18:09:19 +02:00
Alex Hall e9611e22ca Merge branch 'main' of github.com:openai/openai-agents-python into alex/cleanup-tests 2025-03-21 10:13:33 +02:00
Rohan Mehta ec66b71419 update tests 2025-03-20 13:08:38 -04:00
Dominik Kundel f476521c53 fix tests 2025-03-20 09:52:15 -07:00
Dominik Kundel 20f0793862 feat: add voice pipeline support
> Co-authored-by: rm@openai.com
2025-03-20 09:43:13 -07:00
xianghuijin eaafad41ca Revert test file name to match project naming style 2025-03-20 21:28:24 +08:00
xianghuijin 29f39313a3 Rename test file for better clarity 2025-03-20 21:26:59 +08:00
xianghuijin 20271a3591 Fix potential infinite tool call loop by resetting tool_choice after tool execution 2025-03-20 21:22:27 +08:00
Alex Hall da4122975c lint 2025-03-20 13:56:11 +02:00
Alex Hall 8ff2f3a72c Remove redundant weaker tracing assertions 2025-03-20 13:49:38 +02:00
Raduan77 5ee8c79c51 fix typos in tests 2025-03-20 11:24:15 +01: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
Martín Bravo 4a477780a1 style: improve code formatting and readability in visualization functions 2025-03-18 10:09:44 +01:00
Martín Bravo 4012155926 rename: test_visualization.py 2025-03-18 09:59:47 +01:00
Martín Bravo c7805669bc feat: add visualization functions for agent graphs 2025-03-18 09:53:48 +01:00
Martín Bravo dba2d4bd23 Merge branch 'main' of https://github.com/openai/openai-agents-python into feat/draw_graph 2025-03-17 23:50:52 +01:00
Alex Hall 09279d164e mypy 2025-03-17 23:56:42 +02:00
Alex Hall 3ff9238dae Merge branch 'main' of github.com:openai/openai-agents-python into alex/inline-snapshot 2025-03-17 23:55:56 +02: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
MartinEBravo 4dc9b6712e Merge branch 'main' of https://github.com/openai/openai-agents-python into feat/draw_graph 2025-03-17 10:17:36 +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
Alex Hall 8722f91a59 Merge branch 'main' of github.com:openai/openai-agents-python into alex/inline-snapshot 2025-03-14 14:16:34 +02:00
Martín Bravo ed7bad55ee Linting 2025-03-13 18:49:12 +01:00
Martín Bravo 2f3875562b Add unit tests for visualization functions in test_visualizations.py 2025-03-13 18:33:17 +01:00