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

Same ambiguity as the chat variant but raised by the Google GenAI image connection auto-configuration: both an API key and Vertex AI configuration are detected, so the connection defaults to the Gemini Developer API (API key) and warns how to switch to Vertex AI.

Source

Thrown at auto-configurations/models/spring-ai-autoconfigure-model-google-genai/src/main/java/org/springframework/ai/model/google/genai/autoconfigure/image/GoogleGenAiImageConnectionAutoConfiguration.java:67

	@ConditionalOnMissingBean
	public GoogleGenAiImageConnectionDetails googleGenAiImageConnectionDetails(
			GoogleGenAiImageConnectionProperties connectionProperties) throws IOException {

		var connectionBuilder = GoogleGenAiImageConnectionDetails.builder();

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

		// Ambiguity Guard: Professional logging
		if (hasApiKey && hasVertexConfig) {
			if (connectionProperties.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 (connectionProperties.isVertexAi()) {
			if (!hasVertexConfig) {
				throw new IllegalStateException(
						"Vertex AI mode requires both 'project-id' and 'location' to be configured.");
			}
			configureVertexAi(connectionBuilder, connectionProperties);
		}
		else if (hasApiKey) {
			connectionBuilder.apiKey(connectionProperties.getApiKey());
		}
		else if (hasVertexConfig) {
			logger.debug("Project ID and Location detected. Defaulting to Vertex AI mode.");
			configureVertexAi(connectionBuilder, connectionProperties);

View on GitHub (pinned to 98a7beda4f)

Solutions

  1. Set the vertex-ai property to true to explicitly select Vertex AI for image generation.
  2. Remove the API key property/env var if Vertex AI credentials should be used.
  3. Remove Vertex AI project/location settings if the Developer API is intended.
  4. Keep only one credential source per profile to avoid ambiguity.

Example fix

// before (image config, ambiguous)
spring.ai.google.genai.image.api-key=${GOOGLE_API_KEY}
spring.ai.google.genai.image.project-id=my-gcp-project
// after (explicit Developer API mode)
spring.ai.google.genai.image.api-key=${GOOGLE_API_KEY}
# remove project-id/location, or set vertex-ai=true to use Vertex AI
Defensive patterns

Strategy: validation

Validate before calling

boolean hasApiKey = env.containsProperty("spring.ai.google.genai.image.api-key");
boolean hasVertex = env.containsProperty("spring.ai.google.genai.image.project-id");
if (hasApiKey && hasVertex && !env.getProperty("spring.ai.google.genai.image.vertex-ai", Boolean.class, false)) {
    throw new IllegalStateException("Image GenAI config is ambiguous: pick API key or Vertex AI explicitly");
}

Prevention

When it happens

Trigger: Both spring.ai.google.genai.image (or shared) api-key and Vertex AI project/location credentials are present while spring.ai.google.genai.image.vertex-ai (vertex-ai property) is false/unset, when the googleGenAiImageConnectionDetails bean is created.

Common situations: GOOGLE_API_KEY/GEMINI_API_KEY present in the environment on a GCP VM with ADC; shared GenAI properties carrying credentials for both backends; switching image generation from Developer API to Vertex AI without deleting the API key.

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/4ae1a7755e6f62d8. Report an issue: GitHub.