{"record":{"id":"848386428f5d4f3d","repo":"spring-projects/spring-ai","slug":"model-cannot-be-null-or-empty-848386","errorCode":null,"errorMessage":"model cannot be null or empty","messagePattern":"model cannot be null or empty","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"models/spring-ai-ollama/src/main/java/org/springframework/ai/ollama/OllamaChatModel.java","lineNumber":364,"sourceCode":"\n\t\t\t\tvar generator = new Generation(assistantMessage, generationMetadata);\n\t\t\t\treturn new ChatResponse(List.of(generator), from(chunk, previousChatResponse));\n\t\t\t});\n\n\t\t\tFlux<ChatResponse> chatResponseFlux = chatResponse.flatMap(response -> Flux.just(response))\n\t\t\t\t.doOnError(observation::error)\n\t\t\t\t.doFinally(s -> observation.stop())\n\t\t\t\t.contextWrite(ctx -> ctx.put(ObservationThreadLocalAccessor.KEY, observation));\n\n\t\t\treturn new MessageAggregator().aggregate(chatResponseFlux, observationContext::setResponse);\n\t\t});\n\t}\n\n\tprivate void verifyPromptChatOptions(Prompt prompt) {\n\t\tvar chatOptions = prompt.getOptions();\n\n\t\tif (chatOptions != null && !StringUtils.hasText(chatOptions.getModel())) {\n\t\t\tthrow new IllegalArgumentException(\"model cannot be null or empty\");\n\t\t}\n\t}\n\n\t/**\n\t * Package access for testing.\n\t */\n\tOllamaApi.ChatRequest ollamaChatRequest(Prompt prompt, boolean stream) {\n\n\t\tList<OllamaApi.Message> ollamaMessages = prompt.getInstructions().stream().map(message -> {\n\t\t\tif (message.getMessageType() == MessageType.SYSTEM) {\n\t\t\t\treturn List.of(OllamaApi.Message.builder(Role.SYSTEM).content(message.getText()).build());\n\t\t\t}\n\t\t\telse if (message.getMessageType() == MessageType.USER) {\n\t\t\t\tvar messageBuilder = OllamaApi.Message.builder(Role.USER).content(message.getText());\n\t\t\t\tif (message instanceof UserMessage userMessage) {\n\t\t\t\t\tif (!CollectionUtils.isEmpty(userMessage.getMedia())) {\n\t\t\t\t\t\tmessageBuilder.images(userMessage.getMedia()\n\t\t\t\t\t\t\t.stream()","sourceCodeStart":346,"sourceCodeEnd":382,"githubUrl":"https://github.com/spring-projects/spring-ai/blob/98a7beda4f29d80a71c5837eb4053b03a93a46f7/models/spring-ai-ollama/src/main/java/org/springframework/ai/ollama/OllamaChatModel.java#L346-L382","documentation":"OllamaChatModel.verifyPromptChatOptions() validates the runtime options attached to a Prompt. Ollama requires an explicit model name, so if ChatOptions is present but its model is null or empty, IllegalArgumentException('model cannot be null or empty') is thrown before any HTTP call.","triggerScenarios":"Calling call()/stream() with a Prompt whose OllamaChatOptions has no model set and no default model was configured on the OllamaChatModel builder.","commonSituations":"Building OllamaChatModel without .defaultOptions(OllamaOptions.builder().model(...).build()) and then passing runtime options without a model; copying options between requests and dropping the model field.","solutions":["Set a default model when constructing the model: OllamaOptions.builder().model(\"llama3\").build().","Or set the model on the per-request options passed into the Prompt.","Check option-merging code for accidental nulling of the model field."],"exampleFix":"// before\nOllamaChatModel.builder().ollamaApi(api).build(); // no default model\n// after\nOllamaChatModel.builder().ollamaApi(api)\n    .defaultOptions(OllamaOptions.builder().model(\"llama3\").build()).build();","handlingStrategy":"validation","validationCode":"var options = prompt.getOptions();\nif (options != null && (options.getModel() == null || options.getModel().isBlank())) {\n    throw new IllegalArgumentException(\"Set model via OllamaOptions or defaultOptions\");\n}","typeGuard":"boolean hasModel(OllamaChatOptions o) { return o != null && o.getModel() != null && !o.getModel().isBlank(); }","tryCatchPattern":"try {\n    return ollamaChatModel.call(prompt);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().contains(\"model cannot be null\")) { /* set default model options and retry */ }\n    throw e;\n}","preventionTips":["Always configure defaultOptions with a model when building OllamaChatModel.","Write a startup smoke test that issues a tiny chat call to catch missing model config early.","When copying/merging options, assert the model field survives."],"tags":["validation","configuration","ollama","model"],"backgroundTag":"missing-required-option","analyzedSha":"98a7beda4f29d80a71c5837eb4053b03a93a46f7","analyzedAt":"2026-09-11T14:15:49.441Z","contentChangedAt":"2026-09-11T14:15:49.441Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}