From ee7be3de59d9c2be5cba2c3021b4c18a461c00c2 Mon Sep 17 00:00:00 2001 From: Kazuhiro Sera Date: Tue, 8 Jul 2025 20:44:33 +0900 Subject: [PATCH] Fix #968 by upgrading openai package to the latest --- pyproject.toml | 2 +- src/agents/model_settings.py | 7 +++++-- src/agents/models/chatcmpl_converter.py | 5 ++++- src/agents/models/openai_responses.py | 15 +++++++++++---- tests/model_settings/test_serialization.py | 13 ++++++++++++- tests/test_items_helpers.py | 13 +++++++++++-- tests/test_run_step_processing.py | 8 +++++++- uv.lock | 8 ++++---- 8 files changed, 55 insertions(+), 16 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 44bc45947..dfceb88f9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -7,7 +7,7 @@ requires-python = ">=3.9" license = "MIT" authors = [{ name = "OpenAI", email = "support@openai.com" }] dependencies = [ - "openai>=1.87.0", + "openai>=1.93.1, <2", "pydantic>=2.10, <3", "griffe>=1.5.6, <2", "typing-extensions>=4.12.2, <5", diff --git a/src/agents/model_settings.py b/src/agents/model_settings.py index 6f39a968b..1e9edcbc6 100644 --- a/src/agents/model_settings.py +++ b/src/agents/model_settings.py @@ -42,11 +42,14 @@ def validate_from_none(value: None) -> _Omit: serialization=core_schema.plain_serializer_function_ser_schema(lambda instance: None), ) +@dataclass +class MCPToolChoice: + server_label: str + name: str Omit = Annotated[_Omit, _OmitTypeAnnotation] Headers: TypeAlias = Mapping[str, Union[str, Omit]] -ToolChoice: TypeAlias = Union[Literal["auto", "required", "none"], str, None] - +ToolChoice: TypeAlias = Union[Literal["auto", "required", "none"], str, MCPToolChoice, None] @dataclass class ModelSettings: diff --git a/src/agents/models/chatcmpl_converter.py b/src/agents/models/chatcmpl_converter.py index 9d0c6cf5e..d3c71c24e 100644 --- a/src/agents/models/chatcmpl_converter.py +++ b/src/agents/models/chatcmpl_converter.py @@ -44,6 +44,7 @@ from ..exceptions import AgentsException, UserError from ..handoffs import Handoff from ..items import TResponseInputItem, TResponseOutputItem +from ..model_settings import MCPToolChoice from ..tool import FunctionTool, Tool from .fake_id import FAKE_RESPONSES_ID @@ -51,10 +52,12 @@ class Converter: @classmethod def convert_tool_choice( - cls, tool_choice: Literal["auto", "required", "none"] | str | None + cls, tool_choice: Literal["auto", "required", "none"] | str | MCPToolChoice | None ) -> ChatCompletionToolChoiceOptionParam | NotGiven: if tool_choice is None: return NOT_GIVEN + elif isinstance(tool_choice, MCPToolChoice): + raise UserError("MCPToolChoice is not supported for Chat Completions models") elif tool_choice == "auto": return "auto" elif tool_choice == "required": diff --git a/src/agents/models/openai_responses.py b/src/agents/models/openai_responses.py index a7ce62983..d25613aee 100644 --- a/src/agents/models/openai_responses.py +++ b/src/agents/models/openai_responses.py @@ -25,6 +25,7 @@ from ..handoffs import Handoff from ..items import ItemHelpers, ModelResponse, TResponseInputItem from ..logger import logger +from ..model_settings import MCPToolChoice from ..tool import ( CodeInterpreterTool, ComputerTool, @@ -303,10 +304,16 @@ class ConvertedTools: class Converter: @classmethod def convert_tool_choice( - cls, tool_choice: Literal["auto", "required", "none"] | str | None + cls, tool_choice: Literal["auto", "required", "none"] | str | MCPToolChoice | None ) -> response_create_params.ToolChoice | NotGiven: if tool_choice is None: return NOT_GIVEN + elif isinstance(tool_choice, MCPToolChoice): + return { + "server_label": tool_choice.server_label, + "type": "mcp", + "name": tool_choice.name, + } elif tool_choice == "required": return "required" elif tool_choice == "auto": @@ -334,9 +341,9 @@ def convert_tool_choice( "type": "code_interpreter", } elif tool_choice == "mcp": - return { - "type": "mcp", - } + # Note that this is still here for backwards compatibility, + # but migrating to MCPToolChoice is recommended. + return { "type": "mcp" } # type: ignore [typeddict-item] else: return { "type": "function", diff --git a/tests/model_settings/test_serialization.py b/tests/model_settings/test_serialization.py index 23ea5359c..6e8c65180 100644 --- a/tests/model_settings/test_serialization.py +++ b/tests/model_settings/test_serialization.py @@ -5,7 +5,7 @@ from pydantic import TypeAdapter from pydantic_core import to_json -from agents.model_settings import ModelSettings +from agents.model_settings import MCPToolChoice, ModelSettings def verify_serialization(model_settings: ModelSettings) -> None: @@ -29,6 +29,17 @@ def test_basic_serialization() -> None: verify_serialization(model_settings) +def test_mcp_tool_choice_serialization() -> None: + """Tests whether ModelSettings with MCPToolChoice can be serialized to a JSON string.""" + # First, lets create a ModelSettings instance + model_settings = ModelSettings( + temperature=0.5, + tool_choice=MCPToolChoice(server_label="mcp", name="mcp_tool"), + ) + # Now, lets serialize the ModelSettings instance to a JSON string + verify_serialization(model_settings) + + def test_all_fields_serialization() -> None: """Tests whether ModelSettings can be serialized to a JSON string.""" diff --git a/tests/test_items_helpers.py b/tests/test_items_helpers.py index 5dba21d88..f711f21e1 100644 --- a/tests/test_items_helpers.py +++ b/tests/test_items_helpers.py @@ -11,7 +11,10 @@ ) from openai.types.responses.response_function_tool_call import ResponseFunctionToolCall from openai.types.responses.response_function_tool_call_param import ResponseFunctionToolCallParam -from openai.types.responses.response_function_web_search import ResponseFunctionWebSearch +from openai.types.responses.response_function_web_search import ( + ActionSearch, + ResponseFunctionWebSearch, +) from openai.types.responses.response_function_web_search_param import ResponseFunctionWebSearchParam from openai.types.responses.response_output_message import ResponseOutputMessage from openai.types.responses.response_output_message_param import ResponseOutputMessageParam @@ -225,7 +228,12 @@ def test_to_input_items_for_file_search_call() -> None: def test_to_input_items_for_web_search_call() -> None: """A web search tool call output should produce the same dict as a web search input.""" - ws_call = ResponseFunctionWebSearch(id="w1", status="completed", type="web_search_call") + ws_call = ResponseFunctionWebSearch( + id="w1", + action=ActionSearch(type="search", query="query"), + status="completed", + type="web_search_call", + ) resp = ModelResponse(output=[ws_call], usage=Usage(), response_id=None) input_items = resp.to_input_items() assert isinstance(input_items, list) and len(input_items) == 1 @@ -233,6 +241,7 @@ def test_to_input_items_for_web_search_call() -> None: "id": "w1", "status": "completed", "type": "web_search_call", + "action": {"type": "search", "query": "query"}, } assert input_items[0] == expected diff --git a/tests/test_run_step_processing.py b/tests/test_run_step_processing.py index 6a2904791..27d36afa8 100644 --- a/tests/test_run_step_processing.py +++ b/tests/test_run_step_processing.py @@ -7,6 +7,7 @@ ResponseFunctionWebSearch, ) from openai.types.responses.response_computer_tool_call import ActionClick +from openai.types.responses.response_function_web_search import ActionSearch from openai.types.responses.response_reasoning_item import ResponseReasoningItem, Summary from pydantic import BaseModel @@ -306,7 +307,12 @@ async def test_file_search_tool_call_parsed_correctly(): @pytest.mark.asyncio async def test_function_web_search_tool_call_parsed_correctly(): agent = Agent(name="test") - web_search_call = ResponseFunctionWebSearch(id="w1", status="completed", type="web_search_call") + web_search_call = ResponseFunctionWebSearch( + id="w1", + action=ActionSearch(type="search", query="query"), + status="completed", + type="web_search_call", + ) response = ModelResponse( output=[get_text_message("hello"), web_search_call], usage=Usage(), diff --git a/uv.lock b/uv.lock index 880116f58..c6d04a424 100644 --- a/uv.lock +++ b/uv.lock @@ -1461,7 +1461,7 @@ wheels = [ [[package]] name = "openai" -version = "1.87.0" +version = "1.93.1" source = { registry = "https://pypi.org/simple" } dependencies = [ { name = "anyio" }, @@ -1473,9 +1473,9 @@ dependencies = [ { name = "tqdm" }, { name = "typing-extensions" }, ] -sdist = { url = "https://files.pythonhosted.org/packages/47/ed/2b3f6c7e950784e9442115ab8ebeff514d543fb33da10607b39364645a75/openai-1.87.0.tar.gz", hash = "sha256:5c69764171e0db9ef993e7a4d8a01fd8ff1026b66f8bdd005b9461782b6e7dfc", size = 470880, upload-time = "2025-06-16T19:04:26.316Z" } +sdist = { url = "https://files.pythonhosted.org/packages/5e/a8/e4427729da048cb33bda15e70f09f7520bdf3577bafc546b135ecb36af7d/openai-1.93.1.tar.gz", hash = "sha256:11eb8932965d0f79ecc4cb38a60a0c4cef4bcd5fcf08b99fc9a399fa5f1e50ab", size = 487124, upload-time = "2025-07-07T16:40:38.389Z" } wheels = [ - { url = "https://files.pythonhosted.org/packages/36/ac/313ded47ce1d5bc2ec02ed5dd5506bf5718678a4655ac20f337231d9aae3/openai-1.87.0-py3-none-any.whl", hash = "sha256:f9bcae02ac4fff6522276eee85d33047335cfb692b863bd8261353ce4ada5692", size = 734368, upload-time = "2025-06-16T19:04:23.181Z" }, + { url = "https://files.pythonhosted.org/packages/64/4f/875e5af1fb4e5ed4ea9e4a88f482d9ca2e48932105605b6c516e9a14de25/openai-1.93.1-py3-none-any.whl", hash = "sha256:a2c2946c4f21346d4902311a7440381fd8a33466ee7ca688133d1cad29a9357c", size = 755081, upload-time = "2025-07-07T16:40:36.585Z" }, ] [[package]] @@ -1539,7 +1539,7 @@ requires-dist = [ { name = "litellm", marker = "extra == 'litellm'", specifier = ">=1.67.4.post1,<2" }, { name = "mcp", marker = "python_full_version >= '3.10'", specifier = ">=1.9.4,<2" }, { name = "numpy", marker = "python_full_version >= '3.10' and extra == 'voice'", specifier = ">=2.2.0,<3" }, - { name = "openai", specifier = ">=1.87.0" }, + { name = "openai", specifier = ">=1.93.1,<2" }, { name = "pydantic", specifier = ">=2.10,<3" }, { name = "requests", specifier = ">=2.0,<3" }, { name = "types-requests", specifier = ">=2.0,<3" },