Skip to content

the server responded with status 400 (Faraday::BadRequestError) with Gemini API key #21

@palladius

Description

@palladius

Hi, I'm trying to use nanobots with Gemini API key and keep failing repeatedly. after some debugging, I can see that gemini-ai and curl work great with my API key, while nb fails to work.

For convenience the code is all here: https://github.com/palladius/ragazzi

# .env file has this 
export GOOGLE_API_KEY='blah blah blah'
# gemini-api-cartridge.yaml
---
meta:
  symbol: 🤖
  name: Nano Bot Name
  author: Your Name
  version: 1.0.0
  license: CC0-1.0
  description: A helpful assistant.

behaviors:
  interaction:
    directive: You are a helpful assistant.

provider:
  id: google
  credentials:
    service: generative-language-api
    api-key: ENV/GOOGLE_API_KEY
  options:
    model: gemini-pro
$ nb gemini-api-cartridge.yaml - eval "Hello" 
/usr/local/google/home/ricc/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/faraday-2.9.1/lib/faraday/response/raise_error.rb:16:in `on_complete': the server responded with status 400 (Faraday::BadRequestError)
        from /usr/local/google/home/ricc/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/faraday-2.9.1/lib/faraday/middleware.rb:18:in `block in call'
        from /usr/local/google/home/ricc/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/faraday-2.9.1/lib/faraday/response.rb:42:in `on_complete'
        from /usr/local/google/home/ricc/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/faraday-2.9.1/lib/faraday/middleware.rb:17:in `call'
        from /usr/local/google/home/ricc/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/faraday-2.9.1/lib/faraday/rack_builder.rb:152:in `build_response'
        from /usr/local/google/home/ricc/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/faraday-2.9.1/lib/faraday/connection.rb:444:in `run_request'
        from /usr/local/google/home/ricc/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/faraday-2.9.1/lib/faraday/connection.rb:280:in `post'
        from /usr/local/google/home/ricc/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/gemini-ai-4.0.0/controllers/client.rb:150:in `request'
        from /usr/local/google/home/ricc/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/gemini-ai-4.0.0/controllers/client.rb:101:in `stream_generate_content'
        from /usr/local/google/home/ricc/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/nano-bots-3.1.0/components/providers/google.rb:152:in `evaluate'
        from /usr/local/google/home/ricc/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/nano-bots-3.1.0/controllers/session.rb:131:in `process_interaction'
        from /usr/local/google/home/ricc/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/nano-bots-3.1.0/controllers/session.rb:107:in `process'
        from /usr/local/google/home/ricc/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/nano-bots-3.1.0/controllers/session.rb:93:in `evaluate_and_print'
        from /usr/local/google/home/ricc/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/nano-bots-3.1.0/controllers/interfaces/eval.rb:15:in `evaluate'
        from /usr/local/google/home/ricc/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/nano-bots-3.1.0/controllers/instance.rb:52:in `eval'
        from /usr/local/google/home/ricc/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/nano-bots-3.1.0/controllers/interfaces/cli.rb:80:in `handle!'
        from /usr/local/google/home/ricc/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/nano-bots-3.1.0/ports/dsl/nano-bots.rb:32:in `cli'
        from /usr/local/google/home/ricc/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/nano-bots-3.1.0/ports/dsl/nano-bots/cli.rb:5:in `<top (required)>'
        from <internal:/usr/local/google/home/ricc/.rbenv/versions/3.2.1/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:88:in `require'
        from <internal:/usr/local/google/home/ricc/.rbenv/versions/3.2.1/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:88:in `require'
        from /usr/local/google/home/ricc/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/nano-bots-3.1.0/bin/nb:4:in `<top (required)>'
        from /usr/local/google/home/ricc/.rbenv/versions/3.2.1/bin/nb:25:in `load'
        from /usr/local/google/home/ricc/.rbenv/versions/3.2.1/bin/nb:25:in `<main>'

I thought I might have a bad key, but then I tried this with same key and that worked:

require 'gemini-ai'

client = Gemini.new(
  credentials: {
    service: 'generative-language-api',
    api_key: ENV['GOOGLE_API_KEY'],
    version: 'v1beta'
  },
  options: {
    model: 'gemini-pro',
    server_sent_events: true }
)

client.stream_generate_content(
  { contents: {
    role: 'user',
    parts: { text: 'hi!' } } }
) do |event, parsed, raw|
  puts event
end

==>

{"candidates"=>[{"content"=>{"parts"=>[{"text"=>"Hello! How can I assist you today?"}], "role"=>"model"}, "finishReason"=>"STOP", "index"=>0, "safetyRatings"=>[{"category"=>"HARM_CATEGORY_SEXUALLY_EXPLICIT", "probability"=>"NEGLIGIBLE"}, {"category"=>"HARM_CATEGORY_HATE_SPEECH", "probability"=>"NEGLIGIBLE"}, {"category"=>"HARM_CATEGORY_HARASSMENT", "probability"=>"NEGLIGIBLE"}, {"category"=>"HARM_CATEGORY_DANGEROUS_CONTENT", "probability"=>"NEGLIGIBLE"}]}], "usageMetadata"=>{"promptTokenCount"=>3, "candidatesTokenCount"=>9, "totalTokenCount"=>12}}

Please help

$ ruby -v
ruby 3.2.1 (2023-02-08 revision 31819e82c8) [x86_64-linux]

$ cat Gemfile
source "https://rubygems.org"

gem 'nano-bots', '~> 3.1.0'
gem 'gemini-ai', '~> 4.0.0'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions