gbaptista / gemini-ai

A Ruby Gem for interacting with Gemini through Vertex AI, Generative Language API, or AI Studio, Google's generative AI services.
https://rubygems.org/gems/gemini-ai
MIT License
103 stars 21 forks source link

add rescue for quota limit request code 429 #23

Closed shmulickrot closed 5 months ago

shmulickrot commented 5 months ago

please add support rescue for response.status = 429 or give access to the raw response completely so we can handle it with our logic thanks for you work

gbaptista commented 5 months ago

@shmulickrot thanks for opening the issue. I believe that today we can capture a 429 status code and access the raw response.

Did you try this?

require 'gemini-ai'

begin
  client.stream_generate_content({
    contents: { role: 'user', parts: { text: 'hi!' } }
  })
rescue Gemini::Errors::GeminiError => error
  puts error.class # Gemini::Errors::RequestError
  puts error.message # 'the server responded with status 429'

  puts error.payload
  # { contents: [{ role: 'user', parts: { text: 'hi!' } }],
  #   generationConfig: { candidateCount: 1 },
  #   ...
  # }

  puts error.request
  # #<Faraday::ServerError response={:status=>429, :headers...
end

error.request.response should give the raw response.

shmulickrot commented 5 months ago

my bad after checking it also works for Faraday::TooManyRequestsError as it the class of the Gemini Error anyway thanks again