alexrudall/ruby-openai

401 Faraday::UnauthorizedError

Opened this issue · 2 comments

Describe the bug
Calls to client.assistants.list (or any primary API tasks it seems) returns a 401 error even with a brand new API key which was confirmed to work, using curl

To Reproduce
Steps to reproduce the behavior:

  1. Open pry (or irb)
  2. Enter following code:
require 'openai'
client = OpenAI::Client.new(api_key: 'secret-api-key')
puts client.chat(
  parameters: {
    model: 'gpt-3.5-turbo',
    messages: [{ role: 'user', content: 'Hello!' }]
  }
)

Expected behavior
Assistants tied to the API key would be listed

Screenshots
Included.

Image

Image

Image

Desktop (please complete the following information):

  • OS: macOS 15.3.1
  • Ruby: 3.4.1

Additional context
This gem was fully operational until trying a query a few days ago with this result. I suspect it is probably caused by OpenAI changing their API spec somehow and the library now being out of date as a result.

Hi @SteveBenner , I can't reproduce your issue - would you mind checking the access token you're using is valid and correct?

=> #<OpenAI::Client:106880 @access_token=[REDACTED], @admin_token=[REDACTED], @api_type=nil, @api_version="v1", @extra_headers=[REDACTED], @log_errors=false, @organization_id=[REDACTED], @requ...
irb(main):003:0> 
                                                                                                                                
                 3.5-turbo',                                                                                                    
    messages: [{ role: 'user', content3.5-turbo',                                                                               
    messages: [{ role: 'user', content                                                                                          
                                      : 'Hello!' }]                          
irb(main):003:1* puts client.chat(
irb(main):004:2*   parameters: {
irb(main):005:2*     model: 'gpt-3.5-turbo',
irb(main):006:2*     messages: [{ role: 'user', content: 'Hello!' }]
irb(main):007:1*   }
irb(main):008:0> )
{"id"=>"chatcmpl-BB2Z2S95RsbM6UMi3zS0Aiic6dEVL", "object"=>"chat.completion", "created"=>1741971164, "model"=>"gpt-3.5-turbo-0125", "choices"=>[{"index"=>0, "message"=>{"role"=>"assistant", "content"=>"Hi there! How can I help you today?", "refusal"=>nil, "annotations"=>[]}, "logprobs"=>nil, "finish_reason"=>"stop"}], "usage"=>{"prompt_tokens"=>9, "completion_tokens"=>11, "total_tokens"=>20, "prompt_tokens_details"=>{"cached_tokens"=>0, "audio_tokens"=>0}, "completion_tokens_details"=>{"reasoning_tokens"=>0, "audio_tokens"=>0, "accepted_prediction_tokens"=>0, "rejected_prediction_tokens"=>0}}, "service_tier"=>"default", "system_fingerprint"=>nil}
=> nil
irb(main):009:0> client.assistants.list
=> 
{"object"=>"list",                                                                     
 "data"=>                                                                              
  [{"id"=>"asst_mhdkjP3yCQ2mVlmbM83OjRMY",                                             
    "object"=>"assistant",                                                             
    "created_at"=>1741970926,                                                          
    "name"=>"OpenAI-Ruby test assistant",                                              
    "description"=>nil,                                                                
    "model"=>"gpt-4",                                                                  
    "instructions"=>"When asked a question, write and run Ruby code to answer the question",
    "tools"=>[],                                                                       
    "top_p"=>1.0,                                                                      
    "temperature"=>1.0,                                                                
    "reasoning_effort"=>nil,                                                           
    "tool_resources"=>{},                                                              
    "metadata"=>{},
    "response_format"=>"auto"},

@alexrudall its showing same error to me as well on local setup. What is the permission provided for this api token and after how many hours does this start working incase its takes time to start using via local setup.

Loading development environment (Rails 7.1.5.1)
3.2.2 :001 > client = OpenAI::Client.new
3.2.2 :002 >
=>
#<OpenAI::Client:16880 @access_token=[REDACTED], @admin_token=[REDACTED], @api_t...
3.2.2 :003 > response = client.chat(
3.2.2 :004 > parameters: {
3.2.2 :005 > model: "gpt-4o", # Required.
3.2.2 :006 > messages: [{ role: "user", content: "Hello!"}], # Required.
3.2.2 :007 > temperature: 0.7,
3.2.2 :008 > }
3.2.2 :009 > )
(irb):3:in `

': the server responded with status 401 for POST https://api.openai.com/v1/chat/completions (Faraday::UnauthorizedError)
3.2.2 :010 >