Fix: Add empty checks for reasoning content arrays in stream handler#1982
Merged
seratch merged 2 commits intoopenai:mainfrom Oct 23, 2025
Merged
Conversation
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.
There was a problem hiding this comment.
Pull Request Overview
This PR adds defensive checks before accessing array elements in the stream handler to prevent IndexError when reasoning content arrays are empty, addressing potential edge cases where different API providers may initialize arrays differently.
Key Changes:
- Added validation to ensure
summaryarray is non-empty before accessingsummary[0] - Modified
contentarray check fromis Nonetonot contentto handle both None and empty array cases
Comments suppressed due to low confidence (2)
src/agents/models/chatcmpl_stream_handler.py:169
- The defensive check added at line 154-157 protects against empty
summaryarrays, but there's no test coverage verifying this edge case. Consider adding a test that simulates a scenario wherereasoning_content_index_and_output[1].summaryis initialized as an empty list to ensure the defensive check works as intended.
current_content = state.reasoning_content_index_and_output[1].summary[0]
src/agents/models/chatcmpl_stream_handler.py:214
- The change at line 210 from checking
is Nonetonot contentnow handles empty arrays, but there's no test coverage for this scenario. Add a test case that verifies the behavior whenreasoning_content_index_and_output[1].contentis an empty list to ensure the fix prevents the IndexError described in the PR.
current_text = state.reasoning_content_index_and_output[1].content[0]
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
seratch
reviewed
Oct 23, 2025
seratch
approved these changes
Oct 23, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds defensive checks before accessing array elements in
chatcmpl_stream_handler.pyto preventIndexErrorwhen reasoning content arrays are empty.Problem Analysis
Discovered During Code Review
While scanning the codebase for potential bugs, we identified two locations where array elements are accessed without verifying the array is non-empty:
Line 163 (accessing
summary[0]):Line 208 (accessing
content[0]):Root Cause
The code handles two different reasoning formats:
OpenAI Format (line 128):
3rd Party Format (line 176):
Potential Issue:
delta.reasoningfirst → initializes withsummary=[], content=[...]delta.reasoning_content→ tries to accesssummary[0]→IndexErrorTrigger Probability: Low (same model unlikely to mix formats), but possible with:
Solution
Add defensive checks before array access (similar to PR #935 pattern):
Change 1: Line 154 - Check
summaryarrayChange 2: Line 204 - Check
contentarrayWhy This Fix is Correct
Defensive Programming Best Practice
Following the same philosophy as PR #935:
Maintains Existing Behavior
Low Risk
Testing
Existing Tests Pass
Type Safety
Code Quality
$ make format $ make lint All checks passed!Impact
Affected Scenarios:
Risk Assessment:
Research Process
This fix was discovered through:
Related Work
Note: This PR demonstrates proactive bug prevention through careful code analysis and defensive programming, rather than waiting for user reports.