400 Error on Vanilla use of function calling API in Ruby?

Hello!

Just getting started here - I’m using GitHub - gbaptista/gemini-ai: A Ruby Gem for interacting with Gemini through Vertex AI, Generative Language API, or AI Studio, Google's generative AI services. (this seems to be the gem of choice?) in ruby to run function calling. I’ve created an API key via https://aistudio.google.com/app/apikey and done nothing else to configure - I have my API key from that site - and I run the code as follows (and API key is set correctly). The function calling example doesn’t work, but the toy demo they provide does workj

client = Gemini.new(
  credentials: {
    service: 'generative-language-api',
    api_key: ENV['GOOGLE_API_KEY']
  },
  options: { model: 'gemini-pro', server_sent_events: true }
)
input = {
  tools: {
    function_declarations: [
      {
        name: 'date_and_time',
        description: 'Returns the current date and time in the ISO 8601 format for a given timezone.',
        parameters: {
          type: 'object',
          properties: {
            timezone: {
              type: 'string',
              description: 'A string represents the timezone to be used for providing a datetime, following the IANA (Internet Assigned Numbers Authority) Time Zone Database. Examples include "Asia/Tokyo" and "Europe/Paris". If not provided, the default timezone is the user\'s current timezone.'
            }
          }
        }
      }
    ]
  },
  contents: [
    { role: 'user', parts: { text: 'What time is it?' } }
  ]
}
irb(main):068:0> result = client.stream_generate_content(input)                                                                                                                                                                                                                                                                                                                                                                          
ETHON: performed EASY effective_url=https://generativelanguage.googleapis.com/v1/models/gemini-1.0-pro:streamGenerateContent?alt=sse&key=XXX response_code=400 return_code=ok total_time=0.038375                                                                                                                                                                                                    
/usr/local/lib/ruby/gems/3.1.0/gems/faraday-2.12.0/lib/faraday/response/raise_error.rb:30:in `on_complete': the server responded with status 400 (Faraday::BadRequestError)                                                                                                                                                                                                                                                              

The basic toy demo works fine:

irb(main):077:0> input = {"contents":[{"parts":[{"text":"Explain how AI works"}]}]}
=> {:contents=>[{:parts=>[{:text=>"Explain how AI works"}]}]}
irb(main):078:0> result = client.stream_generate_content(input)
ETHON: performed EASY effective_url=https://generativelanguage.googleapis.com/v1/models/vertex-ai-api:streamGenerateContent?key=XXX response_code=404 return_code=ok total_time=0.093008
/usr/local/lib/ruby/gems/3.1.0/gems/faraday-2.12.0/lib/faraday/response/raise_error.rb:30:in `on_complete': the server responded with status 404 (Faraday::ResourceNotFound)
irb(main):079:0> result
=> 
[{"candidates"=>                          
   [{"content"=>{"parts"=>[{"text"=>"**Artificial Intelligence (AI)** is the simulation of human intelligence processes by machines,"}], "role"=>"model"},
     "finishReason"=>"STOP",              
     "index"=>0,                          
     "safetyRatings"=>                    

I’m guessing I’m missing some configuration for my account - any leads on what the likely error is here?

Welcome to the forums!

It isn’t your account. This is a problem with the library you’ve chosen, or at least your configuration, and you may also want to consider a different model. The biggest clue was the message reporting this as the url:

The “v1” in this indicates the API version. The documentation on this is a mess, but I’m pretty sure v1 didn’t support function calling. This should be “v1beta”. From a quick read of the documentation, you can set this in the constructor.

The “gemini-1.0-pro” indicates the model. While Gemini 1.0 Pro supports function calling, it doesn’t support many other Gemini features. Unless you have a good reason otherwise, you should switch to one of the Gemini 1.5 models such as “gemini -1.5-flash”.

While “streamGenerateContent” isn’t bad… it isn’t very useful for function calling. There were at least some reports at one time that you shouldn’t use streaming with function calling. So you may wish to try it with streaming off if you have issues.

That was precisely it! Thank you for that. Makes sense!