Skip to content

Commit 7e0d94f

Browse files
committed
Handle Gemini maxOutputTokens attribute properly
1 parent a0efaa4 commit 7e0d94f

File tree

8 files changed

+211
-16
lines changed

8 files changed

+211
-16
lines changed

gemfiles/rails_7.1.gemfile.lock

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ GEM
9898
rake
9999
thor (>= 0.14.0)
100100
ast (2.4.3)
101-
async (2.32.0)
101+
async (2.33.0)
102102
console (~> 1.29)
103103
fiber-annotation
104104
io-event (~> 1.11)
@@ -132,7 +132,7 @@ GEM
132132
erb (5.0.2)
133133
erubi (1.13.1)
134134
event_stream_parser (1.0.0)
135-
faraday (2.13.4)
135+
faraday (2.14.0)
136136
faraday-net_http (>= 2.0, < 3.5)
137137
json
138138
logger
@@ -148,6 +148,7 @@ GEM
148148
concurrent-ruby (~> 1.1)
149149
webrick (~> 1.7)
150150
websocket-driver (~> 0.7)
151+
ffi (1.17.2-arm64-darwin)
151152
ffi (1.17.2-x86_64-linux-gnu)
152153
fiber-annotation (0.2.0)
153154
fiber-local (1.1.0)
@@ -214,7 +215,7 @@ GEM
214215
mutex_m (0.3.0)
215216
net-http (0.6.0)
216217
uri
217-
net-imap (0.5.10)
218+
net-imap (0.5.11)
218219
date
219220
net-protocol
220221
net-pop (0.1.2)
@@ -224,6 +225,8 @@ GEM
224225
net-smtp (0.5.1)
225226
net-protocol
226227
nio4r (2.7.4)
228+
nokogiri (1.18.10-arm64-darwin)
229+
racc (~> 1.4)
227230
nokogiri (1.18.10-x86_64-linux-gnu)
228231
racc (~> 1.4)
229232
os (1.1.4)
@@ -307,7 +310,7 @@ GEM
307310
diff-lcs (>= 1.2.0, < 2.0)
308311
rspec-support (~> 3.13.0)
309312
rspec-support (3.13.6)
310-
rubocop (1.81.0)
313+
rubocop (1.81.1)
311314
json (~> 2.3)
312315
language_server-protocol (~> 3.17.0.2)
313316
lint_roller (~> 1.1.0)
@@ -355,6 +358,7 @@ GEM
355358
simplecov (~> 0.19)
356359
simplecov-html (0.13.2)
357360
simplecov_json_formatter (0.1.4)
361+
sqlite3 (2.7.4-arm64-darwin)
358362
sqlite3 (2.7.4-x86_64-linux-gnu)
359363
stringio (3.1.7)
360364
thor (1.4.0)
@@ -380,6 +384,7 @@ GEM
380384
zeitwerk (2.7.3)
381385

382386
PLATFORMS
387+
arm64-darwin-24
383388
x86_64-linux
384389

385390
DEPENDENCIES
@@ -416,4 +421,4 @@ DEPENDENCIES
416421
webmock (~> 3.18)
417422

418423
BUNDLED WITH
419-
2.6.9
424+
2.6.9

gemfiles/rails_7.2.gemfile.lock

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ GEM
9292
rake
9393
thor (>= 0.14.0)
9494
ast (2.4.3)
95-
async (2.32.0)
95+
async (2.33.0)
9696
console (~> 1.29)
9797
fiber-annotation
9898
io-event (~> 1.11)
@@ -126,7 +126,7 @@ GEM
126126
erb (5.0.2)
127127
erubi (1.13.1)
128128
event_stream_parser (1.0.0)
129-
faraday (2.13.4)
129+
faraday (2.14.0)
130130
faraday-net_http (>= 2.0, < 3.5)
131131
json
132132
logger
@@ -142,6 +142,7 @@ GEM
142142
concurrent-ruby (~> 1.1)
143143
webrick (~> 1.7)
144144
websocket-driver (~> 0.7)
145+
ffi (1.17.2-arm64-darwin)
145146
ffi (1.17.2-x86_64-linux-gnu)
146147
fiber-annotation (0.2.0)
147148
fiber-local (1.1.0)
@@ -207,7 +208,7 @@ GEM
207208
multipart-post (2.4.1)
208209
net-http (0.6.0)
209210
uri
210-
net-imap (0.5.10)
211+
net-imap (0.5.11)
211212
date
212213
net-protocol
213214
net-pop (0.1.2)
@@ -217,6 +218,8 @@ GEM
217218
net-smtp (0.5.1)
218219
net-protocol
219220
nio4r (2.7.4)
221+
nokogiri (1.18.10-arm64-darwin)
222+
racc (~> 1.4)
220223
nokogiri (1.18.10-x86_64-linux-gnu)
221224
racc (~> 1.4)
222225
os (1.1.4)
@@ -300,7 +303,7 @@ GEM
300303
diff-lcs (>= 1.2.0, < 2.0)
301304
rspec-support (~> 3.13.0)
302305
rspec-support (3.13.6)
303-
rubocop (1.81.0)
306+
rubocop (1.81.1)
304307
json (~> 2.3)
305308
language_server-protocol (~> 3.17.0.2)
306309
lint_roller (~> 1.1.0)
@@ -348,6 +351,7 @@ GEM
348351
simplecov (~> 0.19)
349352
simplecov-html (0.13.2)
350353
simplecov_json_formatter (0.1.4)
354+
sqlite3 (2.7.4-arm64-darwin)
351355
sqlite3 (2.7.4-x86_64-linux-gnu)
352356
stringio (3.1.7)
353357
thor (1.4.0)
@@ -374,6 +378,7 @@ GEM
374378
zeitwerk (2.7.3)
375379

376380
PLATFORMS
381+
arm64-darwin-24
377382
x86_64-linux
378383

379384
DEPENDENCIES
@@ -410,4 +415,4 @@ DEPENDENCIES
410415
webmock (~> 3.18)
411416

412417
BUNDLED WITH
413-
2.6.9
418+
2.6.9

gemfiles/rails_8.0.gemfile.lock

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ GEM
9292
rake
9393
thor (>= 0.14.0)
9494
ast (2.4.3)
95-
async (2.32.0)
95+
async (2.33.0)
9696
console (~> 1.29)
9797
fiber-annotation
9898
io-event (~> 1.11)
@@ -126,7 +126,7 @@ GEM
126126
erb (5.0.2)
127127
erubi (1.13.1)
128128
event_stream_parser (1.0.0)
129-
faraday (2.13.4)
129+
faraday (2.14.0)
130130
faraday-net_http (>= 2.0, < 3.5)
131131
json
132132
logger
@@ -142,6 +142,7 @@ GEM
142142
concurrent-ruby (~> 1.1)
143143
webrick (~> 1.7)
144144
websocket-driver (~> 0.7)
145+
ffi (1.17.2-arm64-darwin)
145146
ffi (1.17.2-x86_64-linux-gnu)
146147
fiber-annotation (0.2.0)
147148
fiber-local (1.1.0)
@@ -207,7 +208,7 @@ GEM
207208
multipart-post (2.4.1)
208209
net-http (0.6.0)
209210
uri
210-
net-imap (0.5.10)
211+
net-imap (0.5.11)
211212
date
212213
net-protocol
213214
net-pop (0.1.2)
@@ -217,6 +218,8 @@ GEM
217218
net-smtp (0.5.1)
218219
net-protocol
219220
nio4r (2.7.4)
221+
nokogiri (1.18.10-arm64-darwin)
222+
racc (~> 1.4)
220223
nokogiri (1.18.10-x86_64-linux-gnu)
221224
racc (~> 1.4)
222225
os (1.1.4)
@@ -301,7 +304,7 @@ GEM
301304
diff-lcs (>= 1.2.0, < 2.0)
302305
rspec-support (~> 3.13.0)
303306
rspec-support (3.13.6)
304-
rubocop (1.81.0)
307+
rubocop (1.81.1)
305308
json (~> 2.3)
306309
language_server-protocol (~> 3.17.0.2)
307310
lint_roller (~> 1.1.0)
@@ -349,6 +352,7 @@ GEM
349352
simplecov (~> 0.19)
350353
simplecov-html (0.13.2)
351354
simplecov_json_formatter (0.1.4)
355+
sqlite3 (2.7.4-arm64-darwin)
352356
sqlite3 (2.7.4-x86_64-linux-gnu)
353357
stringio (3.1.7)
354358
thor (1.4.0)
@@ -376,6 +380,7 @@ GEM
376380
zeitwerk (2.7.3)
377381

378382
PLATFORMS
383+
arm64-darwin-24
379384
x86_64-linux
380385

381386
DEPENDENCIES
@@ -412,4 +417,4 @@ DEPENDENCIES
412417
webmock (~> 3.18)
413418

414419
BUNDLED WITH
415-
2.6.9
420+
2.6.9

lib/ruby_llm/provider.rb

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ def headers
2121
{}
2222
end
2323

24+
def parameter_mappings
25+
{}
26+
end
27+
2428
def slug
2529
self.class.slug
2630
end
@@ -39,6 +43,7 @@ def configuration_requirements
3943

4044
def complete(messages, tools:, temperature:, model:, params: {}, headers: {}, schema: nil, &) # rubocop:disable Metrics/ParameterLists
4145
normalized_temperature = maybe_normalize_temperature(temperature, model)
46+
transformed_params = apply_parameter_mappings(params)
4247

4348
payload = Utils.deep_merge(
4449
render_payload(
@@ -49,7 +54,7 @@ def complete(messages, tools:, temperature:, model:, params: {}, headers: {}, sc
4954
stream: block_given?,
5055
schema: schema
5156
),
52-
params
57+
transformed_params
5358
)
5459

5560
if block_given?
@@ -192,6 +197,28 @@ def configured_remote_providers(config)
192197

193198
private
194199

200+
def apply_parameter_mappings(params)
201+
return params if parameter_mappings.empty?
202+
203+
transformed = params.dup
204+
205+
parameter_mappings.each do |source_key, target_path|
206+
next unless transformed.key?(source_key)
207+
208+
value = transformed.delete(source_key)
209+
*keys, last_key = target_path
210+
211+
target = keys.inject(transformed) do |hash, key|
212+
hash[key] = {} unless hash[key].is_a?(Hash)
213+
hash[key]
214+
end
215+
216+
target[last_key] = value
217+
end
218+
219+
transformed
220+
end
221+
195222
def try_parse_json(maybe_json)
196223
return maybe_json unless maybe_json.is_a?(String)
197224

lib/ruby_llm/providers/gemini.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ def headers
2222
}
2323
end
2424

25+
private
26+
27+
def parameter_mappings
28+
{
29+
max_tokens: %i[generationConfig maxOutputTokens]
30+
}
31+
end
32+
2533
class << self
2634
def capabilities
2735
Gemini::Capabilities

spec/fixtures/vcr_cassettes/chat_with_params_gemini_gemini-2_5-flash_automatically_maps_max_tokens_to_maxoutputtokens.yml

Lines changed: 81 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)