From 8f16d0334f8ae857cac0c06aad71c69e08116052 Mon Sep 17 00:00:00 2001 From: Takeshi Yoneda Date: Wed, 23 Oct 2024 09:45:47 -0700 Subject: [PATCH] Correctly set Accept header to text/event-stream for streaming Signed-off-by: Takeshi Yoneda --- completion.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/completion.go b/completion.go index fb43634..4ecc14c 100644 --- a/completion.go +++ b/completion.go @@ -47,7 +47,12 @@ func (r *CompletionService) NewStreaming(ctx context.Context, body CompletionNew err error ) opts = append(r.Options[:], opts...) - opts = append([]option.RequestOption{option.WithJSONSet("stream", true)}, opts...) + opts = append(opts, + option.WithJSONSet("stream", true), + // Streaming response returns a text/event-stream content type. To comply with + // the SSE spec, we need to set the Accept header to text/event-stream explicitly. + option.WithHeader("Accept", "text/event-stream"), + ) path := "completions" err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &raw, opts...) return ssestream.NewStream[Completion](ssestream.NewDecoder(raw), err)