Skip to content

Commit 40144da

Browse files
wanlin31copybara-github
authored andcommitted
No public description
PiperOrigin-RevId: 766719630
1 parent 36d6d87 commit 40144da

File tree

2 files changed

+78
-4
lines changed

2 files changed

+78
-4
lines changed

client.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package genai
1717
import (
1818
"context"
1919
"fmt"
20+
"log"
2021
"net/http"
2122
"os"
2223
"strings"
@@ -74,7 +75,7 @@ func (t Backend) String() string {
7475
// ClientConfig is the configuration for the GenAI client.
7576
type ClientConfig struct {
7677
// Optional. API Key for GenAI. Required for BackendGeminiAPI.
77-
// Can also be set via the GOOGLE_API_KEY environment variable.
78+
// Can also be set via the GOOGLE_API_KEY or GEMINI_API_KEY environment variable.
7879
// Get a Gemini API key: https://ai.google.dev/gemini-api/docs/api-key
7980
APIKey string
8081

@@ -115,6 +116,9 @@ func defaultEnvVarProvider() map[string]string {
115116
if v, ok := os.LookupEnv("GOOGLE_API_KEY"); ok {
116117
vars["GOOGLE_API_KEY"] = v
117118
}
119+
if v, ok := os.LookupEnv("GEMINI_API_KEY"); ok {
120+
vars["GEMINI_API_KEY"] = v
121+
}
118122
if v, ok := os.LookupEnv("GOOGLE_CLOUD_PROJECT"); ok {
119123
vars["GOOGLE_CLOUD_PROJECT"] = v
120124
}
@@ -133,6 +137,18 @@ func defaultEnvVarProvider() map[string]string {
133137
return vars
134138
}
135139

140+
func getAPIKeyFromEnv(envVars map[string]string) string {
141+
googleAPIKey := envVars["GOOGLE_API_KEY"]
142+
geminiAPIKey := envVars["GEMINI_API_KEY"]
143+
if googleAPIKey != "" && geminiAPIKey != "" {
144+
log.Printf("Warning: Both GOOGLE_API_KEY and GEMINI_API_KEY are set. Using GOOGLE_API_KEY.")
145+
}
146+
if googleAPIKey != "" {
147+
return googleAPIKey
148+
}
149+
return geminiAPIKey
150+
}
151+
136152
// NewClient creates a new GenAI client.
137153
//
138154
// You can configure the client by passing in a ClientConfig struct.
@@ -142,7 +158,10 @@ func defaultEnvVarProvider() map[string]string {
142158
//
143159
// - Environment Variables for BackendGeminiAPI:
144160
//
145-
// - GOOGLE_API_KEY: Required. Specifies the API key for the Gemini API.
161+
// - GEMINI_API_KEY: Specifies the API key for the Gemini API.
162+
//
163+
// - GOOGLE_API_KEY: Can also be used to specify the API key for the Gemini API.
164+
// If both GOOGLE_API_KEY and GEMINI_API_KEY are set, GOOGLE_API_KEY will be used.
146165
//
147166
// - Environment Variables for BackendVertexAI:
148167
//
@@ -188,7 +207,9 @@ func NewClient(ctx context.Context, cc *ClientConfig) (*Client, error) {
188207

189208
// Only set the API key for MLDev API.
190209
if cc.APIKey == "" && cc.Backend == BackendGeminiAPI {
191-
cc.APIKey = envVars["GOOGLE_API_KEY"]
210+
if apiKey := getAPIKeyFromEnv(envVars); apiKey != "" {
211+
cc.APIKey = apiKey
212+
}
192213
}
193214
if cc.Project == "" {
194215
cc.Project = envVars["GOOGLE_CLOUD_PROJECT"]

client_test.go

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ func TestNewClient(t *testing.T) {
291291
}
292292
})
293293

294-
t.Run("API Key from environment", func(t *testing.T) {
294+
t.Run("API Key from GOOGLE_API_KEY only", func(t *testing.T) {
295295
apiKey := "test-api-key-env"
296296
client, err := NewClient(ctx, &ClientConfig{Backend: BackendGeminiAPI,
297297
envVarProvider: func() map[string]string {
@@ -307,6 +307,59 @@ func TestNewClient(t *testing.T) {
307307
t.Errorf("Expected API key %q, got %q", apiKey, client.clientConfig.APIKey)
308308
}
309309
})
310+
t.Run("API Key from GEMINI_API_KEY only", func(t *testing.T) {
311+
apiKey := "test-api-key-env"
312+
client, err := NewClient(ctx, &ClientConfig{Backend: BackendGeminiAPI,
313+
envVarProvider: func() map[string]string {
314+
return map[string]string{
315+
"GEMINI_API_KEY": apiKey,
316+
}
317+
},
318+
})
319+
if err != nil {
320+
t.Fatalf("Expected no error, got %v", err)
321+
}
322+
if client.clientConfig.APIKey != apiKey {
323+
t.Errorf("Expected API key %q, got %q", apiKey, client.clientConfig.APIKey)
324+
}
325+
})
326+
327+
t.Run("API Key from GEMINI_API_KEY and GOOGLE_API_KEY as empty string", func(t *testing.T) {
328+
apiKey := "test-api-key-env"
329+
client, err := NewClient(ctx, &ClientConfig{Backend: BackendGeminiAPI,
330+
envVarProvider: func() map[string]string {
331+
return map[string]string{
332+
"GOOGLE_API_KEY": "",
333+
"GEMINI_API_KEY": apiKey,
334+
}
335+
},
336+
})
337+
if err != nil {
338+
t.Fatalf("Expected no error, got %v", err)
339+
}
340+
if client.clientConfig.APIKey != apiKey {
341+
t.Errorf("Expected API key %q, got %q", apiKey, client.clientConfig.APIKey)
342+
}
343+
})
344+
345+
t.Run("API Key both GEMINI_API_KEY and GOOGLE_API_KEY", func(t *testing.T) {
346+
geminiAPIKey := "gemini-api-key-env"
347+
googleAPIKey := "google-api-key-env"
348+
client, err := NewClient(ctx, &ClientConfig{Backend: BackendGeminiAPI,
349+
envVarProvider: func() map[string]string {
350+
return map[string]string{
351+
"GOOGLE_API_KEY": googleAPIKey,
352+
"GEMINI_API_KEY": geminiAPIKey,
353+
}
354+
},
355+
})
356+
if err != nil {
357+
t.Fatalf("Expected no error, got %v", err)
358+
}
359+
if client.clientConfig.APIKey != googleAPIKey {
360+
t.Errorf("Expected APIggcg key %q, got %q", googleAPIKey, client.clientConfig.APIKey)
361+
}
362+
})
310363

311364
t.Run("Base URL from HTTPOptions", func(t *testing.T) {
312365
baseURL := "https://test-base-url.com/"

0 commit comments

Comments
 (0)