Skip to content

Commit 436bc15

Browse files
committed
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.
1 parent 03dca68 commit 436bc15

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/agents/models/chatcmpl_stream_handler.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,12 @@ async def handle_stream(
150150
)
151151

152152
if reasoning_content and state.reasoning_content_index_and_output:
153+
# Ensure summary list has at least one element
154+
if not state.reasoning_content_index_and_output[1].summary:
155+
state.reasoning_content_index_and_output[1].summary = [
156+
Summary(text="", type="summary_text")
157+
]
158+
153159
yield ResponseReasoningSummaryTextDeltaEvent(
154160
delta=reasoning_content,
155161
item_id=FAKE_RESPONSES_ID,
@@ -201,7 +207,7 @@ async def handle_stream(
201207
)
202208

203209
# Create a new summary with updated text
204-
if state.reasoning_content_index_and_output[1].content is None:
210+
if not state.reasoning_content_index_and_output[1].content:
205211
state.reasoning_content_index_and_output[1].content = [
206212
Content(text="", type="reasoning_text")
207213
]

0 commit comments

Comments
 (0)