{"record":{"id":"fa24004f597d50e3","repo":"spring-projects/spring-ai","slug":"unsupported-message-type-fa2400","errorCode":null,"errorMessage":"Unsupported message type: ","messagePattern":"Unsupported message type: ","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"models/spring-ai-ollama/src/main/java/org/springframework/ai/ollama/OllamaChatModel.java","lineNumber":412,"sourceCode":"\t\t\t\t\ttoolCalls = assistantMessage.getToolCalls().stream().map(toolCall -> {\n\t\t\t\t\t\tvar function = new ToolCallFunction(toolCall.name(),\n\t\t\t\t\t\t\t\tjsonHelper.fromJsonToMap(toolCall.arguments()));\n\t\t\t\t\t\treturn new ToolCall(toolCall.id(), function);\n\t\t\t\t\t}).toList();\n\t\t\t\t}\n\t\t\t\treturn List.of(OllamaApi.Message.builder(Role.ASSISTANT)\n\t\t\t\t\t.content(assistantMessage.getText())\n\t\t\t\t\t.toolCalls(toolCalls)\n\t\t\t\t\t.build());\n\t\t\t}\n\t\t\telse if (message.getMessageType() == MessageType.TOOL) {\n\t\t\t\tToolResponseMessage toolMessage = (ToolResponseMessage) message;\n\t\t\t\treturn toolMessage.getResponses()\n\t\t\t\t\t.stream()\n\t\t\t\t\t.map(tr -> OllamaApi.Message.builder(Role.TOOL).content(tr.responseData()).build())\n\t\t\t\t\t.toList();\n\t\t\t}\n\t\t\tthrow new IllegalArgumentException(\"Unsupported message type: \" + message.getMessageType());\n\t\t}).flatMap(List::stream).toList();\n\n\t\tOllamaChatOptions requestOptions = (OllamaChatOptions) prompt.getOptions();\n\t\tAssert.state(requestOptions != null, \"ChatOptions must not be null\");\n\n\t\tString model = requestOptions.getModel();\n\t\tAssert.state(model != null, \"model must not be null\");\n\t\tOllamaApi.ChatRequest.Builder requestBuilder = OllamaApi.ChatRequest.builder(model)\n\t\t\t.stream(stream)\n\t\t\t.messages(ollamaMessages)\n\t\t\t.options(requestOptions)\n\t\t\t.think(requestOptions.getThinkOption());\n\n\t\tif (requestOptions.getFormat() != null) {\n\t\t\trequestBuilder.format(requestOptions.getFormat());\n\t\t}\n\n\t\tif (requestOptions.getKeepAlive() != null) {","sourceCodeStart":394,"sourceCodeEnd":430,"githubUrl":"https://github.com/spring-projects/spring-ai/blob/98a7beda4f29d80a71c5837eb4053b03a93a46f7/models/spring-ai-ollama/src/main/java/org/springframework/ai/ollama/OllamaChatModel.java#L394-L430","documentation":"OllamaChatModel.ollamaChatRequest() maps Spring AI message types (UserMessage, AssistantMessage, SystemMessage, ToolResponseMessage) to Ollama API messages. Any other MessageType reaches the final throw of IllegalArgumentException('Unsupported message type: ...').","triggerScenarios":"Adding a message of an unsupported type (e.g. a custom Message implementation or ToolResponseMessage variant not handled) into the Prompt's message list and calling request().","commonSituations":"Custom chat memory returning bespoke Message implementations; combining messages produced by other model adapters; Spring AI upgrades introducing new message types before the Ollama adapter handles them.","solutions":["Use only supported message types: UserMessage, AssistantMessage, SystemMessage, ToolResponseMessage.","Filter or convert custom Message implementations before building the Prompt.","Upgrade spring-ai-ollama if a newly introduced framework message type is not yet mapped."],"exampleFix":"// before\nmessages.add(myCustomMessage); // unsupported type\n// after\nmessages.add(new UserMessage(myCustomMessage.getText()));","handlingStrategy":"validation","validationCode":"prompt.getInstructions().forEach(m -> {\n    var t = m.getMessageType();\n    if (!(t == MessageType.USER || t == MessageType.ASSISTANT || t == MessageType.SYSTEM || t == MessageType.TOOL)) {\n        throw new IllegalArgumentException(\"Unsupported message type: \" + t);\n    }\n});","typeGuard":"boolean isSupported(Message m) { return m instanceof UserMessage || m instanceof AssistantMessage || m instanceof SystemMessage || m instanceof ToolResponseMessage; }","tryCatchPattern":"try {\n    return ollamaChatModel.call(prompt);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().startsWith(\"Unsupported message type\")) { /* drop/convert offending message and retry */ }\n    throw e;\n}","preventionTips":["Store only framework-standard Message types in chat memory.","Sanitize history loaded from external stores by reconstructing standard Message types.","Add a filter step that maps/converts messages before building the Prompt."],"tags":["validation","message-types","ollama"],"backgroundTag":"unsupported-operation","analyzedSha":"98a7beda4f29d80a71c5837eb4053b03a93a46f7","analyzedAt":"2026-09-11T14:15:49.441Z","contentChangedAt":"2026-09-11T14:15:49.441Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}