fix: handle SSE error events from providers like Groq #1057
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.
This PR fixes SSE (Server-Sent Events) error parsing for providers that use explicit event types in their error responses, such as Groq.
Currently, when Groq sends error events with an "event: error" prefix, the library fails to parse them correctly, returning "unexpected end of JSON input" instead of the actual error details.
While OpenAI's official documentation doesn't explicitly document their SSE error format, this change maintains compatibility with OpenAI's implementation while adding support for the standard SSE format used by other providers.
Related: https://platform.openai.com/docs/api-reference/streaming
Solution
The solution modifies the SSE stream parser to:
The implementation handles both formats transparently without requiring configuration:
Tests
Added three comprehensive test cases:
All existing tests continue to pass, maintaining backward compatibility.
Additional context
This issue was discovered when using the library with Groq's API for tool calling. When the model makes invalid tool calls, Groq returns error events that the current implementation cannot parse.
Example error from Groq:
The fix ensures these errors are properly surfaced to the application instead of being masked as EOF or parsing errors.