Skip to content

Handling non-JSON payloads with asDecodedServerSentEventsWithJSONData() #622

Closed
@paulhdk

Description

@paulhdk

Question

Hi!

I'm generating code with the OpenAI OpenAPI specification and am iterating over streamed responses from the chat/completions endpoint with asDecodedServerSentEventsWithJSONData().

The problem is that API returns ChatCompletionStreamResponse JSON payloads until the stream is done, which is denoted by a simple [DONE] payload.

Because [DONE] isn't valid JSON, the last chunk of a successful stream will throw a DecodingError.dataCorrupted().

Currently, I'm just handling it with an empty catch block to avoid the crash, but that seems like a hacky workaround. So, I was wondering whether there was a better solution.

The following ideas haven't been successful so far:

  • Use a ClientMiddleware to match incoming payloads for the [DONE] bytes and toss the payload if they're found.
  • Add another schema to the OpenAPI spec that defines [DONE] and is connect with the ChatCompletionStreamResponse through an anyOf.

Do you have any suggestions?

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/supportAdopter support requests.status/triageCollecting information required to triage the issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions