From 687f7d5f71e95e81f0481093f28acaabdc27f238 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mauricio=20V=C3=A1squez?= Date: Wed, 20 Dec 2023 14:22:43 -0500 Subject: [PATCH] Fix support for non json speech to text Fixes issue when using non audio formats: Transcription error: json: cannot unmarshal number into Go value of type openai.audioTextResponse Fixes: 8e165dc9aadc ("Feat Add headers to openai responses (#506)") --- audio.go | 6 +++++- client.go | 11 +++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/audio.go b/audio.go index 4cbe4fe64..ec4be0ecd 100644 --- a/audio.go +++ b/audio.go @@ -68,11 +68,15 @@ type AudioResponse struct { } type audioTextResponse struct { - Text string `json:"text"` + Text string httpHeader } +func (r *audioTextResponse) SetText(text string) { + r.Text = text +} + func (r *audioTextResponse) ToAudioResponse() AudioResponse { return AudioResponse{ Text: r.Text, diff --git a/client.go b/client.go index 056226c61..20f04836d 100644 --- a/client.go +++ b/client.go @@ -24,6 +24,10 @@ type Response interface { SetHeader(http.Header) } +type TextResponse interface { + SetText(string) +} + type httpHeader http.Header func (h *httpHeader) SetHeader(header http.Header) { @@ -195,6 +199,13 @@ func decodeResponse(body io.Reader, v any) error { if result, ok := v.(*string); ok { return decodeString(body, result) + } else if result, ok := v.(TextResponse); ok { + var text string + if err := decodeString(body, &text); err != nil { + return err + } + result.SetText(text) + return nil } return json.NewDecoder(body).Decode(v) }