Skip to content

Commit 8ae8cea

Browse files
committed
fixup parameter formatting for gemini
1 parent 091f281 commit 8ae8cea

1 file changed

Lines changed: 56 additions & 16 deletions

File tree

lib/ruby_llm/protocols/gemini/tools.rb

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -128,27 +128,67 @@ def convert_tool_schema_to_gemini(schema)
128128

129129
schema = RubyLLM::Utils.deep_stringify_keys(schema)
130130

131-
raise ArgumentError, 'Gemini tool parameters must be objects' unless schema['type'] == 'object'
131+
#raise ArgumentError, 'Gemini tool parameters must be objects' unless schema['type'] == 'object'
132+
133+
#{
134+
# type: 'OBJECT',
135+
# properties: schema.fetch('properties', {}).transform_values { |property| convert_property(property) },
136+
# required: (schema['required'] || []).map(&:to_s)
137+
#}
138+
format_parameters(schema.fetch('properties', {}))
139+
end
140+
141+
def format_mcp_parameter(param)
142+
formatted_param = {
143+
type: param_type_for_gemini(param.type),
144+
description: param.description,
145+
}
146+
if param.type.to_s == 'array' && param.items && param.items['type']
147+
formatted_param[:items] = {
148+
type: param_type_for_gemini(param.items['type']),
149+
# description: param.items.description
150+
}
151+
elsif param.type.to_s == 'object' && param.properties
152+
formatted_param[:properties] = param.properties.transform_values do |param|
153+
format_parameter(param)
154+
end
155+
end
156+
formatted_param.compact
157+
end
132158

159+
def format_parameter(param)
160+
unless param.is_a?(Hash)
161+
return format_mcp_parameter(param)
162+
end
163+
formatted_param = {
164+
type: param_type_for_gemini(param['type']),
165+
description: param['description'],
166+
}
167+
if param['type'].to_s == 'array' && param['items'] && param['items']['type']
168+
formatted_param[:items] = {
169+
type: param_type_for_gemini(param['items']['type']),
170+
# description: param.items.description
171+
}
172+
elsif param['type'].to_s == 'object' && param['properties']
173+
formatted_param[:properties] = param['properties'].transform_values do |param|
174+
format_parameter(param)
175+
end
176+
end
177+
formatted_param.compact
178+
end
179+
180+
# Format tool parameters for Gemini API
181+
def format_parameters(parameters)
133182
{
134183
type: 'OBJECT',
135-
properties: parameters.transform_values do |param|
136-
formatted_param = {
137-
type: param_type_for_gemini(param.type),
138-
description: param.description,
139-
}
140-
if param.type.to_s == 'array' && param.items && param.items['type']
141-
142-
formatted_param[:items] = {
143-
type: param_type_for_gemini(param.items['type']),
144-
# description: param.items.description
145-
}
146-
end
147-
formatted_param.compact
184+
properties: parameters.transform_values do |property|
185+
format_parameter(property)
186+
# convert_property(property)
148187
end,
149-
required: parameters.select { |_, p| p.required }.keys.map(&:to_s)
188+
required: parameters.select { |_, p| (p.is_a?(Hash) ? p['required'] : p.required) }.keys.map(&:to_s)
150189
}
151-
end
190+
end
191+
152192

153193
def convert_property(property_schema) # rubocop:disable Metrics/PerceivedComplexity
154194
normalized_schema = normalize_any_of_schema(property_schema)

0 commit comments

Comments
 (0)