-
-
Notifications
You must be signed in to change notification settings - Fork 273
Fix array parameter handling in tool declarations for Gemini and OpenAI #358
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
The Gemini API requires an 'items' field for array-type parameters in tool declarations to specify the type of array elements. Without this field, the API returns errors like: "GenerateContentRequest.tools[0].function_declarations[2].parameters.properties[fields].items: missing field" This fix adds the required 'items: { type: 'STRING' }' field for all array parameters when formatting tools for the Gemini provider. Changes: - Modified format_parameters method to add items field for array types - Added comprehensive unit tests for the fix - Added integration tests to verify proper tool formatting This resolves issues when using tools with array parameters in Gemini models, enabling proper function calling with arrays. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
Removed duplicate test coverage by consolidating tools_array_parameter_handling_spec.rb into tools_spec.rb. The single test file now comprehensively covers: - Array parameters with items field - Mixed parameter types - Required vs optional parameters - Integration test of format_tools method
Similar to the Gemini fix, OpenAI also requires an 'items' field for array-type parameters in tool/function declarations. Without this field, tools with array parameters may not work correctly. This commit adds the required 'items: { type: 'string' }' field for all array-type parameters when formatting tools for OpenAI. Changes: - Modified param_schema method to add items field for array types - Added comprehensive tests for array parameter handling - Tests verify both single and multiple array parameters work correctly
}.compact | ||
|
||
# Add items field for array types | ||
property[:items] = { type: 'STRING' } if param.type.to_s.downcase == 'array' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the only array type is string?
Previously, array parameters in tool declarations only supported string item types. This was limiting for tools that needed arrays of numbers, booleans, objects, or other types. Changes: - Add item_type parameter to RubyLLM::Parameter class - Update OpenAI provider to use item_type for array parameters - Update Gemini provider to use item_type with proper type mapping - Add comprehensive tests for different array item types This enables tools to declare arrays with specific item types: - Arrays of integers/numbers for numeric data - Arrays of booleans for flag collections - Arrays of objects for complex structured data Addresses feedback from PR crmne#358 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
@crmne Thank you for the feedback! I've updated the implementation to support all array item types, not just strings. Changes made:
The implementation now properly supports arrays of any JSON Schema type, addressing your concern. The gemfile.lock changes are just platform-specific dependencies (ARM64 Darwin) that were automatically added by bundler. The PR has been rebased on the latest main branch and all tests are passing. |
Thank you so much @finbarr! Looking at the DSL though I think it doesn't look very RubyLLM-like. I'm thinking it may be worth rethinking the Tool DSL completely and take inspiration from @danielfriis's excellent RubyLLM::Schema. Could be great for 2.0! I'll think about it. |
@crmne ok sounds good. My company Shogun is using my fork in production to fix the items type bug for now. As you're thinking about 2.0, one area we've repeatedly run into issues is around observability. It's difficult to get every single raw request/response out of the library right now. |
@finbarr have you tried |
@crmne yes. But we want every single raw request that gets made, and every single raw response that gets received. The hooks and method returns hide failures in the middle, e.g., tool failure retries. |
@crmne it would actually be really nice if there was an easy way to modify requests slightly with an |
Could be an interesting hook to add! Feel free to open an issue so we can gauge demand |
Summary
items
field for array parameters in tool declarationsGenerateContentRequest.tools[0].function_declarations[2].parameters.properties[fields].items: missing field
Problem
Both Gemini and OpenAI APIs require an
items
field for array-type parameters in tool/function declarations to specify the type of array elements. Without this field, tools with array parameters fail with missing field errors or may not work correctly.Solution
Modified the parameter formatting methods in both providers to automatically add the
items
field for all array-type parameters:items: { type: 'STRING' }
informat_parameters
methoditems: { type: 'string' }
inparam_schema
methodChanges
lib/ruby_llm/providers/gemini/tools.rb
to add items field for array typeslib/ruby_llm/providers/openai/tools.rb
to add items field for array typesspec/ruby_llm/providers/gemini/tools_spec.rb
spec/ruby_llm/providers/openai/tools_spec.rb
Test Plan
This resolves issues when using tools with array parameters in both Gemini and OpenAI models, enabling proper function calling with arrays across multiple providers.
🤖 Generated with Claude Code