Skip to content
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

Streaming exception handling #949

Open
linhaojun857 opened this issue Mar 4, 2025 · 1 comment
Open

Streaming exception handling #949

linhaojun857 opened this issue Mar 4, 2025 · 1 comment
Labels
bug Something isn't working

Comments

@linhaojun857
Copy link

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.

@linhaojun857 linhaojun857 added the bug Something isn't working label Mar 4, 2025
@linhaojun857
Copy link
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
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant