From 436bc15f5d7917a6616ba1149f82750276072531 Mon Sep 17 00:00:00 2001 From: Lucas Wang Date: Thu, 23 Oct 2025 02:55:10 +0800 Subject: [PATCH] Fix: Add empty checks for reasoning content arrays in stream handler Add defensive checks before accessing array elements in reasoning content processing to prevent IndexError when arrays are empty. Changes: - Line 154: Check summary array is non-empty before accessing summary[0] - Line 204: Change 'is None' to truthiness check to handle both None and [] This prevents crashes when 3rd-party APIs initialize with empty arrays and later receive content in OpenAI format. --- src/agents/models/chatcmpl_stream_handler.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/agents/models/chatcmpl_stream_handler.py b/src/agents/models/chatcmpl_stream_handler.py index 474bffe09..5faf06c14 100644 --- a/src/agents/models/chatcmpl_stream_handler.py +++ b/src/agents/models/chatcmpl_stream_handler.py @@ -150,6 +150,12 @@ async def handle_stream( ) if reasoning_content and state.reasoning_content_index_and_output: + # Ensure summary list has at least one element + if not state.reasoning_content_index_and_output[1].summary: + state.reasoning_content_index_and_output[1].summary = [ + Summary(text="", type="summary_text") + ] + yield ResponseReasoningSummaryTextDeltaEvent( delta=reasoning_content, item_id=FAKE_RESPONSES_ID, @@ -201,7 +207,7 @@ async def handle_stream( ) # Create a new summary with updated text - if state.reasoning_content_index_and_output[1].content is None: + if not state.reasoning_content_index_and_output[1].content: state.reasoning_content_index_and_output[1].content = [ Content(text="", type="reasoning_text") ]