spring-projects/spring-ai · warning

Both API Key and Vertex AI config detected. Defaulting to Ge

Error message

Both API Key and Vertex AI config detected. Defaulting to Gemini Developer API (API Key). To use Vertex AI instead, set 'spring.ai.google.genai.vertex-ai=true'.

What it means

Spring AI's Google GenAI chat auto-configuration detected credentials for both the Gemini Developer API (API key) and Vertex AI in the configuration. Since both are present, the library defaults to the Gemini Developer API (API key) mode and warns that Vertex AI will not be used unless spring.ai.google.genai.vertex-ai=true is explicitly set.

Source

Thrown at auto-configurations/models/spring-ai-autoconfigure-model-google-genai/src/main/java/org/springframework/ai/model/google/genai/autoconfigure/chat/GoogleGenAiChatAutoConfiguration.java:85

	@Bean
	@ConditionalOnMissingBean
	public Client googleGenAiClient(GoogleGenAiConnectionProperties properties) throws IOException {
		Client.Builder builder = Client.builder();

		boolean hasApiKey = StringUtils.hasText(properties.getApiKey());
		boolean hasProject = StringUtils.hasText(properties.getProjectId());
		boolean hasLocation = StringUtils.hasText(properties.getLocation());
		boolean hasVertexConfig = hasProject && hasLocation;

		// Ambiguity Guard: Professional logging
		if (hasApiKey && hasVertexConfig) {
			if (properties.isVertexAi()) {
				logger.info(
						"Both API Key and Vertex AI config detected. Vertex AI mode is explicitly enabled; the API key will be ignored.");
			}
			else {
				logger.warn("Both API Key and Vertex AI config detected. Defaulting to Gemini Developer API (API Key). "
						+ "To use Vertex AI instead, set 'spring.ai.google.genai.vertex-ai=true'.");
			}
		}

		// Mode Selection with Fail-Fast Validation
		if (properties.isVertexAi()) {
			if (!hasVertexConfig) {
				throw new IllegalStateException(
						"Vertex AI mode requires both 'project-id' and 'location' to be configured.");
			}
			configureVertexAi(builder, properties);
		}
		else if (hasApiKey) {
			builder.apiKey(properties.getApiKey());
		}
		else if (hasVertexConfig) {
			logger.debug("Project ID and Location detected. Defaulting to Vertex AI mode.");
			configureVertexAi(builder, properties);

View on GitHub (pinned to 98a7beda4f)

Solutions

  1. Set spring.ai.google.genai.vertex-ai=true if Vertex AI is the intended backend.
  2. Remove spring.ai.google.genai.api-key (and GOOGLE_API_KEY/GEMINI_API_KEY env vars) if Vertex AI should be used implicitly.
  3. Remove the Vertex AI project/location config if the Developer API with an API key is intended.
  4. Set spring.ai.google.genai.api-key explicitly (not via environment) to make intent unambiguous.

Example fix

// before (ambiguous: both credentials present)
spring.ai.google.genai.api-key=${GEMINI_API_KEY}
spring.ai.google.genai.project-id=my-gcp-project
// after (explicit Vertex AI mode)
spring.ai.google.genai.vertex-ai=true
spring.ai.google.genai.project-id=my-gcp-project
# api-key removed
Defensive patterns

Strategy: validation

Validate before calling

boolean hasApiKey = env.containsProperty("spring.ai.google.genai.api-key") || env.getProperty("GOOGLE_API_KEY") != null;
boolean hasVertex = env.containsProperty("spring.ai.google.genai.project-id");
if (hasApiKey && hasVertex && !env.getProperty("spring.ai.google.genai.vertex-ai", Boolean.class, false)) {
    throw new IllegalStateException("Provide only one GenAI credential mode or set vertex-ai=true");
}

Prevention

When it happens

Trigger: Both an API key (e.g. spring.ai.google.genai.api-key or GOOGLE_API_KEY/GEMINI_API_KEY env var) and Vertex AI config (project id/location and OAuth credentials) are present while spring.ai.google.genai.vertex-ai is false or unset, when googleGenAiClient bean is created.

Common situations: GOOGLE_API_KEY or GEMINI_API_KEY set in the environment on GCP machines where ADC (Application Default Credentials) are also configured; copying example properties that include both options; migrating between the two APIs without removing the old credential.

Understand the failure class

Background: Conflicting config options: "cannot be used together" — configuration validation errors across open-source libraries — this error's family across 162 libraries.

Related errors


AI-assisted analysis of spring-projects/spring-ai@98a7beda4f (2026-09-11). Data as JSON: /api/errors/38389311b766886b. Report an issue: GitHub.