Skip to content

Fix embedding return format inconsistency for single-string arrays#267

Merged
crmne merged 2 commits intocrmne:mainfrom
finbarr:fix-single-string-array-embeddings
Jul 16, 2025
Merged

Fix embedding return format inconsistency for single-string arrays#267
crmne merged 2 commits intocrmne:mainfrom
finbarr:fix-single-string-array-embeddings

Conversation

@finbarr
Copy link
Copy Markdown
Contributor

@finbarr finbarr commented Jun 30, 2025

Summary

  • Fixes inconsistent return format when passing an array with a single string to the embed method
  • Ensures RubyLLM.embed([string]) returns [[vector]] instead of [vector]
  • Maintains backward compatibility for single string inputs

Problem

As reported in #254, the embedding API had inconsistent behavior:

RubyLLM.embed(string) -> [vector] 
RubyLLM.embed([string, string]) -> [[vector], [vector]] 
RubyLLM.embed([string]) -> [vector]  (should be [[vector]])

Solution

  • Modified the base Provider class to pass the original text parameter to parse_embedding_response
  • Updated OpenAI and Gemini providers to check if input was an array before unwrapping single embeddings
  • Added comprehensive test coverage for this edge case

Test plan

  • Added new test case specifically for single-string arrays
  • All existing tests pass
  • Verified fix works for both OpenAI and Gemini providers

Fixes #254

🤖 Generated with Claude Code

When passing an array with a single string to the embed method, it was
returning a flat array of floats instead of an array containing one array
of floats. This broke the expected consistent behavior where arrays always
return arrays of arrays.

Fixed by:
- Passing the original text parameter through to parse_embedding_response
- Checking if input was an array before unwrapping single embeddings
- Ensuring RubyLLM.embed([string]) returns [[vector]] not [vector]

Fixes crmne#254

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@codecov
Copy link
Copy Markdown

codecov bot commented Jul 16, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 89.72%. Comparing base (78482e5) to head (f1a6958).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #267   +/-   ##
=======================================
  Coverage   89.72%   89.72%           
=======================================
  Files          75       75           
  Lines        2812     2812           
  Branches      555      555           
=======================================
  Hits         2523     2523           
  Misses        289      289           

☔ 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 merged commit f33e5d8 into crmne:main Jul 16, 2025
14 checks passed
infinityrobot added a commit to infinityrobot/ruby_llm that referenced this pull request Jul 18, 2025
@infinityrobot infinityrobot mentioned this pull request Jul 18, 2025
17 tasks
parruda pushed a commit to parruda/ruby_llm that referenced this pull request Nov 18, 2025
…rmne#267)

## Summary
- Fixes inconsistent return format when passing an array with a single
string to the embed method
- Ensures `RubyLLM.embed([string])` returns `[[vector]]` instead of
`[vector]`
- Maintains backward compatibility for single string inputs

## Problem
As reported in crmne#254, the embedding API had inconsistent behavior:
```ruby
RubyLLM.embed(string) -> [vector] ✓
RubyLLM.embed([string, string]) -> [[vector], [vector]] ✓
RubyLLM.embed([string]) -> [vector] ✗ (should be [[vector]])
```

## Solution
- Modified the base Provider class to pass the original `text` parameter
to `parse_embedding_response`
- Updated OpenAI and Gemini providers to check if input was an array
before unwrapping single embeddings
- Added comprehensive test coverage for this edge case

## Test plan
- [x] Added new test case specifically for single-string arrays
- [x] All existing tests pass
- [x] Verified fix works for both OpenAI and Gemini providers

Fixes crmne#254

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Carmine Paolino <carmine@paolino.me>
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] Inconsistent return format for RubyLLM.embed with single vs multiple inputs

2 participants