-
Notifications
You must be signed in to change notification settings - Fork 750
fix: handle undefined choice in together ai finish reason transform #1321
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good defensive programming fix for undefined choice access, plus improved error logging with provider context.
} | ||
const parsedChunk: TogetherAIChatCompletionStreamChunk = JSON.parse(chunk); | ||
const finishReason = parsedChunk.choices[0].finish_reason | ||
const finishReason = parsedChunk.choices[0]?.finish_reason |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🐛 Bug Fix
Issue: Potential runtime crash when parsedChunk.choices[0]
is undefined - accessing finish_reason
property on undefined would throw TypeError
Fix: Added optional chaining operator to safely access finish_reason
property
Impact: Prevents runtime crashes when Together AI returns empty choices array or undefined first choice
const finishReason = parsedChunk.choices[0]?.finish_reason | |
const finishReason = parsedChunk.choices[0]?.finish_reason |
} | ||
} catch (error) { | ||
console.error('Error during stream processing:', error); | ||
console.error('Error during stream processing:', proxyProvider, error); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Code Refactor
Issue: Error logs lack context about which provider failed, making debugging difficult in multi-provider scenarios
Fix: Added proxyProvider
parameter to error logs for better traceability
Impact: Improves debugging efficiency by identifying which provider caused stream processing failures
console.error('Error during stream processing:', proxyProvider, error); | |
console.error('Error during stream processing:', proxyProvider, error); |
console.error( | ||
'Failed to close the writer:', | ||
proxyProvider, | ||
closeError | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Code Refactor
Issue: Writer close error logs lack provider context for debugging
Fix: Added proxyProvider
parameter to close error logs with proper formatting
Impact: Consistent error logging pattern across all stream handler error cases
console.error( | |
'Failed to close the writer:', | |
proxyProvider, | |
closeError | |
); | |
console.error( | |
'Failed to close the writer:', | |
proxyProvider, | |
closeError | |
); |
} | ||
} catch (error) { | ||
console.error('Error during stream processing:', error); | ||
console.error('Error during stream processing:', proxyProvider, error); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Code Refactor
Issue: Duplicate error logging pattern - second occurrence also lacks provider context
Fix: Added proxyProvider
parameter for consistency with first error handler
Impact: Maintains consistent error logging across all stream processing error cases
console.error('Error during stream processing:', proxyProvider, error); | |
console.error('Error during stream processing:', proxyProvider, error); |
console.error( | ||
'Failed to close the writer:', | ||
proxyProvider, | ||
closeError | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Code Refactor
Issue: Second writer close error handler also lacks provider context
Fix: Added proxyProvider
parameter to complete the consistent error logging pattern
Impact: All error cases now include provider information for better debugging
console.error( | |
'Failed to close the writer:', | |
proxyProvider, | |
closeError | |
); | |
console.error( | |
'Failed to close the writer:', | |
proxyProvider, | |
closeError | |
); |
Description
Motivation
Type of Change
How Has This Been Tested?
Screenshots (if applicable)
Checklist
Related Issues