spring-projects/spring-ai · warning

Both maxTokens and maxCompletionTokens are set. OpenAI API…

Error message

Both maxTokens and maxCompletionTokens are set. OpenAI API does not support setting both parameters simultaneously. As maxToken is deprecated, we will ignore it and use maxCompletionToken (${maxCompletionTokens}).

What it means

In OpenAiChatOptions.Builder.maxTokens(), setting maxTokens when maxCompletionTokens was already set triggers a warning: the OpenAI API cannot accept both parameters at once. Because maxTokens is the deprecated parameter, the builder keeps maxCompletionTokens and the maxTokens value is ignored. This prevents the request from failing with a 400 from OpenAI.

Solutions

  1. Remove the maxTokens(...) call and keep only maxCompletionTokens(...)
  2. If you intended to raise the cap, call maxCompletionTokens(n) instead of maxTokens(n)
  3. Ensure defaults and request-level options do not both set the pair

Example fix

// before
OpenAiChatOptions.builder().maxCompletionTokens(2048).maxTokens(1024).build();
// after
OpenAiChatOptions.builder().maxCompletionTokens(2048).build();
Defensive patterns

Strategy: validation

Validate before calling

if (opts.getMaxTokens() != null && opts.getMaxCompletionTokens() != null) {
    // resolve before sending: keep maxCompletionTokens
}

Prevention

When it happens

Trigger: Calling OpenAiChatOptions.Builder.maxTokens(n) after maxCompletionTokens(m) has already been set on the same builder.

Common situations: Migrating legacy code that used maxTokens while also adopting the newer maxCompletionTokens option; merging default options (which set one) with per-request options (which set the other).

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

Appendix: source

Thrown at models/spring-ai-openai/src/main/java/org/springframework/ai/openai/OpenAiChatOptions.java:833

		protected @Nullable Boolean strict;

		protected @Nullable Map<String, String> metadata;

		protected @Nullable String reasoningEffort;

		protected @Nullable String verbosity;

		protected @Nullable String serviceTier;

		protected @Nullable String promptCacheKey;

		protected @Nullable Map<String, Object> extraBody;

		@Override
		public B maxTokens(@Nullable Integer maxTokens) {
			if (this.maxCompletionTokens != null) {
				if (logger.isWarnEnabled()) {
					logger.warn(
							"Both maxTokens and maxCompletionTokens are set. OpenAI API does not support setting both parameters simultaneously. "
									+ "As maxToken is deprecated, we will ignore it and use maxCompletionToken ("
									+ this.maxCompletionTokens + ").");
				}
			}
			else {
				this.maxTokens = maxTokens;
			}
			return self();
		}

		public B baseUrl(@Nullable String baseUrl) {
			this.baseUrl = baseUrl;
			return self();
		}

		public B apiKey(@Nullable String apiKey) {
			this.apiKey = apiKey;

View on GitHub (pinned to 98a7beda4f)