Skip to content

fix: handle SSE error events from providers like Groq #1057

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

lox
Copy link

@lox lox commented Aug 5, 2025

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:

  1. Extract only the JSON portion after "data: " when parsing error events
  2. Skip non-data lines (like "event: error") instead of accumulating them
  3. Reset the error accumulator after each error to prevent data corruption across multiple errors
  4. Maintain full backward compatibility with OpenAI's simpler error format

The implementation handles both formats transparently without requiring configuration:

  • OpenAI format: data: {"error": {...}}
  • Groq/standard SSE format: event: error\ndata: {"error": {...}}

Tests

Added three comprehensive test cases:

  • TestStreamReaderParsesErrorEvents: Validates parsing of Groq's error event format
  • TestStreamReaderHandlesErrorEventWithExtraData: Tests mixed content streams with errors
  • TestStreamReaderResetsErrorAccumulator: Ensures error accumulator is properly reset

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:

event: error
data: {"error":{"message":"Invalid tool_call: tool \"name_unknown\" does not exist.","type":"invalid_request_error","code":"invalid_tool_call"}}

The fix ensures these errors are properly surfaced to the application instead of being masked as EOF or parsing errors.

lox added 2 commits August 5, 2025 14:11
- Parse error events that use explicit SSE event types (event: error)
- Extract only JSON data from error events, skipping event type lines
- Reset error accumulator after each error to prevent data corruption
- Add comprehensive test coverage for both OpenAI and Groq error formats
- Maintain backward compatibility with OpenAI's simpler format

Fixes parsing of error events from Groq API when invalid tool calls occur
- Replace type assertions with errors.As for errorlint compliance
- Break long lines to stay under 120 character limit
- Maintain all existing functionality and test coverage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant