Skip to content

Commit 1ca1a20

Browse files
committed
fixup parameter formatting for gemini
1 parent c4df5fb commit 1ca1a20

1 file changed

Lines changed: 56 additions & 16 deletions

File tree

lib/ruby_llm/providers/gemini/tools.rb

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

113113
schema = RubyLLM::Utils.deep_stringify_keys(schema)
114114

115-
raise ArgumentError, 'Gemini tool parameters must be objects' unless schema['type'] == 'object'
115+
#raise ArgumentError, 'Gemini tool parameters must be objects' unless schema['type'] == 'object'
116+
117+
#{
118+
# type: 'OBJECT',
119+
# properties: schema.fetch('properties', {}).transform_values { |property| convert_property(property) },
120+
# required: (schema['required'] || []).map(&:to_s)
121+
#}
122+
format_parameters(schema.fetch('properties', {}))
123+
end
124+
125+
def format_mcp_parameter(param)
126+
formatted_param = {
127+
type: param_type_for_gemini(param.type),
128+
description: param.description,
129+
}
130+
if param.type.to_s == 'array' && param.items && param.items['type']
131+
formatted_param[:items] = {
132+
type: param_type_for_gemini(param.items['type']),
133+
# description: param.items.description
134+
}
135+
elsif param.type.to_s == 'object' && param.properties
136+
formatted_param[:properties] = param.properties.transform_values do |param|
137+
format_parameter(param)
138+
end
139+
end
140+
formatted_param.compact
141+
end
116142

143+
def format_parameter(param)
144+
unless param.is_a?(Hash)
145+
return format_mcp_parameter(param)
146+
end
147+
formatted_param = {
148+
type: param_type_for_gemini(param['type']),
149+
description: param['description'],
150+
}
151+
if param['type'].to_s == 'array' && param['items'] && param['items']['type']
152+
formatted_param[:items] = {
153+
type: param_type_for_gemini(param['items']['type']),
154+
# description: param.items.description
155+
}
156+
elsif param['type'].to_s == 'object' && param['properties']
157+
formatted_param[:properties] = param['properties'].transform_values do |param|
158+
format_parameter(param)
159+
end
160+
end
161+
formatted_param.compact
162+
end
163+
164+
# Format tool parameters for Gemini API
165+
def format_parameters(parameters)
117166
{
118167
type: 'OBJECT',
119-
properties: parameters.transform_values do |param|
120-
formatted_param = {
121-
type: param_type_for_gemini(param.type),
122-
description: param.description,
123-
}
124-
if param.type.to_s == 'array' && param.items && param.items['type']
125-
126-
formatted_param[:items] = {
127-
type: param_type_for_gemini(param.items['type']),
128-
# description: param.items.description
129-
}
130-
end
131-
formatted_param.compact
168+
properties: parameters.transform_values do |property|
169+
format_parameter(property)
170+
# convert_property(property)
132171
end,
133-
required: parameters.select { |_, p| p.required }.keys.map(&:to_s)
172+
required: parameters.select { |_, p| (p.is_a?(Hash) ? p['required'] : p.required) }.keys.map(&:to_s)
134173
}
135-
end
174+
end
175+
136176

137177
def convert_property(property_schema) # rubocop:disable Metrics/PerceivedComplexity
138178
normalized_schema = normalize_any_of_schema(property_schema)

0 commit comments

Comments
 (0)