spring-ai icon indicating copy to clipboard operation
spring-ai copied to clipboard

spring.ai.google.genai.credentials-uri not applied to generated client

Open Alien2150 opened this issue 10 months ago • 2 comments

Bug description The property spring.ai.google.genai.credentials-uri is parsed by GoogleGenAiChatAutoConfiguration, but the credentials loaded from the URI are not actually applied to the generated API client. Although GoogleCredentials.fromStream(...) is called, the resulting credentials object is never set on the client builder, and no call to setCredentials(...) is made. Consequently, when GOOGLE_APPLICATION_CREDENTIALS is not set, the client falls back to GoogleCredentials.getApplicationDefault(), which often fails in environments such as Kubernetes/EKS.

This contradicts the documented/expected behavior that configuring spring.ai.google.genai.credentials-uri should allow the GenAI client to use the specified credentials file.

Environment

  • Spring AI version: 1.1.2
  • google-genai 1.32.0

Steps to reproduce Set spring.ai.google.genai.credentials-uri in application.yml to point to a mounted service account JSON file (e.g., /var/run/secrets/spring/eks-gcp-cred.json).

Include the spring-ai-starter-model-google-genai dependency. Do not set the GOOGLE_APPLICATION_CREDENTIALS environment variable. Deploy/run the application and attempt to inject or use the auto-configured Client or ChatModel. Observe the failure with an exception such as:

Failed to get application default credentials, please explicitly provide credentials.

Expected behavior When spring.ai.google.genai.credentials-uri is provided:

The Spring AI auto-configuration should load the credentials from the configured URI.

The loaded credentials should be applied to the generated API client, equivalent to calling clientBuilder.credentials(credentials).

The client should work without requiring GOOGLE_APPLICATION_CREDENTIALS to be set.

Minimal Complete Reproducible example application.yml:

spring:
  ai:
    google:
      genai:
        credentials-uri: "file:/var/run/secrets/spring/eks-gcp-cred.json"

service:

@Service
class GeminiServiceClient(private val client: Client) {
    fun ask(prompt: String): String {
        return client.chat("gemini-1.5-pro", prompt)
            .getResult()
            .getOutput()
            .getContent()
    }
}

Alien2150 avatar Jan 14 '26 11:01 Alien2150

Related #5218

LoperLee avatar Jan 15 '26 13:01 LoperLee

I took a quick look at the current main branch. It looks like the chat auto-configuration path now applies credentials-uri to the Google GenAI Client.Builder, but the embedding path still appears to load GoogleCredentials without passing them into the generated client builder.

Specifically, GoogleGenAiEmbeddingConnectionAutoConfiguration reads GoogleCredentials, while GoogleGenAiEmbeddingConnectionDetails.Builder currently only wires projectId, location, apiKey, or a preconfigured Client.

Would a narrow PR be welcome to add credentials support to GoogleGenAiEmbeddingConnectionDetails and wire spring.ai.google.genai.embedding.credentials-uri through the embedding auto-configuration as well?

fw-coder-tl avatar Jun 13 '26 10:06 fw-coder-tl