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 use maxCompletionToken (${maxCompletionTokens}).

What it means

In OpenAiChatOptions.Builder.maxCompletionTokens(), setting maxCompletionTokens while maxTokens is already set triggers a warning that both parameters cannot be sent together to the OpenAI API. Since maxTokens is deprecated, the builder clears it via this.maxTokens(null) and the new maxCompletionTokens value wins. This ordering is the intended migration path.

Solutions

  1. Prefer this setter order (maxCompletionTokens after maxTokens) — the deprecated value is dropped automatically
  2. Clean the configuration so only maxCompletionTokens is set, silencing the warning
  3. Update any defaults/properties that still set maxTokens

Example fix

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

Strategy: validation

Validate before calling

if (opts.getMaxTokens() != null && opts.getMaxCompletionTokens() != null) {
    // maxCompletionTokens wins; clear maxTokens
}

Prevention

When it happens

Trigger: Calling OpenAiChatOptions.Builder.maxCompletionTokens(n) after maxTokens(m) was already set on the same builder (e.g. options inherited from legacy configuration).

Common situations: Modernizing old configuration from the deprecated maxTokens to maxCompletionTokens while both appear in the builder chain; programmatically overriding a preconfigured options object.

Understand the failure class

Background: "is deprecated and will be removed" — deprecation warnings for old API names, keywords, and options, and how to migrate before the removal release — this error's family across 29 libraries.

Related errors


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

Appendix: source

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

		public B logitBias(@Nullable Map<String, Integer> logitBias) {
			this.logitBias = logitBias;
			return self();
		}

		public B logprobs(@Nullable Boolean logprobs) {
			this.logprobs = logprobs;
			return self();
		}

		public B topLogprobs(@Nullable Integer topLogprobs) {
			this.topLogprobs = topLogprobs;
			return self();
		}

		public B maxCompletionTokens(@Nullable Integer maxCompletionTokens) {
			if (maxCompletionTokens != null && this.maxTokens != 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 use maxCompletionToken ("
									+ maxCompletionTokens + ").");
				}
				this.maxTokens(null);
			}
			this.maxCompletionTokens = maxCompletionTokens;
			return self();
		}

		public B n(@Nullable Integer n) {
			this.n = n;
			return self();
		}

		public B outputModalities(@Nullable List<String> outputModalities) {
			this.outputModalities = outputModalities;
			return self();

View on GitHub (pinned to 98a7beda4f)