Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

BrianBorge
Copy link

What this does

Fixes three critical bugs in the Gemini schema conversion that were causing schema information loss:

  1. Description fields stripped - All schema descriptions were ignored, leading to generic AI responses
  2. Integer/number type merging - Both integer and number types incorrectly converted to single NUMBER type
  3. Attribute loss - Type-specific attributes like enum, format, nullable were not preserved

The fix ensures all schema information is properly converted to Gemini format, resulting in more accurate and contextually appropriate structured outputs.

Type of change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation
  • Performance improvement

Scope check

  • I read the Contributing Guide
  • This aligns with RubyLLM's focus on LLM communication
  • This isn't application-specific logic that belongs in user code
  • This benefits most users, not just my specific use case

Quality check

  • I ran overcommit --install and all hooks pass
  • I tested my changes thoroughly
  • I updated documentation if needed
  • I didn't modify auto-generated files manually (models.json, aliases.json)

API changes

  • Breaking change
  • New public methods/classes
  • Changed method signatures
  • No API changes

Related issues

Fixes #354

Before/After Examples

Description Preservation

Before: Generic responses due to ignored descriptions

{"example" => "hello"}

After: Contextual responses using schema descriptions

{"example" => "The attendee spent most of their time in technical workshops, networking during breaks, and particularly enjoyed the keynote speech on AI ethics."}

Type Distinction

Before: Both integer and number became floats

{"number1" => 100.5, "number2" => 25.75}  # Both floats

After: Proper type enforcement

{"number1" => 1.23, "number2" => 42}  # Integer constraint respected

Attribute Preservation

After: All schema attributes now preserved:

  • description for all types
  • enum for string constraints
  • format for validation hints
  • nullable for optional fields

Technical Changes

Modified convert_schema_to_gemini method in lib/ruby_llm/providers/gemini/chat.rb:

  • Added description preservation for all schema types
  • Fixed type mapping: integerINTEGER, numberNUMBER
  • Enhanced attribute preservation for format, nullable, etc.
  • Maintained backward compatibility

Testing

  • ✅ All existing tests pass
  • ✅ Verified improved responses locally
  • ✅ Confirmed proper integer/number type handling

@BrianBorge BrianBorge mentioned this pull request Aug 19, 2025
3 tasks
Comment on lines 112 to 131
result[:format] = schema[:format] if schema[:format]
result[:nullable] = schema[:nullable] if schema.key?(:nullable)
result
when 'number'
result = { type: 'NUMBER' }
result[:description] = schema[:description] if schema[:description]
result[:format] = schema[:format] if schema[:format]
result[:nullable] = schema[:nullable] if schema.key?(:nullable)
result
when 'integer'
result = { type: 'INTEGER' }
result[:description] = schema[:description] if schema[:description]
result[:format] = schema[:format] if schema[:format]
result[:nullable] = schema[:nullable] if schema.key?(:nullable)
Copy link
Author

@BrianBorge BrianBorge Aug 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can dry this up if you want, but I decided to keep it all in the same method unless asked otherwise. I thought it best to keep it all in the same method for now.

Copy link
Owner

@crmne crmne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@BrianBorge BrianBorge force-pushed the gemini-schema-conversion-bugs branch from 8c7f0bb to 2418b47 Compare August 19, 2025 18:01
@BrianBorge
Copy link
Author

@crmne - I saw the Rubocop failures. Initially, the overcommit hooks didn't run. I must not have had it set up. That is fixed now. I forced-pushed up a change to satisfy Rubocop.

@BrianBorge BrianBorge force-pushed the gemini-schema-conversion-bugs branch from 2418b47 to df2c50e Compare August 19, 2025 18:11
- preserve descriptions,
- distinguish integer/number types
@BrianBorge BrianBorge force-pushed the gemini-schema-conversion-bugs branch from df2c50e to 519a10c Compare August 19, 2025 21:27
Copy link

codecov bot commented Aug 19, 2025

Codecov Report

❌ Patch coverage is 89.28571% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 87.85%. Comparing base (20bb0c5) to head (c022336).
⚠️ Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
lib/ruby_llm/providers/gemini/chat.rb 89.28% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #355      +/-   ##
==========================================
+ Coverage   87.82%   87.85%   +0.03%     
==========================================
  Files          88       88              
  Lines        3483     3500      +17     
  Branches      719      733      +14     
==========================================
+ Hits         3059     3075      +16     
- Misses        424      425       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@crmne crmne closed this in 474ee96 Aug 20, 2025
@crmne
Copy link
Owner

crmne commented Aug 20, 2025

Hi @BrianBorge I actually wanted the code to be a little nicer and add tests, so I went ahead and did it myself so we can release this ASAP. Thanks for starting the PR!

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.

[BUG] Schema Issues with Gemini
2 participants