Skip to content

refactor: migrate to new genai API and update Gemini provider#238

Merged
appleboy merged 3 commits intomainfrom
gemini
May 4, 2025
Merged

refactor: migrate to new genai API and update Gemini provider#238
appleboy merged 3 commits intomainfrom
gemini

Conversation

@appleboy
Copy link
Owner

@appleboy appleboy commented May 4, 2025

  • Replace deprecated google/generative-ai-go/genai package with google.golang.org/genai and update imports accordingly
  • Refactor Gemini provider: update API usage to reflect new genai client structure and methods
  • Update completion and function call logic to use explicit content/message structs rather than genai.Text
  • Add internal error handling for invalid function call responses
  • Improve usage metrics extraction with additional nil checks
  • Remove unused imports and update client initialization logic to match new genai API
  • Add new indirect dependencies in go.mod (google.golang.org/genai, github.com/google/go-cmp, github.com/gorilla/websocket)
  • Remove obsolete and unused dependencies in go.mod

Summary by CodeRabbit

  • New Features
    • Improved handling and validation of function call responses in summary prefix generation, with clearer error reporting for invalid responses.
  • Refactor
    • Updated to use the latest Google AI library for enhanced reliability and compatibility.
    • Streamlined response extraction and configuration for content generation.
    • Cleaned up internal dependencies for improved maintainability.

…ntation

- Replace deprecated google/generative-ai-go/genai package with google.golang.org/genai and update imports accordingly
- Refactor Gemini provider: update API usage to reflect new genai client structure and methods
- Update completion and function call logic to use explicit content/message structs rather than genai.Text
- Add internal error handling for invalid function call responses
- Improve usage metrics extraction with additional nil checks
- Remove unused imports and update client initialization logic to match new genai API
- Add new indirect dependencies in go.mod (google.golang.org/genai, github.com/google/go-cmp, github.com/gorilla/websocket)
- Remove obsolete and unused dependencies in go.mod

Signed-off-by: appleboy <[email protected]>
@coderabbitai
Copy link

coderabbitai bot commented May 4, 2025

Caution

Review failed

The pull request is closed.

Walkthrough

The changes update the Gemini provider's integration with Google's generative AI APIs. The dependency management in go.mod was revised, removing the old github.com/google/generative-ai-go package and related indirect dependencies, and switching to the new google.golang.org/genai package. The provider's implementation was refactored to use the new API, updating struct fields, method implementations, and error handling. The construction and handling of requests and responses were adapted to the new client and content model. Additionally, imports were cleaned up and a new sentinel error was introduced for invalid function call handling.

Changes

File(s) Change Summary
go.mod Updated dependencies: removed github.com/google/generative-ai-go and google.golang.org/api, cleaned up several indirect Google-related dependencies, added google.golang.org/genai and new utility indirect dependencies.
provider/gemini/func.go Changed import path for the genai package from github.com/google/generative-ai-go/genai to google.golang.org/genai.
provider/gemini/gemini.go Refactored to use the new genai.Client API: changed Client.client field type, added topP field, rewrote Completion and GetSummaryPrefix methods to use new content generation and function call APIs, improved error handling, introduced ErrInvalidFunctionCall, cleaned imports, and updated constructor New.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant GeminiClient as Gemini Provider
    participant GenAI as Google GenAI API

    User->>GeminiClient: Completion(ctx, content)
    GeminiClient->>GenAI: GenerateContent(model, content, config)
    GenAI-->>GeminiClient: Response (text, usage metadata)
    GeminiClient-->>User: core.Response (text, usage)

    User->>GeminiClient: GetSummaryPrefix(ctx, content)
    GeminiClient->>GenAI: GenerateContent(model, content, config with function call)
    GenAI-->>GeminiClient: Response (function call result)
    GeminiClient-->>User: core.Response (prefix or error)
Loading

Poem

In the warren, code hops anew,
With genai’s path now shining through.
Old dependencies, swept aside,
New clients found and errors tried.
From function calls to text replies,
The Gemini rabbit leaps and flies!
🐇✨

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 golangci-lint (1.64.8)

Error: you are using a configuration file for golangci-lint v2 with golangci-lint v1: please use golangci-lint v2
Failed executing command with error: you are using a configuration file for golangci-lint v2 with golangci-lint v1: please use golangci-lint v2


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 90fbcc3 and 94d57ac.

📒 Files selected for processing (1)
  • provider/gemini/gemini.go (3 hunks)
✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🔭 Outside diff range comments (2)
go.mod (1)

3-3: ⚠️ Potential issue

go 1.24 will break builds on current Go tool-chains

The latest publicly released version is 1.22. Declaring an unreleased major version prevents go commands (including CI, IDE tooling and third-party modules) from working unless contributors run a development build of Go.

Unless you really need a bleeding-edge feature gated to 1.24, roll this back to the latest stable tag (go 1.22) so the module can be consumed everywhere.

provider/gemini/gemini.go (1)

17-26: 🛠️ Refactor suggestion

debug struct field is dead code and fails linting

golangci-lint already flags it (unused). Either (a) wire it into meaningful behaviour or (b) drop the field to keep the struct minimal.

-type Client struct {
-	client      *genai.Client
-	model       string
-	maxTokens   int32
-	temperature float32
-	topP        float32
-	debug       bool
-}
+type Client struct {
+	client      *genai.Client
+	model       string
+	maxTokens   int32
+	temperature float32
+	topP        float32
+}
🧰 Tools
🪛 GitHub Check: lint

[failure] 25-25:
field debug is unused (unused)

🪛 GitHub Actions: Lint and Testing

[error] 25-25: golangci-lint: field debug is unused (unused)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 76cda3c and 2e76ef0.

⛔ Files ignored due to path filters (1)
  • go.sum is excluded by !**/*.sum
📒 Files selected for processing (3)
  • go.mod (2 hunks)
  • provider/gemini/func.go (1 hunks)
  • provider/gemini/gemini.go (3 hunks)
🧰 Additional context used
🪛 GitHub Actions: Lint and Testing
provider/gemini/gemini.go

[error] 25-25: golangci-lint: field debug is unused (unused)

⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Analyze (go)
🔇 Additional comments (3)
go.mod (1)

36-41:

Details

❓ Verification inconclusive

Double-check that the new indirect deps are actually needed

github.com/google/go-cmp and github.com/gorilla/websocket suddenly appear as indirect requirements. Their presence is fine if they are truly pulled in via the new genai client, but they often creep in through test-only or transitive paths and can later be pruned by running

Run the tidy command and commit the resulting go.mod / go.sum to avoid dragging unused code into downstream consumers.


🏁 Script executed:

go mod tidy

Length of output: 4485


Commit go mod tidy results and verify indirect deps

The go mod tidy run didn’t remove github.com/google/go-cmp or github.com/gorilla/websocket, so they remain as indirect requirements. Please:

  • Confirm these modules are genuinely pulled in (e.g. by the new genai client) rather than through a test-only or spurious transitive path.
  • Run git diff go.mod go.sum and commit any changes from go mod tidy to prevent unused dependencies from leaking downstream.
provider/gemini/func.go (1)

3-3: Import path update looks good – just remember to do a full build test

google.golang.org/genai replaces the deprecated path, which is correct.
A quick go vet ./... after this change will flush out any lingering references to the old package.

provider/gemini/gemini.go (1)

30-47:

Details

✅ Verification successful

Possible API mismatch: client.Models.GenerateContent signature

The v1.x examples for google.golang.org/genai show usage like:

model := client.GenerativeModel("gemini-pro")
resp, err := model.GenerateContent(ctx, parts...)

Here the call is:

resp, err := c.client.Models.GenerateContent(ctx, c.model, data, cfg)

If Models.GenerateContent does not exist (or its parameter list differs), this will not compile. Please run:

and adjust to the idiomatic pattern if needed (e.g. obtain a model := c.client.GenerativeModel(c.model) first).


🏁 Script executed:

go vet ./...

Length of output: 14


🏁 Script executed:

#!/bin/bash
# Find definition of Models field/type and GenerateContent method signature
set -e

echo "Searching for client type definition and Models field..."
rg "type .*Client" -A5 -B5

echo -e "\nSearching for 'Models' usage in the client struct..."
rg "Models" -A5 -B5

echo -e "\nSearching for GenerateContent method definition..."
rg "GenerateContent(" -R -A3 -n

Length of output: 6186


🏁 Script executed:

#!/bin/bash
# Inspect go.mod for google.golang.org/genai requirement
if [ -f go.mod ]; then
  echo "go.mod entries for genai:"
  rg "google.golang.org/genai" -n go.mod || true

  echo -e "\nListing installed modules including genai:"
  go list -m all | grep "google.golang.org/genai" || true
else
  echo "No go.mod file found in repository root."
fi

Length of output: 334


No API mismatch found; Models.GenerateContent is valid in v1.3.0
go vet ./... ran without errors on google.golang.org/genai v1.3.0, confirming that c.client.Models.GenerateContent(ctx, c.model, data, cfg) matches the published API. No changes required.

appleboy added 2 commits May 4, 2025 09:54
- Add github.com/yassinebenaid/godump v0.11.1 as a new dependency
- Import godump in the gemini provider code
- Dump Gemini API response candidates to console when debug mode is enabled

Signed-off-by: appleboy <[email protected]>
- Remove the ErrInvalidFunctionCall error declaration
- Refactor error handling in GetSummaryPrefix to provide more granular error messages for missing candidates, content, function calls, or prefix values
- Store the extracted prefix in a variable to simplify assignment to the response object

Signed-off-by: appleboy <[email protected]>
@appleboy appleboy merged commit 3e76cf1 into main May 4, 2025
10 of 11 checks passed
@appleboy appleboy deleted the gemini branch May 4, 2025 02:00
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.

1 participant