-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Description
Client
Vertex AI Generative (Go SDK, streaming)
Environment
- OS / Container: K8s on cloud service
- Runtime: Go 1.24.9
- Deployment: Kubernetes (long-lived pods)
- Auth: Service Account JSON via GOOGLE_APPLICATION_CREDENTIALS
Code and Dependencies
package main
import (
"context"
"fmt"
"google.golang.org/genai"
)
func main() {
ctx := context.Background()
client, err := genai.NewClient(ctx, &genai.ClientConfig{
Backend: genai.BackendVertexAI,
Project: "PROJECT_ID",
Location: "REGION",
})
if err != nil {
panic(err)
}
iter := client.Models.GenerateContentStream(ctx, "MODEL",
[]*genai.Content{
genai.NewContentFromText("Please produce a long response.", genai.RoleUser),
},
&genai.GenerateContentConfig{},
)
for resp, err := range iter {
if err != nil {
fmt.Printf("stream error: %v\n", err)
return
}
_ = resp
}
}
go.mod
module rpc
go 1.24.9
require (
google.golang.org/genai v1.40.0
google.golang.org/grpc v1.74.2
cloud.google.com/go/auth v0.16.2 // indirect
cloud.google.com/go v0.121.2 // indirect
// ... (other deps)
)
Expected behavior
Streaming should survive access token refresh; the SDK should renew tokens and keep the
active stream alive without returning UNAUTHENTICATED.
Actual behavior
When the access token expires during an already-established stream, the SDK returns
UNAUTHENTICATED with reason:ACCESS_TOKEN_EXPIRED and the stream ends.
Screenshots
Error 401, Message: Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project., Status: UNAUTHENTICATED, Details: [map[@type:type.googleapis.com/google.rpc.ErrorInfo domain:googleapis.com metadata:map[method:google.cloud.aiplatform.v1beta1.PredictionService.StreamGenerateContent service:aiplatform.googleapis.com] reason:ACCESS_TOKEN_EXPIRED]]
Additional context
- It's not always possible to reproduce the error. Observing the logs, there are occasionally a few concentrated error events over 5 days.
- Using service account JSON credentials via ADC (GOOGLE_APPLICATION_CREDENTIALS).
- Stream duration can exceed token lifetime.
- Error example: UNAUTHENTICATED: Request had invalid authentication credentials.
reason:ACCESS_TOKEN_EXPIRED.