Skip to content

Commit 4e7d00a

Browse files
authored
fix(go/plugins/googlegenai): handle empty candidate parts when thinking (#3754)
1 parent 72ab0d7 commit 4e7d00a

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

go/plugins/googlegenai/gemini.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ func generate(
300300
if err != nil {
301301
return nil, err
302302
}
303-
for i, c := range chunk.Candidates {
303+
for _, c := range chunk.Candidates {
304304
tc, err := translateCandidate(c)
305305
if err != nil {
306306
return nil, err
@@ -311,8 +311,7 @@ func generate(
311311
if err != nil {
312312
return nil, err
313313
}
314-
// stream only supports text
315-
chunks = append(chunks, c.Content.Parts[i])
314+
chunks = append(chunks, c.Content.Parts...)
316315
}
317316
// keep the last chunk for usage metadata
318317
resp = chunk

go/plugins/googlegenai/googleai_live_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,45 @@ func TestGoogleAILive(t *testing.T) {
491491
t.Fatalf("thoughts tokens should not be zero or greater than 100, got: %d", resp.Usage.ThoughtsTokens)
492492
}
493493
})
494+
t.Run("thinking stream with structured output", func(t *testing.T) {
495+
type Output struct {
496+
Text string `json:"text"`
497+
}
498+
499+
m := googlegenai.GoogleAIModel(g, "gemini-2.5-flash")
500+
resp, err := genkit.Generate(ctx, g,
501+
ai.WithConfig(genai.GenerateContentConfig{
502+
Temperature: genai.Ptr[float32](0.4),
503+
ThinkingConfig: &genai.ThinkingConfig{
504+
IncludeThoughts: true,
505+
ThinkingBudget: genai.Ptr[int32](1024),
506+
},
507+
}),
508+
ai.WithModel(m),
509+
ai.WithOutputType(Output{}),
510+
ai.WithPrompt("Analogize photosynthesis and growing up."),
511+
ai.WithStreaming(func(ctx context.Context, chunk *ai.ModelResponseChunk) error {
512+
return nil
513+
}),
514+
)
515+
if err != nil {
516+
t.Fatal(err)
517+
}
518+
if resp == nil {
519+
t.Fatal("nil response obtanied")
520+
}
521+
var out Output
522+
err = resp.Output(&out)
523+
if err != nil {
524+
t.Fatalf("unable to unmarshal response: %v", err)
525+
}
526+
if resp.Usage.ThoughtsTokens == 0 || resp.Usage.ThoughtsTokens > 1024 {
527+
t.Fatalf("thoughts tokens should not be zero or greater than 1024, got: %d", resp.Usage.ThoughtsTokens)
528+
}
529+
if resp.Reasoning() == "" {
530+
t.Fatalf("no reasoning found")
531+
}
532+
})
494533
t.Run("thinking disabled", func(t *testing.T) {
495534
m := googlegenai.GoogleAIModel(g, "gemini-2.5-flash")
496535
resp, err := genkit.Generate(ctx, g,

0 commit comments

Comments
 (0)