Skip to content

fix(sse): do not dispatch events without data and default event type to message - #835

Open
adityajai119 wants to merge 1 commit into
deepgram:mainfrom
adityajai119:fix/sse-decoder-blank-events
Open

adityajai119 wants to merge 1 commit into
deepgram:mainfrom
adityajai119:fix/sse-decoder-blank-events

Conversation

@adityajai119

Copy link
Copy Markdown

Problem

SSEDecoder.decode() used the presence of any buffered field -- including id, retry, or event -- to decide whether to dispatch an event on a blank line. Per the SSE event-stream interpretation algorithm, an event is dispatched only when the data buffer is non-empty. Keep-alives such as "id: 42" or "retry: 1000" followed by a blank line therefore produced spurious ServerSentEvent objects with empty data; callers using .json() then failed with a JSON decode error.

The same dispatch path also passed event=self._event verbatim, yielding event="" when no event: field was present instead of the spec default "message" (and the ServerSentEvent dataclass default).

Fix

  • Dispatch on a blank line only when data has been buffered; clear the event-type buffer when discarding.
  • Default the event type to "message" when no event: field was supplied.

Tests

New tests/custom/test_sse_decoder_blank_lines.py covers id-only, retry-only, event-only frames, no leakage into the next real event, and normal multi-line data dispatch. Before the fix: 4 failed / 1 passed. After: 5 passed. ruff check passes on both changed files.

This branch has not been deployed

No deployments
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