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

When json_post is executed, is JSON.parse not executed on the response? #416

Open
tonegawa07 opened this issue Dec 26, 2023 · 3 comments
Open

Comments

@tonegawa07
Copy link

tonegawa07 commented Dec 26, 2023

I noticed that in the recent commit (dc4d1c6) on https://github.com/alexrudall/ruby-openai, the processing for JSON.parse within the json_post method seems to have been removed. Could you please clarify the reason behind this change?

dc4d1c6

After creating a stub with WebMock as shown below, I encountered an error during the execution of dig('choices', 0, 'message', 'content') due to the absence of JSON.parse being executed.

response = {
  "model" => "gpt-4-0613",
  "choices" => [
    {
      "message" => {
        "role" => "assistant",
        "content" => "Response from the API",
      },
      "finish_reason" => "stop",
      "index" => 0
    }
  ]
}

stub_request(:post, "https://api.openai.com/v1/chat/completions")
  .to_return(status: 200, body: response.to_json, headers: {})
ERROR -- : undefined method `dig' for "{\"model\":\"gpt-4-0613\",\"choices\":[{\"message\":{\"role\":\"assistant\",\"content\":\"Response from the API\"},\"finish_reason\":\"stop\",\"index\":0}]}":String

response.dig('choices', 0, 'message', 'content')
                ^^^^ (NoMethodError)

It seems like the removal of JSON.parse from json_post might be the cause of this issue. What are your thoughts on this?

@gjtorikian
Copy link
Contributor

I ran into this error too. In lieu for there being a solution, I came up with this monkey patch:

  # workaround for https://github.com/alexrudall/ruby-openai/issues/416
  if Rails.env.test?
    module OpenAIClientExtensions
      def chat(**kwargs)
        rez = super
        JSON.parse(rez)
      end
    end

    module OpenAI
      class Client
        prepend OpenAIClientExtensions
      end
    end
  end

@jstewmc
Copy link

jstewmc commented Jan 30, 2025

I found this issue when I was searching for a solution to the same problem. It's been a minute; and, I don't know if y'all are still having it; but, I found the solution.

Set the request's content type:

stub_request(:post, "https://api.openai.com/v1/chat/completions")
  .to_return(status: 200, body: response.to_json, headers: { "Content-Type" => "application/json" })

This'll get Faraday to parse the response correctly, 🚀.

@gjtorikian
Copy link
Contributor

Ah, thanks for that!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants