Skip to main content
Version: 2.1.1 (Latest)

Configuration

This section covers the configuration options for Spring AI Watsonx.ai integration, including connection properties, authentication, and advanced configuration scenarios.

Connection Properties​

The Spring AI Watsonx.ai integration requires several connection properties to authenticate and connect to your Watsonx.ai service instance.

Basic Configuration​

Add these properties to your application.properties or application.yml file:

application.properties

# Required connection properties
spring.ai.watsonx.ai.api-key=${WATSONX_AI_API_KEY}
spring.ai.watsonx.ai.base-url=${WATSONX_AI_BASE_URL}
spring.ai.watsonx.ai.project-id=${WATSONX_AI_PROJECT_ID}

# Optional: Enable specific models
spring.ai.watsonx.ai.chat.enabled=true
spring.ai.watsonx.ai.embedding.enabled=true
spring.ai.watsonx.ai.moderation.enabled=true
spring.ai.watsonx.ai.rerank.enabled=true
spring.ai.watsonx.ai.text-extraction.enabled=true

application.yml

spring:
ai:
watsonx:
ai:
api-key: ${WATSONX_AI_API_KEY}
base-url: ${WATSONX_AI_BASE_URL}
project-id: ${WATSONX_AI_PROJECT_ID}
chat:
enabled: true
options:
model: ibm/granite-4-h-small
temperature: 0.7
embedding:
enabled: true
options:
model: ibm/granite-embedding-278m-multilingual
moderation:
enabled: true
rerank:
enabled: true
options:
model: intfloat/multilingual-e5-large
text-extraction:
enabled: true

Environment Variables​

It's recommended to use environment variables for sensitive configuration:

export WATSONX_AI_API_KEY=your_api_key_here
export WATSONX_AI_BASE_URL=https://us-south.ml.cloud.ibm.com
export WATSONX_AI_PROJECT_ID=your_project_id_here

Authentication​

Watsonx.ai uses IBM Cloud IAM (Identity and Access Management) for authentication. The integration supports several authentication methods:

The simplest method is using an API key:

spring.ai.watsonx.ai.api-key=your_api_key_here

Service Credentials​

You can also configure using service credentials from IBM Cloud:

spring:
ai:
watsonx:
ai:
credentials:
apikey: your_api_key_here
base-url: https://us-south.ml.cloud.ibm.com

Chat Model Configuration​

Configure chat model specific properties:

PropertyDefaultDescription
spring.ai.watsonx.ai.chat.options.modelibm/granite-4-h-smallThe foundation model to use
spring.ai.watsonx.ai.chat.options.temperature0.7Sampling temperature (0.0 to 2.0)
spring.ai.watsonx.ai.chat.options.max-completion-tokensnot set (watsonx.ai's default)Maximum number of tokens to generate
spring.ai.watsonx.ai.chat.options.top-p1.0Nucleus sampling parameter
spring.ai.watsonx.ai.chat.options.stop-sequences[]List of sequences that stop generation
spring.ai.watsonx.ai.chat.options.presence-penalty0.0Presence penalty (-2.0 to 2.0)
spring.ai.watsonx.ai.chat.options.frequency-penaltynot set (watsonx.ai's default)Frequency penalty (-2.0 to 2.0)
spring.ai.watsonx.ai.chat.options.n1Number of chat completion choices to generate
spring.ai.watsonx.ai.chat.options.logprobsnot set (watsonx.ai's default)Whether to return log probabilities

Example configuration:

spring:
ai:
watsonx:
ai:
chat:
options:
model: meta-llama/llama-3-3-70b-instruct
temperature: 0.3
max-completion-tokens: 2048
top-p: 0.9
stop-sequences:
- "Human:"
- "AI:"

Embedding Model Configuration​

Configure embedding model specific properties:

PropertyDefaultDescription
spring.ai.watsonx.ai.embedding.options.modelibm/granite-embedding-278m-multilingualThe embedding model to use
spring.ai.watsonx.ai.embedding.options.truncate-input-tokensnullTruncate input if it exceeds model limits

Example configuration:

spring:
ai:
watsonx:
ai:
embedding:
options:
model: ibm/granite-embedding-278m-multilingual
truncate-input-tokens: true

Advanced Configuration​

Retry Configuration​

Configure retry behavior for failed requests:

spring:
ai:
retry:
max-attempts: 3
backoff:
initial-interval: 1000ms
multiplier: 2
max-interval: 10000ms

The retry properties are Spring AI's shared spring.ai.retry.* properties, not watsonx.ai specific ones.

HTTP Client Configuration​

The watsonx.ai APIs are called with Spring's RestClient (and WebClient for chat streaming), built from the RestClient.Builder and WebClient.Builder beans. Connection pools, timeouts and codec limits are configured on those builders, for example with a RestClientCustomizer or WebClientCustomizer bean, and not with watsonx.ai properties.

@Configuration
public class WatsonxAiHttpClientConfiguration {

@Bean
public WebClientCustomizer watsonxAiWebClientCustomizer() {
return builder -> builder
.codecs(configurer -> configurer.defaultCodecs().maxInMemorySize(1024 * 1024));
}
}

Multiple Model Configurations​

You can configure multiple models for different use cases:

@Configuration
public class MultiModelConfiguration {

@Bean
@Primary
public WatsonxAiChatModel defaultChatModel(WatsonxAiChatApi chatApi) {
return WatsonxAiChatModel.builder()
.watsonxAiChatApi(chatApi)
.options(WatsonxAiChatOptions.builder()
.model("ibm/granite-4-h-small")
.temperature(0.7)
.build())
.build();
}

@Bean("creativeChatModel")
public WatsonxAiChatModel creativeChatModel(WatsonxAiChatApi chatApi) {
return WatsonxAiChatModel.builder()
.watsonxAiChatApi(chatApi)
.options(WatsonxAiChatOptions.builder()
.model("meta-llama/llama-3-3-70b-instruct")
.temperature(1.2)
.maxCompletionTokens(2048)
.build())
.build();
}

@Bean("analyticalChatModel")
public WatsonxAiChatModel analyticalChatModel(WatsonxAiChatApi chatApi) {
return WatsonxAiChatModel.builder()
.watsonxAiChatApi(chatApi)
.options(WatsonxAiChatOptions.builder()
.model("ibm/granite-20b-code-instruct")
.temperature(0.1)
.build())
.build();
}
}

Usage:

@Service
public class MultiModelService {

private final WatsonxAiChatModel defaultChatModel;
private final WatsonxAiChatModel creativeChatModel;
private final WatsonxAiChatModel analyticalChatModel;

public MultiModelService(
WatsonxAiChatModel defaultChatModel,
@Qualifier("creativeChatModel") WatsonxAiChatModel creativeChatModel,
@Qualifier("analyticalChatModel") WatsonxAiChatModel analyticalChatModel) {
this.defaultChatModel = defaultChatModel;
this.creativeChatModel = creativeChatModel;
this.analyticalChatModel = analyticalChatModel;
}

public String generateCreativeContent(String prompt) {
return creativeChatModel.call(prompt);
}

public String analyzeCode(String code) {
return analyticalChatModel.call("Analyze this code: " + code);
}
}

Profile-Specific Configuration​

Configure different settings for different environments:

application-dev.yml

spring:
ai:
watsonx:
ai:
chat:
options:
temperature: 1.0 # More creative for development
max-completion-tokens: 512 # Shorter responses for testing

application-prod.yml

spring:
ai:
watsonx:
ai:
chat:
options:
temperature: 0.3 # More deterministic for production
max-completion-tokens: 2048 # Longer responses for production use
retry:
max-attempts: 5 # More retries in production

Configuration Validation​

Spring Boot will validate your configuration on startup. Common configuration errors include:

  • Setting both spring.ai.watsonx.ai.project-id and spring.ai.watsonx.ai.space-id, or neither of them. watsonx.ai needs exactly one on every request, so the application fails to start and names the properties to fix. Blank values count as not set. See Upgrading: project ID and space ID are checked at startup.
  • Invalid model names
  • Out-of-range parameter values
  • Network connectivity issues

Enable debug logging to troubleshoot configuration issues:

logging:
level:
org.springaicommunity.watsonx: DEBUG
org.springframework.ai: DEBUG

Upgrading: project ID and space ID are checked at startup​

In 2.0.x, an application with both project-id and space-id, or neither, started normally, and only its first watsonx.ai request failed. Now it fails at startup. This also affects applications that start but never call watsonx.ai, such as a @SpringBootTest or a CI profile that only sets api-key, and applications that get both IDs from two profiles.

With neither ID, the startup report says:

Invalid value '...' for configuration property 'spring.ai.watsonx.ai.project-id' (...). Validation failed for the following reason:

watsonx.ai needs a project or a space. Set spring.ai.watsonx.ai.project-id or spring.ai.watsonx.ai.space-id.

With both:

The following configuration properties are mutually exclusive:

spring.ai.watsonx.ai.project-id
spring.ai.watsonx.ai.space-id

To fix it, do one of these:

  • Set exactly one of spring.ai.watsonx.ai.project-id and spring.ai.watsonx.ai.space-id.

  • Turn off the watsonx.ai models the application doesn't use, for example in a test or CI profile. With all of them off, no ID is needed:

    spring.ai.model.chat=none
    spring.ai.model.embedding=none
    spring.ai.model.moderation=none
    spring.ai.watsonx.ai.rerank.enabled=false
    spring.ai.watsonx.ai.text-extraction.enabled=false
  • Define your own watsonx.ai API beans (WatsonxAiChatApi and the others). The check runs only for the API beans the auto-configuration creates.

Security Considerations​

  1. Never hardcode API keys in your source code
  2. Use environment variables or secure configuration management
  3. Rotate API keys regularly
  4. Limit project permissions to only what's needed
  5. Monitor API usage for unusual patterns

Example secure configuration:

spring:
ai:
watsonx:
ai:
api-key: ${WATSONX_AI_API_KEY:#{null}}
base-url: ${WATSONX_AI_URL:#{null}}
project-id: ${WATSONX_AI_PROJECT_ID:#{null}}

# Fail startup if required properties are missing
management:
endpoint:
health:
probes:
enabled: true
health:
readiness-state:
enabled: true
liveness-state:
enabled: true