{"record":{"id":"fe3fd38860b77e70","repo":"spring-projects/spring-ai","slug":"both-maxtokens-and-maxcompletiontokens-are-set-op","errorCode":null,"errorMessage":"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}).","messagePattern":"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 \\((.+?)\\)\\.","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"models/spring-ai-openai/src/main/java/org/springframework/ai/openai/OpenAiChatOptions.java","lineNumber":833,"sourceCode":"\t\tprotected @Nullable Boolean strict;\n\n\t\tprotected @Nullable Map<String, String> metadata;\n\n\t\tprotected @Nullable String reasoningEffort;\n\n\t\tprotected @Nullable String verbosity;\n\n\t\tprotected @Nullable String serviceTier;\n\n\t\tprotected @Nullable String promptCacheKey;\n\n\t\tprotected @Nullable Map<String, Object> extraBody;\n\n\t\t@Override\n\t\tpublic B maxTokens(@Nullable Integer maxTokens) {\n\t\t\tif (this.maxCompletionTokens != null) {\n\t\t\t\tif (logger.isWarnEnabled()) {\n\t\t\t\t\tlogger.warn(\n\t\t\t\t\t\t\t\"Both maxTokens and maxCompletionTokens are set. OpenAI API does not support setting both parameters simultaneously. \"\n\t\t\t\t\t\t\t\t\t+ \"As maxToken is deprecated, we will ignore it and use maxCompletionToken (\"\n\t\t\t\t\t\t\t\t\t+ this.maxCompletionTokens + \").\");\n\t\t\t\t}\n\t\t\t}\n\t\t\telse {\n\t\t\t\tthis.maxTokens = maxTokens;\n\t\t\t}\n\t\t\treturn self();\n\t\t}\n\n\t\tpublic B baseUrl(@Nullable String baseUrl) {\n\t\t\tthis.baseUrl = baseUrl;\n\t\t\treturn self();\n\t\t}\n\n\t\tpublic B apiKey(@Nullable String apiKey) {\n\t\t\tthis.apiKey = apiKey;","sourceCodeStart":815,"sourceCodeEnd":851,"githubUrl":"https://github.com/spring-projects/spring-ai/blob/98a7beda4f29d80a71c5837eb4053b03a93a46f7/models/spring-ai-openai/src/main/java/org/springframework/ai/openai/OpenAiChatOptions.java#L815-L851","documentation":"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.","triggerScenarios":"Calling OpenAiChatOptions.Builder.maxTokens(n) after maxCompletionTokens(m) has already been set on the same builder.","commonSituations":"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).","solutions":["Remove the maxTokens(...) call and keep only maxCompletionTokens(...)","If you intended to raise the cap, call maxCompletionTokens(n) instead of maxTokens(n)","Ensure defaults and request-level options do not both set the pair"],"exampleFix":"// before\nOpenAiChatOptions.builder().maxCompletionTokens(2048).maxTokens(1024).build();\n// after\nOpenAiChatOptions.builder().maxCompletionTokens(2048).build();","handlingStrategy":"validation","validationCode":"if (opts.getMaxTokens() != null && opts.getMaxCompletionTokens() != null) {\n    // resolve before sending: keep maxCompletionTokens\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Migrate fully to maxCompletionTokens","Never set both in defaults + per-request options","Audit legacy property files for maxTokens"],"tags":["openai","options","deprecated","conflict"],"backgroundTag":"conflicting-config-options","analyzedSha":"98a7beda4f29d80a71c5837eb4053b03a93a46f7","analyzedAt":"2026-09-11T14:15:49.441Z","contentChangedAt":"2026-09-11T14:15:49.441Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}