Skip to content

Conversation

@roomote
Copy link
Contributor

@roomote roomote bot commented Dec 4, 2025

This PR attempts to address Issue #9816.

Problem

The Gemini API was receiving malformed requests with empty data fields, causing the error:

GenerateContentRequest.contents[4].parts[2].data: required oneof field 'data' must have one initialized field

Root Cause

The convertAnthropicContentToGemini function uses flatMap which can return empty arrays when:

  • thoughtSignature blocks are excluded
  • tool_result blocks have no content
  • Unsupported content block types are encountered

These empty arrays were being included in the parts array sent to Gemini, causing API errors.

Solution

  1. Added filtering logic to remove empty parts from the flatMap results
  2. Enhanced convertAnthropicMessageToGemini to properly handle empty parts arrays
  3. Added comprehensive test coverage for edge cases

Testing

  • All existing tests pass
  • Added 6 new test cases covering various edge cases with empty content blocks
  • Verified the fix prevents sending empty parts to the Gemini API

Feedback and guidance are welcome!

…I errors

- Added filtering logic to remove empty parts from flatMap results
- Prevents "required oneof field data must have one initialized field" errors
- Added comprehensive tests for edge cases with empty content blocks
- Fixes #9816
@roomote
Copy link
Contributor Author

roomote bot commented Dec 4, 2025

Rooviewer Clock   See task on Roo Cloud

Review complete. Found 1 code quality observation:

  • The filter added at lines 139-145 is redundant since flatMap already handles empty arrays

Mention @roomote in a comment to request specific changes to this pull request or fix all unresolved issues.

Comment on lines +139 to +145
// Filter out any empty arrays that were returned by flatMap
// This prevents sending empty parts to the Gemini API which would cause errors
return parts.filter((part): part is Part => {
// Check if the part is actually a Part object (not an empty array)
// Empty arrays from flatMap will be filtered out here
return part && typeof part === "object" && Object.keys(part).length > 0
})
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This filter is redundant because flatMap already handles empty arrays by flattening them away (an empty array [] contributes nothing to the flattened result). When the callback returns [] on lines 60, 84, 106, or 135, flatMap automatically excludes those from the final array. The filter won't actually filter anything since flatMap has already removed empty arrays, and every Part object returned in the code has at least one property (making Object.keys(part).length > 0 always true for valid Parts).

Fix it with Roo Code or mention @roomote and request a fix.

@hannesrudolph hannesrudolph added the Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. label Dec 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels.

Projects

Status: Triage

Development

Successfully merging this pull request may close these issues.

3 participants