{"record":{"id":"73f23569bd021907","repo":"spring-projects/spring-ai","slug":"the-topk-option-is-not-supported-by-openai-chat-mo","errorCode":null,"errorMessage":"The topK option is not supported by OpenAI chat models. Ignoring.","messagePattern":"The topK option is not supported by OpenAI chat models\\. Ignoring\\.","errorType":"console","errorClass":null,"httpStatus":null,"severity":"info","filePath":"models/spring-ai-openai/src/main/java/org/springframework/ai/openai/OpenAiChatModel.java","lineNumber":503,"sourceCode":"\t\ttry {\n\t\t\treturn result.created();\n\t\t}\n\t\tcatch (OpenAIInvalidDataException ex) {\n\t\t\treturn 0L;\n\t\t}\n\t}\n\n\tprivate DefaultUsage getDefaultUsage(CompletionUsage usage) {\n\t\tLong cacheRead = usage.promptTokensDetails().flatMap(details -> details.cachedTokens()).orElse(null);\n\t\treturn new DefaultUsage(Math.toIntExact(usage.promptTokens()), Math.toIntExact(usage.completionTokens()),\n\t\t\t\tMath.toIntExact(usage.totalTokens()), usage, cacheRead, null);\n\t}\n\n\tprivate void verifyPromptChatOptions(Prompt prompt) {\n\t\tvar chatOptions = prompt.getOptions();\n\n\t\tif (chatOptions != null && chatOptions.getTopK() != null) {\n\t\t\tlogger.warn(\"The topK option is not supported by OpenAI chat models. Ignoring.\");\n\t\t}\n\t}\n\n\t/**\n\t * Creates a chat completion request from the given prompt.\n\t * @param prompt the prompt containing messages and options\n\t * @param stream whether this is a streaming request\n\t * @return the chat completion create parameters\n\t */\n\tChatCompletionCreateParams createRequest(Prompt prompt, boolean stream) {\n\n\t\tList<ChatCompletionMessageParam> chatCompletionMessageParams = prompt.getInstructions()\n\t\t\t.stream()\n\t\t\t.map(message -> {\n\t\t\t\tif (message.getMessageType() == MessageType.USER || message.getMessageType() == MessageType.SYSTEM) {\n\t\t\t\t\t// Handle simple text content for user and system messages\n\t\t\t\t\tChatCompletionUserMessageParam.Builder builder = ChatCompletionUserMessageParam.builder();\n","sourceCodeStart":485,"sourceCodeEnd":521,"githubUrl":"https://github.com/spring-projects/spring-ai/blob/98a7beda4f29d80a71c5837eb4053b03a93a46f7/models/spring-ai-openai/src/main/java/org/springframework/ai/openai/OpenAiChatModel.java#L485-L521","documentation":"verifyPromptChatOptions inspects the per-call Prompt options and warns that topK is not a parameter supported by the OpenAI chat completion API. The option is silently ignored — the request is still sent without topK. Spring AI throws/logs this to surface a portability issue: code written for models that use topK (e.g. Ollama) silently loses that setting when pointed at OpenAI.","triggerScenarios":"Passing a Prompt whose ChatOptions has a non-null topK to OpenAiChatModel.call(prompt) or stream(prompt) — typically via OpenAiChatOptions.builder().topK(n) or runtime options set for another provider.","commonSituations":"Swapping model implementations (e.g. from Ollama/Vertex to OpenAI) while reusing the same options object; copying sample code that sets topK; configuring topK via shared application options.","solutions":["Remove the topK setting from the options used with OpenAI chat models","If temperature-like control is needed, use OpenAI-supported knobs such as temperature or topP instead","Use provider-specific OpenAiChatOptions when constructing the Prompt"],"exampleFix":"// before\nOpenAiChatOptions options = OpenAiChatOptions.builder().topK(40).temperature(0.7).build();\n// after\nOpenAiChatOptions options = OpenAiChatOptions.builder().temperature(0.7).topP(0.9).build();","handlingStrategy":"validation","validationCode":"if (options != null && options.getTopK() != null) {\n    // strip it before calling OpenAI\n    options = OpenAiChatOptions.fromOptions(options).mutate().topK(null).build();\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use provider-specific options classes per model","Don't share a generic ChatOptions object across providers","Prefer temperature/topP on OpenAI"],"tags":["openai","options","unsupported-option"],"backgroundTag":"unsupported-operation","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"}