🌐
Go Packages
pkg.go.dev › google.golang.org › genai
genai package - google.golang.org/genai - Go Packages
3 weeks ago - For example, with just a few lines of code, you can access Gemini's multimodal capabilities to generate text from text-and-image input. parts := []*genai.Part{ {Text: "What's this image about?"}, {InlineData: &genai.Blob{Data: imageBytes, MIMEType: "image/jpeg"}}, } result, err := ...
Packages
Go is an open source programming language that makes it easy to build simple, reliable, and efficient software.
Standard library
Common problems companies solve with Go · Stories about how and why companies use Go
Fmt package
Package fmt implements formatted I/O with functions analogous to C's printf and scanf.
net/http
Package http provides HTTP client and server implementations.
🌐
GitHub
github.com › googleapis › go-genai
GitHub - googleapis/go-genai: Google Gen AI Go SDK provides an interface for developers to integrate Google's generative models into their Go applications. · GitHub
May 22, 2026 - For example, with just a few lines of code, you can access Gemini's multimodal capabilities to generate text from text-and-image input. parts := []*genai.Part{ {Text: "What's this image about?"}, {InlineData: &genai.Blob{Data: imageBytes, MIMEType: ...
Starred by 1.1K users
Forked by 152 users
Languages   Go
Discussions

Mocking google/genai library
DISCLAIMER: I have never used the google/genai library. If you are creating another type with the GenerateContentStream method to mock the genai lib, and not sure how to return a iter.Seq2, note that iter.Seq2 is just func(yield func(K, V) bool). Here K would be *genai.GenerateContentResponse and V would be error. (They don't have to be a Key and Value pair) c.f. https://pkg.go.dev/iter#hdr-Iterators Another thing I would like to mention is that unless you are using every single method *genai.Client has, use smaller interfaces to mock it. Assuming that you use the three methods, create two small interfaces, e.g. Uploader and ContentGenerator, at the consumer. genai.Client.Models and genai.Client.Files will implicitly satisfy your interface, without having to create a wrapper struct. Now to mock them, you can create a InMemoryUploader, and InMemoryContentGenerator, and all you have to do is define 3 mock methods. i.e. You can cherry pick the methods from external libraries to an interface of your choice, and the external libraries will satisfy them. This allows smaller interfaces and easier mocking. More on reddit.com
🌐 r/golang
4
0
May 2, 2025
Can Go be used for machine learning and AI?
Yes it can and that's what I use it for every day. More on reddit.com
🌐 r/golang
24
28
May 22, 2024
Best Library for genAi? Any suggestion?
Using the official SDKs? https://github.com/google/generative-ai-go https://platform.openai.com/docs/libraries/community-libraries More on reddit.com
🌐 r/golang
3
0
July 27, 2024
How to Reuse Generative AI Client in Go?
Personally I would say the general rule of thumb is that a client SHOULD be able to be used freely from multiple goroutines without a problem, unless there's some good reason that's not possible. However, I would personally also go with a client MUST document whether or not that is legal, because in general, Go code that doesn't document what is legal should be assumed to be unsafe to use from multiple goroutines without locking. Just as a Go library should assume that a caller will add concurrency if they want it, a Go library should generally assume the user will add concurrency-safety if they want it, unless there's a reasonably good reason to assume the library should handle it itself (e.g., a library whose entire reason is concurrency-based like sync.Map). The page you link to is too much documentation for me to paw through to see if they do document it, but if they don't the only option is to figure it out yourself, unfortunately. Since that's probably an autogenerated client it could go either way. You could even end up in the unfortunate situation where it happens to be safe today but it's just coincidence and a future update could become unsafe because it's not technically "supported". More on reddit.com
🌐 r/golang
4
0
November 14, 2024
🌐
Go Packages
pkg.go.dev › github.com › google › generative-ai-go › genai
genai package - github.com/google/generative-ai-go/genai - Go Packages
May 2, 2025 - This example shows how to set the History field on ChatSession explicitly. package main import ( "context" "fmt" "log" "os" "github.com/google/generative-ai-go/genai" "google.golang.org/api/option" ) func main() { ctx := context.Background() client, err := genai.NewClient(ctx, option.WithA...
🌐
Go Packages
pkg.go.dev › cloud.google.com › go › vertexai › genai
genai package - cloud.google.com/go/vertexai/genai - Go Packages
April 9, 2026 - This example shows how to send multiple requests concurrently using goroutines. package main import ( "context" "fmt" "log" "cloud.google.com/go/vertexai/genai" ) func main() { ctx := context.Background() const projectID = "YOUR PROJECT ID" const location = "GCP LOCATION" client, err := ...
🌐
GitHub
github.com › google › generative-ai-go
GitHub - google/generative-ai-go: Go SDK for Google Generative AI · GitHub
With Gemini 2.0, we took the chance to create a single unified SDK for all developers who want to use Google's GenAI models (Gemini, Veo, Imagen, etc). As part of that process, we took all of the feedback from this SDK and what developers like about other SDKs in the ecosystem to create the Google Gen AI SDK. The Gemini API docs are fully updated to show examples of the new Google Gen AI SDK: Get started.
Starred by 856 users
Forked by 101 users
Languages   Go 98.7% | Shell 1.3%
🌐
GitHub
github.com › google › generative-ai-go › blob › main › genai › example_test.go
generative-ai-go/genai/example_test.go at main · google/generative-ai-go
resp, err := model.GenerateContent(ctx, genai.Text("List a few popular cookie recipes using this JSON schema."))
Author   google
🌐
Google
docs.cloud.google.com › gemini enterprise agent platform › google gen ai sdk
Google Gen AI SDK | Gemini Enterprise Agent Platform | Google Cloud Documentation
from google import genai # TODO(developer): Update below line API_KEY = "YOUR_API_KEY" client = genai.Client(vertexai=True, api_key=API_KEY) response = client.models.generate_content( model="gemini-3.5-flash", contents="Explain bubble sort to me.", ) print(response.text) # Example response: # Bubble Sort is a simple sorting algorithm that repeatedly steps through the list · The Google Gen AI SDK for Go is available on go.dev and GitHub: google-genai on go.dev · go-genai on GitHub · go get google.golang.org/genai ·
Find elsewhere
🌐
DEV Community
dev.to › pradumnasaraf › building-an-ai-powered-cli-with-golang-and-google-gemini-45a1
Building an AI-Powered CLI with Golang and Google Gemini - DEV Community
August 27, 2024 - package cmd import ( "context" "fmt" "log" "os" "github.com/google/generative-ai-go/genai" "github.com/spf13/cobra" "google.golang.org/api/option" ) // searchCmd represents the search command var searchCmd = &cobra.Command{ Use: "search", Short: "A brief description of your command", // Added the getResponse() function Run: func(cmd *cobra.Command, args []string) { getResponse() }, } func init() { rootCmd.AddCommand(searchCmd) } func getResponse() { ctx := context.Background() client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY"))) if err != nil { log.Fatal(err) } defer client.Close() model := client.GenerativeModel("gemini-1.5-flash") resp, err := model.GenerateContent(ctx, genai.Text("Write a story about a AI and magic")) if err != nil { log.Fatal(err) } fmt.Println(resp.Candidates[0].Content.Parts[0]) }
🌐
Go Packages
pkg.go.dev › github.com › zzxwill › generative-ai-go › genai
genai package - github.com/zzxwill/generative-ai-go/genai - Go Packages
This example shows how to set the History field on ChatSession explicitly. package main import ( "context" "fmt" "log" "os" "github.com/google/generative-ai-go/genai" "google.golang.org/api/option" ) func main() { ctx := context.Background() client, err := genai.NewClient(ctx, option.WithA...
🌐
CloudZenia Blogs
cloudzenia.com › home › blogs › how to implement gemini ai model in golang: a comprehensive tutorial
Implement Gemini AI Model in Golang: A Complete Guide | CloudZenia
December 4, 2025 - package main import ( "context" "encoding/json" "fmt" "log" "os" "github.com/google/generative-ai-go/genai" "github.com/joho/godotenv" "google.golang.org/api/option" ) type Content struct{ Parts []string `json:Parts` Role string `json:Role` } type Candidates struct { Content *Content `json:Content` } type ContentResponse struct{ Candidates *[]Candidates `json:Candidates` } func main() { //Load .env key err := godotenv.Load() if err != nil { log.Fatal("Error loading .env key") } api_key := os.Getenv("API_KEY") //initializing the gemini ctx := context.Background() client, err := genai.NewClient(
🌐
Go Packages
pkg.go.dev › cloud.google.com › go › vertexai
vertexai package - cloud.google.com/go/vertexai - Go Packages
April 9, 2026 - The Vertex AI Go SDK enables developers to use Google's state-of-the-art generative AI models (like Gemini) to build AI-powered features and applications. ... For example, with just a few lines of code, you can access Gemini's multimodal capabilities to generate text from text-and-image input.
🌐
Medium
tomelin-tech.medium.com › unveiling-ai-with-golang-and-google-gemini-dd185318b7ac
🚀 Unveiling AI with Golang and Google Gemini! ✨ | by Rafael Tomelin | Medium
May 23, 2025 - import ( "context" "errors" "fmt" "log" "github.com/google/generative-ai-go/genai" "google.golang.org/api/option" )
🌐
Google
docs.cloud.google.com › client libraries › package cloud.google.com/go/vertexai/genai (v0.18.0)
Package cloud.google.com/go/vertexai/genai (v0.18.0) | Go client libraries | Google Cloud Documentation
package main import ( "context" "fmt" "log" "cloud.google.com/go/vertexai/genai" ) // Your GCP project const projectID = "your-project" // A GCP location like "us-central1"; if you're using standard Google-published // models (like untuned Gemini ...
🌐
Medium
sraynitjsr.medium.com › golang-genai-integration-961687be314d
GoLang GenAI Integration. Using… | by Subhradeep Ray | Medium
April 21, 2025 - package main import ( "context" "encoding/json" "fmt" "log" "net/http" "os" "strings" "slices" "github.com/google/generative-ai-go/genai" "google.golang.org/api/iterator" "google.golang.org/api/option" ) var ( client *genai.Client ctx context.Context ) func initClient() { apiKey := os.Getenv("API_KEY") if apiKey == "" { log.Fatal("API_KEY not set in environment") } ctx = context.Background() var err error client, err = genai.NewClient(ctx, option.WithAPIKey(apiKey)) if err != nil { log.Fatalf("Failed to create genai client: %v", err) } } func getModels(w http.ResponseWriter, r *http.Request) {
🌐
Reddit
reddit.com › r/golang › mocking google/genai library
r/golang on Reddit: Mocking google/genai library
May 2, 2025 -

Hello everyone, I'm relatively new to Go development and currently facing challenges with testing.

I'm struggling to mock the libraries in the google/genai SDK. I tried to create a wrapper for abstraction.

package clients
import (
    "context"
    "google.golang.org/genai"
    "io"
    "iter"
)

type GenaiClientWrapper struct {
    *genai.Client
}

func NewGenaiClientWrapper(client *genai.Client) *GenaiClientWrapper {
    return &GenaiClientWrapper{Client: client}
}

func (c GenaiClientWrapper) GenerateContent(ctx context.Context, model string, contents []*genai.Content, config *genai.GenerateContentConfig) (*genai.GenerateContentResponse, error) {
    return c.Client.Models.GenerateContent(
       ctx,
       model,
       contents,
       config,
    )
}

func (c GenaiClientWrapper) GenerateContentStream(ctx context.Context, model string, contents []*genai.Content, config *genai.GenerateContentConfig) iter.Seq2[*genai.GenerateContentResponse, error] {
    return c.Client.Models.GenerateContentStream(
       ctx,
       model,
       contents,
       config,
    )
}

func (c GenaiClientWrapper) Upload(ctx context.Context, r io.Reader, config *genai.UploadFileConfig) (*genai.File, error) {
    return c.Client.Files.Upload(
       ctx,
       r,
       config,
    )
}

But i can't seem to find a way to mock the iter.Seq2 response. Has anyone tried to use the genai sdk in their projects? Is there a better way to implement the abstraction?

🌐
Eli Bendersky
eli.thegreenplace.net › 2023 › using-gemini-models-from-go
Using Gemini models from Go - Eli Bendersky's website
The Go SDK lives at https://github.com/google/generative-ai-go, with package documentation at https://pkg.go.dev/github.com/google/generative-ai-go; it has a good section of examples we can follow. ... package main import ( "context" "encoding/json" "fmt" "log" "os" "github.com/google/generative-ai-go/genai" "google.golang.org/api/option" ) func main() { ctx := context.Background() client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("API_KEY"))) if err != nil { log.Fatal(err) } defer client.Close() model := client.GenerativeModel("gemini-pro-vision") imgData1, err := os.ReadFile(".
🌐
GitHub
github.com › google › generative-ai-go › releases
Releases · google/generative-ai-go
genai: switch gen-examples flow to use go:generate by @eliben in #168 · genai: add more samples for CountTokens by @eliben in #167 · genai: adjust samples for count tokens by @eliben in #170 · genai: rename source snippets file to ...
Author   google
🌐
Liu Houliang
liuhouliang.com › en › post › go_gemini
Use Google Gemini API in Go - Liu Houliang
February 19, 2024 - import "github.com/google/generative-ai-go/genai" import "google.golang.org/api/option" ctx := context.Background() // Access your API key as an environment variable (see "Set up your API key" above) client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("API_KEY"))) if err != nil { log.Fatal(err) } defer client.Close() model := client.GenerativeModel("MODEL_NAME") When the prompt input includes only text, use the gemini-pro model with the GenerateContent method to generate text output: ctx := context.Background() // Access your API key as an environment variable (see "Set up your API