From 80914613b164a93dadb1a907cf484288d2074090 Mon Sep 17 00:00:00 2001 From: Kazuhiro Sera Date: Fri, 25 Jul 2025 18:18:04 +0900 Subject: [PATCH] Fix #1227 Chat Completions model fails to handle streaming with openai 1.97.1+ --- pyproject.toml | 2 +- src/agents/models/chatcmpl_stream_handler.py | 25 +++++++++++--------- tests/voice/test_workflow.py | 1 + uv.lock | 8 +++---- 4 files changed, 20 insertions(+), 16 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index f21040b45..c2216a6af 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.96.1, <2", + "openai>=1.97.1,<2", "pydantic>=2.10, <3", "griffe>=1.5.6, <2", "typing-extensions>=4.12.2, <5", diff --git a/src/agents/models/chatcmpl_stream_handler.py b/src/agents/models/chatcmpl_stream_handler.py index eab4c291b..3c3ec06bb 100644 --- a/src/agents/models/chatcmpl_stream_handler.py +++ b/src/agents/models/chatcmpl_stream_handler.py @@ -198,6 +198,7 @@ async def handle_stream( is not None, # fixed 0 -> 0 or 1 type="response.output_text.delta", sequence_number=sequence_number.get_and_increment(), + logprobs=[], ) # Accumulate the text into the response part state.text_content_index_and_output[1].text += delta.content @@ -288,10 +289,11 @@ async def handle_stream( function_call = state.function_calls[tc_delta.index] # Start streaming as soon as we have function name and call_id - if (not state.function_call_streaming[tc_delta.index] and - function_call.name and - function_call.call_id): - + if ( + not state.function_call_streaming[tc_delta.index] + and function_call.name + and function_call.call_id + ): # Calculate the output index for this function call function_call_starting_index = 0 if state.reasoning_content_index_and_output: @@ -308,9 +310,9 @@ async def handle_stream( # Mark this function call as streaming and store its output index state.function_call_streaming[tc_delta.index] = True - state.function_call_output_idx[ - tc_delta.index - ] = function_call_starting_index + state.function_call_output_idx[tc_delta.index] = ( + function_call_starting_index + ) # Send initial function call added event yield ResponseOutputItemAddedEvent( @@ -327,10 +329,11 @@ async def handle_stream( ) # Stream arguments if we've started streaming this function call - if (state.function_call_streaming.get(tc_delta.index, False) and - tc_function and - tc_function.arguments): - + if ( + state.function_call_streaming.get(tc_delta.index, False) + and tc_function + and tc_function.arguments + ): output_index = state.function_call_output_idx[tc_delta.index] yield ResponseFunctionCallArgumentsDeltaEvent( delta=tc_function.arguments, diff --git a/tests/voice/test_workflow.py b/tests/voice/test_workflow.py index e99810be1..611e6f255 100644 --- a/tests/voice/test_workflow.py +++ b/tests/voice/test_workflow.py @@ -86,6 +86,7 @@ async def stream_response( output_index=0, item_id=item.id, sequence_number=0, + logprobs=[], ) yield ResponseCompletedEvent( diff --git a/uv.lock b/uv.lock index e24040d4a..5c7c0b5de 100644 --- a/uv.lock +++ b/uv.lock @@ -1463,7 +1463,7 @@ wheels = [ [[package]] name = "openai" -version = "1.96.1" +version = "1.97.1" source = { registry = "https://pypi.org/simple" } dependencies = [ { name = "anyio" }, @@ -1475,9 +1475,9 @@ dependencies = [ { name = "tqdm" }, { name = "typing-extensions" }, ] -sdist = { url = "https://files.pythonhosted.org/packages/2f/b5/18fd5e1b6b6c7dca52d60307b3637f9e9e3206a8041a9c8028985dbc6260/openai-1.96.1.tar.gz", hash = "sha256:6d505b5cc550e036bfa3fe99d6cff565b11491d12378d4c353f92ef72b0a408a", size = 489065, upload-time = "2025-07-15T21:39:37.215Z" } +sdist = { url = "https://files.pythonhosted.org/packages/a6/57/1c471f6b3efb879d26686d31582997615e969f3bb4458111c9705e56332e/openai-1.97.1.tar.gz", hash = "sha256:a744b27ae624e3d4135225da9b1c89c107a2a7e5bc4c93e5b7b5214772ce7a4e", size = 494267, upload-time = "2025-07-22T13:10:12.607Z" } wheels = [ - { url = "https://files.pythonhosted.org/packages/4f/57/325bbdbdc27b47309be35cb4e0eb8980b0c1bc997194c797c3691d88ae41/openai-1.96.1-py3-none-any.whl", hash = "sha256:0afaab2019bae8e145e7a1baf6953167084f019dd15042c65edd117398c1eb1c", size = 757454, upload-time = "2025-07-15T21:39:34.517Z" }, + { url = "https://files.pythonhosted.org/packages/ee/35/412a0e9c3f0d37c94ed764b8ac7adae2d834dbd20e69f6aca582118e0f55/openai-1.97.1-py3-none-any.whl", hash = "sha256:4e96bbdf672ec3d44968c9ea39d2c375891db1acc1794668d8149d5fa6000606", size = 764380, upload-time = "2025-07-22T13:10:10.689Z" }, ] [[package]] @@ -1541,7 +1541,7 @@ requires-dist = [ { name = "litellm", marker = "extra == 'litellm'", specifier = ">=1.67.4.post1,<2" }, { name = "mcp", marker = "python_full_version >= '3.10'", specifier = ">=1.11.0,<2" }, { name = "numpy", marker = "python_full_version >= '3.10' and extra == 'voice'", specifier = ">=2.2.0,<3" }, - { name = "openai", specifier = ">=1.96.1,<2" }, + { name = "openai", specifier = ">=1.97.1,<2" }, { name = "pydantic", specifier = ">=2.10,<3" }, { name = "requests", specifier = ">=2.0,<3" }, { name = "types-requests", specifier = ">=2.0,<3" },