Skip to content

Streaming exception handling #949

@linhaojun857

Description

@linhaojun857

Your issue may already be reported!
Please search on the issue tracker before creating one.

Describe the bug
When the data returned by SSE streaming starts with "data:" (not "data: "), streaming processing fails.

To Reproduce
Steps to reproduce the behavior, including any relevant code snippets.

Expected behavior
A clear and concise description of what you expected to happen.

Screenshots/Logs
If applicable, add screenshots to help explain your problem. For non-graphical issues, please provide any relevant logs or stack traces.

Environment (please complete the following information):

  • go-openai version: [e.g. v1.12.0]
  • Go version: [e.g. 1.18]
  • OpenAI API version: [e.g. v1]
  • OS: [e.g. Ubuntu 20.04]

Additional context
Add any other context about the problem here.

Activity

linhaojun857

linhaojun857 commented on Mar 4, 2025

@linhaojun857
Author

This is the processing logic of openai-python:

    def decode(self, line: str) -> ServerSentEvent | None:
        # See: https://html.spec.whatwg.org/multipage/server-sent-events.html#event-stream-interpretation  # noqa: E501

        if not line:
            if not self._event and not self._data and not self._last_event_id and self._retry is None:
                return None

            sse = ServerSentEvent(
                event=self._event,
                data="\n".join(self._data),
                id=self._last_event_id,
                retry=self._retry,
            )

            # NOTE: as per the SSE spec, do not reset last_event_id.
            self._event = None
            self._data = []
            self._retry = None

            return sse

        if line.startswith(":"):
            return None

        fieldname, _, value = line.partition(":")

        if value.startswith(" "):
            value = value[1:]

        if fieldname == "event":
            self._event = value
        elif fieldname == "data":
            self._data.append(value)
        elif fieldname == "id":
            if "\0" in value:
                pass
            else:
                self._last_event_id = value
        elif fieldname == "retry":
            try:
                self._retry = int(value)
            except (TypeError, ValueError):
                pass
        else:
            pass  # Field is ignored.

        return None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @linhaojun857

        Issue actions

          Streaming exception handling · Issue #949 · sashabaranov/go-openai