Skip to content

Conversation

@waleedlatif1
Copy link
Collaborator

Summary

  • Fix Gemini API error when tools return primitive values (boolean, string, number, null)
  • Add ensureStructResponse utility to wrap primitives in { value: ... } for functionResponse.response
  • Apply fix to both message history conversion and direct tool execution paths

Type of Change

  • Bug fix

Testing

  • Added 24 unit tests for the fix
  • Tested manually

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

@vercel
Copy link

vercel bot commented Jan 20, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Review Updated (UTC)
docs Skipped Skipped Jan 20, 2026 3:39pm

Request Review

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Jan 20, 2026

Greptile Summary

Fixed Gemini API error when tools return primitive values by wrapping them in objects. The Gemini API requires functionResponse.response to be a google.protobuf.Struct (object with string keys), but tools can return primitives (booleans, strings, numbers, null) or arrays which are not valid Struct values.

  • Added ensureStructResponse utility in apps/sim/providers/google/utils.ts that wraps primitives and arrays in { value: ... } format
  • Applied fix in two critical paths:
    1. Direct tool execution in apps/sim/providers/gemini/core.ts at line 108
    2. Message history conversion in convertToGeminiFormat at line 200
  • Added comprehensive test coverage with 24 unit tests covering all primitive types, arrays, objects, and edge cases

This ensures compatibility with Gemini's strict type requirements while preserving the ability for tools to return any JSON-serializable value.

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk
  • The fix addresses a specific API compatibility issue with a focused, well-tested solution. The implementation is clean with proper type safety, comprehensive test coverage (24 unit tests), and handles all edge cases. Both execution paths are covered.
  • No files require special attention

Important Files Changed

Filename Overview
apps/sim/providers/google/utils.ts Added ensureStructResponse utility to wrap primitives/arrays in { value: ... } and applied in convertToGeminiFormat for Gemini API compatibility
apps/sim/providers/gemini/core.ts Applied ensureStructResponse to tool execution results at line 108 to ensure proper Gemini API format
apps/sim/providers/google/utils.test.ts Comprehensive test coverage with 24 unit tests for ensureStructResponse and convertToGeminiFormat with primitive tool responses

Sequence Diagram

sequenceDiagram
    participant Tool as Tool Execution
    participant Core as Gemini Core
    participant Utils as ensureStructResponse
    participant API as Gemini API
    
    Tool->>Core: executeTool returns primitive (boolean/string/number/null)
    Core->>Utils: ensureStructResponse(result.output)
    
    alt Input is object (not array)
        Utils-->>Core: Return as-is
    else Input is primitive or array
        Utils-->>Core: Wrap in { value: ... }
    end
    
    Core->>API: functionResponse.response with wrapped value
    API-->>Core: Success (Struct format validated)
    
    Note over Utils,API: History path (convertToGeminiFormat)
    
    Tool->>Utils: Tool message with primitive content
    Utils->>Utils: JSON.parse(content)
    Utils->>Utils: ensureStructResponse(parsed)
    
    alt Parsed is object
        Utils-->>API: Return as-is
    else Parsed is primitive/array
        Utils-->>API: Wrap in { value: ... }
    end
Loading

@waleedlatif1 waleedlatif1 merged commit 07f0c01 into staging Jan 20, 2026
12 checks passed
@waleedlatif1 waleedlatif1 deleted the fix/google branch January 20, 2026 17:27
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