{"record":{"id":"0d842166333fb999","repo":"spring-projects/spring-ai","slug":"model-cannot-be-null-or-empty-0d8421","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/OllamaEmbeddingModel.java","lineNumber":153,"sourceCode":"\n\t\t\t\tEmbeddingResponse embeddingResponse = new EmbeddingResponse(embeddings, embeddingResponseMetadata);\n\n\t\t\t\tobservationContext.setResponse(embeddingResponse);\n\n\t\t\t\treturn embeddingResponse;\n\t\t\t});\n\t}\n\n\tprivate DefaultUsage getDefaultUsage(OllamaApi.EmbeddingsResponse response) {\n\t\treturn new DefaultUsage(Optional.ofNullable(response.promptEvalCount()).orElse(0), 0);\n\t}\n\n\tEmbeddingRequest buildEmbeddingRequest(EmbeddingRequest embeddingRequest) {\n\t\tOllamaEmbeddingOptions requestOptions = mergeOptions(embeddingRequest.getOptions());\n\n\t\t// Validate request options\n\t\tif (!StringUtils.hasText(requestOptions.getModel())) {\n\t\t\tthrow new IllegalArgumentException(\"model cannot be null or empty\");\n\t\t}\n\n\t\treturn new EmbeddingRequest(embeddingRequest.getInstructions(), requestOptions);\n\t}\n\n\tprivate OllamaEmbeddingOptions mergeOptions(@Nullable EmbeddingOptions requestOptions) {\n\t\tOllamaEmbeddingOptions options = this.options;\n\n\t\tif (requestOptions == null) {\n\t\t\treturn options;\n\t\t}\n\n\t\tOllamaEmbeddingOptions.Builder builder = OllamaEmbeddingOptions.builder()\n\t\t\t.model(ModelOptionsUtils.mergeOption(requestOptions.getModel(), options.getModel()))\n\t\t\t.dimensions(ModelOptionsUtils.mergeOption(requestOptions.getDimensions(), options.getDimensions()));\n\n\t\tif (requestOptions instanceof OllamaEmbeddingOptions ro) {\n\t\t\tbuilder.keepAlive(ModelOptionsUtils.mergeOption(ro.getKeepAlive(), options.getKeepAlive()))","sourceCodeStart":135,"sourceCodeEnd":171,"githubUrl":"https://github.com/spring-projects/spring-ai/blob/98a7beda4f29d80a71c5837eb4053b03a93a46f7/models/spring-ai-ollama/src/main/java/org/springframework/ai/ollama/OllamaEmbeddingModel.java#L135-L171","documentation":"OllamaEmbeddingModel.buildEmbeddingRequest() merges the request options with defaults and validates that a model name is present, since Ollama embeddings require an explicit model. If the merged options' model is null/empty, IllegalArgumentException('model cannot be null or empty') is thrown.","triggerScenarios":"Calling embed()/embedForResponse() with an EmbeddingRequest whose options lack a model while the OllamaEmbeddingModel was built without default embedding options specifying a model.","commonSituations":"Constructing OllamaEmbeddingModel without .defaultOptions(OllamaOptions.builder().model(\"nomic-embed-text\").build()); building EmbeddingRequest with empty EmbeddingOptions at the call site.","solutions":["Set a default embedding model on the builder: OllamaOptions.builder().model(\"nomic-embed-text\").build().","Or pass EmbeddingOptions with a model in the EmbeddingRequest.","Verify option merge logic isn't clearing the model property."],"exampleFix":"// before\nnew OllamaEmbeddingModel(api); // no model configured\n// after\nnew OllamaEmbeddingModel(api,\n    OllamaOptions.builder().model(\"nomic-embed-text\").build());","handlingStrategy":"validation","validationCode":"var model = embeddingRequest.getOptions() != null ? embeddingRequest.getOptions().getModel() : null;\nif (model == null || model.isBlank()) {\n    throw new IllegalArgumentException(\"Embedding model must be set in options or defaults\");\n}","typeGuard":"boolean hasEmbeddingModel(EmbeddingOptions o) { return o != null && o.getModel() != null && !o.getModel().isBlank(); }","tryCatchPattern":"try {\n    return embeddingModel.embed(texts);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().contains(\"model cannot be null\")) { /* supply model options and retry */ }\n    throw e;\n}","preventionTips":["Configure OllamaEmbeddingModel defaultOptions with an embedding model such as nomic-embed-text.","Remember chat models and embedding models need separate model names in Ollama.","Add a boot-time health check that embeds a short sample string."],"tags":["validation","configuration","ollama","embeddings"],"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"}