Skip to content

Commit

Permalink
Return data as is if can't get json decoded
Browse files Browse the repository at this point in the history
  • Loading branch information
dvcrn committed Feb 17, 2024
1 parent 626067a commit 70141d2
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 6 deletions.
11 changes: 5 additions & 6 deletions lib/ex_openai/client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ defmodule ExOpenAI.Client do

def process_url(url), do: Config.api_url() <> url

def process_response_body(body) do
# audio/speech endpoint returns binary data, so leave as is
if is_binary(body) do
{:ok, body}
else
Jason.decode(body)
def process_response_body(body) do
case Jason.decode(body) do
{:ok, decoded_json} -> {:ok, decoded_json}
# audio/speech endpoint returns binary data, so leave as is
_ -> {:ok, body}
end
end

Expand Down
19 changes: 19 additions & 0 deletions test/audio_tts_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
defmodule ExOpenAI.TextToSpeechTest do
use ExUnit.Case, async: true
use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney

setup do
ExVCR.Config.cassette_library_dir("./test/fixture/vcr_cassettes")
:ok
end

test "audio text-to-speech" do
use_cassette "audio_text_to_speech" do
{:ok, res} =
ExOpenAI.Audio.create_speech("Hello, hello, hello, just a test.", :"tts-1-hd", :shimmer)

assert res != nil
assert byte_size(res) == 37920
end
end
end
43 changes: 43 additions & 0 deletions test/fixture/vcr_cassettes/audio_text_to_speech.json

Large diffs are not rendered by default.

0 comments on commit 70141d2

Please sign in to comment.