Skip to content

Commit 35d8898

Browse files
committed
fix(go/genkit): expose detailed 4xx errors in googlegenai plugin
1 parent 2023053 commit 35d8898

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

go/samples/basic-gemini/main.go

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,45 @@ package main
1717
import (
1818
"context"
1919
"errors"
20+
"fmt"
2021
"log"
22+
"os"
23+
"os/signal"
24+
"syscall"
2125

2226
"github.com/firebase/genkit/go/ai"
2327
"github.com/firebase/genkit/go/genkit"
2428
"github.com/firebase/genkit/go/plugins/googlegenai"
2529
)
2630

2731
func main() {
28-
ctx := context.Background()
32+
ctx, cancel := context.WithCancel(context.Background())
33+
defer cancel()
34+
35+
// Handle SIGINT (Ctrl+C) for graceful shutdown
36+
sigCh := make(chan os.Signal, 1)
37+
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
38+
go func() {
39+
<-sigCh
40+
log.Println("Received interrupt, shutting down...")
41+
cancel()
42+
}()
2943

3044
// Initialize Genkit with the Google AI plugin. When you pass nil for the
3145
// Config parameter, the Google AI plugin will get the API key from the
3246
// GEMINI_API_KEY or GOOGLE_API_KEY environment variable, which is the recommended
3347
// practice.
34-
g, err := genkit.Init(ctx, genkit.WithPlugins(&googlegenai.GoogleAI{}))
48+
// Initialize Genkit with the Google AI plugin using an invalid API key
49+
g, err := genkit.Init(ctx, genkit.WithPlugins(&googlegenai.GoogleAI{
50+
APIKey: "invalid-key-12345", // Explicitly set invalid key
51+
}))
3552
if err != nil {
36-
log.Fatal(err)
53+
log.Fatalf("Initialization failed: %v", err)
3754
}
55+
log.Println("Genkit initialized successfully")
3856

3957
// Define a simple flow that generates jokes about a given topic
40-
genkit.DefineFlow(g, "jokesFlow", func(ctx context.Context, input string) (string, error) {
58+
jokesFlow := genkit.DefineFlow(g, "jokesFlow", func(ctx context.Context, input string) (string, error) {
4159
m := googlegenai.GoogleAIModel(g, "gemini-2.5-pro-preview-03-25")
4260
if m == nil {
4361
return "", errors.New("jokesFlow: failed to find model")
@@ -50,12 +68,20 @@ func main() {
5068
}),
5169
ai.WithPrompt(`Tell silly short jokes about %s`, input))
5270
if err != nil {
53-
return "", err
71+
// Log detailed error
72+
log.Printf("Generate error: %v", err)
73+
return "", fmt.Errorf("failed to generate: %w", err)
5474
}
5575

5676
text := resp.Text()
5777
return text, nil
5878
})
5979

60-
<-ctx.Done()
80+
// Execute the flow to trigger the 4xx error
81+
log.Println("Running jokesFlow with input 'cats'")
82+
result, err := jokesFlow.Run(ctx, "cats")
83+
if err != nil {
84+
log.Fatalf("Flow failed: %v", err)
85+
}
86+
log.Printf("Flow result: %s", result)
6187
}

0 commit comments

Comments
 (0)