Skip to content

Commit 94a662a

Browse files
committed
feat(go-adk): apply Gemini maxOutputTokens generation config
The ADK Gemini model reads generation config from the per-request LLMRequest.Config rather than the model definition, so the ModelConfig-level maxOutputTokens was never applied by the Go ADK agent (only the Python runtime handled it via _GeminiGenerationConfigMixin). Wrap the native Gemini and Gemini Vertex AI models so max_output_tokens is injected into each request without overriding a per-request value. The wrapper is a no-op when maxOutputTokens is unset or non-positive. Signed-off-by: anthonytwh <[email protected]>
1 parent 75d90bf commit 94a662a

3 files changed

Lines changed: 164 additions & 2 deletions

File tree

‎go/adk/pkg/agent/agent.go‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,14 @@ func CreateLLM(ctx context.Context, m adk.Model, log logr.Logger) (adkmodel.LLM,
228228
if err != nil {
229229
return nil, fmt.Errorf("failed to build HTTP client for Gemini: %w", err)
230230
}
231-
return adkgemini.NewModel(ctx, modelName, &genai.ClientConfig{
231+
geminiModel, err := adkgemini.NewModel(ctx, modelName, &genai.ClientConfig{
232232
APIKey: apiKey,
233233
HTTPClient: httpClient,
234234
})
235+
if err != nil {
236+
return nil, err
237+
}
238+
return models.WrapGeminiWithGenerationConfig(geminiModel, m.MaxOutputTokens), nil
235239

236240
case *adk.GeminiVertexAI:
237241
project := os.Getenv("GOOGLE_CLOUD_PROJECT")
@@ -246,11 +250,15 @@ func CreateLLM(ctx context.Context, m adk.Model, log logr.Logger) (adkmodel.LLM,
246250
if modelName == "" {
247251
modelName = DefaultGeminiModel
248252
}
249-
return adkgemini.NewModel(ctx, modelName, &genai.ClientConfig{
253+
geminiModel, err := adkgemini.NewModel(ctx, modelName, &genai.ClientConfig{
250254
Backend: genai.BackendVertexAI,
251255
Project: project,
252256
Location: location,
253257
})
258+
if err != nil {
259+
return nil, err
260+
}
261+
return models.WrapGeminiWithGenerationConfig(geminiModel, m.MaxOutputTokens), nil
254262

255263
case *adk.Anthropic:
256264
modelName := m.Model

‎go/adk/pkg/models/gemini.go‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Package models: helpers for the native Gemini / Gemini Vertex AI models.
2+
//
3+
// The ADK Gemini model reads its generation config from the per-request
4+
// LLMRequest.Config rather than from the model definition, so a
5+
// ModelConfig-level setting such as maxOutputTokens is not otherwise applied.
6+
// geminiGenerationConfigModel wraps the ADK model and injects the setting into
7+
// each request, mirroring the Python _GeminiGenerationConfigMixin.
8+
package models
9+
10+
import (
11+
"context"
12+
"iter"
13+
14+
"google.golang.org/adk/model"
15+
"google.golang.org/genai"
16+
)
17+
18+
// geminiGenerationConfigModel wraps an ADK Gemini model.LLM and applies a
19+
// ModelConfig-level generation config (currently max_output_tokens) to each
20+
// request. A per-request value always wins: the cap is only applied when the
21+
// request does not already set MaxOutputTokens.
22+
type geminiGenerationConfigModel struct {
23+
model.LLM
24+
maxOutputTokens int32
25+
}
26+
27+
// WrapGeminiWithGenerationConfig wraps inner so that maxOutputTokens is applied
28+
// to requests that don't already set it. When maxOutputTokens is nil or not
29+
// positive, inner is returned unchanged.
30+
func WrapGeminiWithGenerationConfig(inner model.LLM, maxOutputTokens *int) model.LLM {
31+
if maxOutputTokens == nil || *maxOutputTokens <= 0 {
32+
return inner
33+
}
34+
return &geminiGenerationConfigModel{
35+
LLM: inner,
36+
maxOutputTokens: int32(*maxOutputTokens),
37+
}
38+
}
39+
40+
// GenerateContent injects the configured max_output_tokens into the request
41+
// config, without overriding a value the request already set, then delegates to
42+
// the wrapped model.
43+
func (m *geminiGenerationConfigModel) GenerateContent(ctx context.Context, req *model.LLMRequest, stream bool) iter.Seq2[*model.LLMResponse, error] {
44+
if req != nil && m.maxOutputTokens > 0 {
45+
if req.Config == nil {
46+
req.Config = &genai.GenerateContentConfig{}
47+
}
48+
if req.Config.MaxOutputTokens == 0 {
49+
req.Config.MaxOutputTokens = m.maxOutputTokens
50+
}
51+
}
52+
return m.LLM.GenerateContent(ctx, req, stream)
53+
}

‎go/adk/pkg/models/gemini_test.go‎

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package models
2+
3+
import (
4+
"context"
5+
"iter"
6+
"testing"
7+
8+
"google.golang.org/adk/model"
9+
"google.golang.org/genai"
10+
)
11+
12+
// fakeLLM records the request it was called with and returns an empty stream.
13+
type fakeLLM struct {
14+
name string
15+
gotReq *model.LLMRequest
16+
gotCall bool
17+
}
18+
19+
func (f *fakeLLM) Name() string { return f.name }
20+
21+
func (f *fakeLLM) GenerateContent(_ context.Context, req *model.LLMRequest, _ bool) iter.Seq2[*model.LLMResponse, error] {
22+
f.gotCall = true
23+
f.gotReq = req
24+
return func(yield func(*model.LLMResponse, error) bool) {}
25+
}
26+
27+
func drain(seq iter.Seq2[*model.LLMResponse, error]) {
28+
for range seq { //nolint:revive // consume the iterator
29+
}
30+
}
31+
32+
func intPtr(i int) *int { return &i }
33+
34+
func TestWrapGeminiWithGenerationConfig_NoOpWhenUnset(t *testing.T) {
35+
inner := &fakeLLM{name: "gemini-2.0-flash"}
36+
for _, tc := range []struct {
37+
name string
38+
max *int
39+
}{
40+
{"nil", nil},
41+
{"zero", intPtr(0)},
42+
{"negative", intPtr(-1)},
43+
} {
44+
t.Run(tc.name, func(t *testing.T) {
45+
got := WrapGeminiWithGenerationConfig(inner, tc.max)
46+
if got != model.LLM(inner) {
47+
t.Errorf("expected inner model returned unchanged, got wrapper %T", got)
48+
}
49+
})
50+
}
51+
}
52+
53+
func TestGeminiGenerationConfig_AppliesCapWhenUnset(t *testing.T) {
54+
inner := &fakeLLM{name: "gemini-2.0-flash"}
55+
wrapped := WrapGeminiWithGenerationConfig(inner, intPtr(1024))
56+
57+
req := &model.LLMRequest{Config: &genai.GenerateContentConfig{}}
58+
drain(wrapped.GenerateContent(context.Background(), req, false))
59+
60+
if !inner.gotCall {
61+
t.Fatal("wrapped model did not delegate to inner")
62+
}
63+
if got := inner.gotReq.Config.MaxOutputTokens; got != 1024 {
64+
t.Errorf("MaxOutputTokens = %d, want 1024", got)
65+
}
66+
}
67+
68+
func TestGeminiGenerationConfig_NilConfigInitialized(t *testing.T) {
69+
inner := &fakeLLM{name: "gemini-2.0-flash"}
70+
wrapped := WrapGeminiWithGenerationConfig(inner, intPtr(512))
71+
72+
req := &model.LLMRequest{} // Config is nil
73+
drain(wrapped.GenerateContent(context.Background(), req, false))
74+
75+
if inner.gotReq.Config == nil {
76+
t.Fatal("Config was not initialized")
77+
}
78+
if got := inner.gotReq.Config.MaxOutputTokens; got != 512 {
79+
t.Errorf("MaxOutputTokens = %d, want 512", got)
80+
}
81+
}
82+
83+
func TestGeminiGenerationConfig_DoesNotOverridePerRequestValue(t *testing.T) {
84+
inner := &fakeLLM{name: "gemini-2.0-flash"}
85+
wrapped := WrapGeminiWithGenerationConfig(inner, intPtr(1024))
86+
87+
req := &model.LLMRequest{Config: &genai.GenerateContentConfig{MaxOutputTokens: 256}}
88+
drain(wrapped.GenerateContent(context.Background(), req, false))
89+
90+
if got := inner.gotReq.Config.MaxOutputTokens; got != 256 {
91+
t.Errorf("MaxOutputTokens = %d, want 256 (per-request value must win)", got)
92+
}
93+
}
94+
95+
func TestGeminiGenerationConfig_NamePassthrough(t *testing.T) {
96+
inner := &fakeLLM{name: "gemini-2.0-flash"}
97+
wrapped := WrapGeminiWithGenerationConfig(inner, intPtr(1024))
98+
if got := wrapped.Name(); got != "gemini-2.0-flash" {
99+
t.Errorf("Name() = %q, want %q", got, "gemini-2.0-flash")
100+
}
101+
}

0 commit comments

Comments
 (0)