Fix: Add empty checks for reasoning content arrays in stream handler #1982
+7
−1
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.