Skip to content

Conversation

finbarr
Copy link
Contributor

@finbarr finbarr commented Aug 21, 2025

Summary

  • Fixes API errors when using tools with array-type parameters in both Gemini and OpenAI providers
  • Adds required items field for array parameters in tool declarations
  • Resolves errors like: GenerateContentRequest.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:

  • Gemini: Added items: { type: 'STRING' } in format_parameters method
  • OpenAI: Added items: { type: 'string' } in param_schema method

Changes

  • Modified lib/ruby_llm/providers/gemini/tools.rb to add items field for array types
  • Modified lib/ruby_llm/providers/openai/tools.rb to add items field for array types
  • Added comprehensive tests in spec/ruby_llm/providers/gemini/tools_spec.rb
  • Added comprehensive tests in spec/ruby_llm/providers/openai/tools_spec.rb

Test Plan

  • All existing tests pass
  • New unit tests for array parameter formatting in both providers
  • Integration tests verify proper tool formatting with mixed parameter types
  • Rubocop linting passes
  • Verified fix matches the working monkey patch from production usage (Gemini)
  • Tested with actual tool calls for both providers

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

finbarr and others added 3 commits August 21, 2025 10:28
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
@finbarr finbarr changed the title Fix Gemini array parameter handling in tool declarations Fix array parameter handling in tool declarations for Gemini and OpenAI Aug 21, 2025
type: param_type_for_gemini(param.type),
description: param.description
}.compact

# Add items field for array types
property[:items] = { type: 'STRING' } if param.type.to_s.downcase == 'array'
Copy link
Owner

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants