{"record":{"id":"5c83ade7e377971c","repo":"alibaba/spring-ai-alibaba","slug":"invalid-messagetype-value","errorCode":null,"errorMessage":"Invalid MessageType value: ","messagePattern":"Invalid MessageType value: ","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"spring-ai-alibaba-admin/spring-ai-alibaba-admin-server-runtime/src/main/java/com/alibaba/cloud/ai/studio/runtime/domain/chat/MessageRole.java","lineNumber":67,"sourceCode":"\n\tMessageRole(String value) {\n\t\tthis.value = value;\n\t}\n\n\t/**\n\t * Converts a string value to its corresponding MessageRole enum.\n\t * @param value The string value to convert\n\t * @return The corresponding MessageRole enum\n\t * @throws IllegalArgumentException if the value is invalid\n\t */\n\tpublic static MessageRole of(String value) {\n\t\tfor (MessageRole messageRole : MessageRole.values()) {\n\t\t\tif (messageRole.getValue().equals(value)) {\n\t\t\t\treturn messageRole;\n\t\t\t}\n\t\t}\n\n\t\tthrow new IllegalArgumentException(\"Invalid MessageType value: \" + value);\n\t}\n\n}\n","sourceCodeStart":49,"sourceCodeEnd":71,"githubUrl":"https://github.com/alibaba/spring-ai-alibaba/blob/f82da0b50f35744c13968191be2b1cd2452ef550/spring-ai-alibaba-admin/spring-ai-alibaba-admin-server-runtime/src/main/java/com/alibaba/cloud/ai/studio/runtime/domain/chat/MessageRole.java#L49-L71","documentation":"MessageRole.of() iterates all MessageRole enum constants and throws IllegalArgumentException when no constant's value equals the given string. This means an unrecognized message role string (after the trailing value in the message) was passed, e.g. from deserialized chat data. It is a fail-fast guard for the OPEN/ASSISTANT/... role vocabulary of the chat protocol.","triggerScenarios":"Calling MessageRole.of(value) with a string that is not any enum constant's value, including null or empty string.","commonSituations":"Deserializing chat messages persisted by an older/newer version whose role strings differ; API clients sending unknown roles like \"function\" or \"tool\"; passing raw model output directly as a role.","solutions":["Use only the role strings defined by MessageRole values (check the enum's getValue() strings).","Normalize/trim the input string before calling of().","Wrap of() in try-catch (IllegalArgumentException) if the role comes from external data, and default to a safe role.","If a new role is genuinely needed, add a constant to MessageRole instead of passing an ad-hoc string."],"exampleFix":"// before\nMessageRole role = MessageRole.of(\"bot\"); // throws\n// after\nMessageRole role = MessageRole.of(\"assistant\"); // or validate first\nif (role == null) { role = MessageRole.USER; }","handlingStrategy":"try-catch","validationCode":"boolean knownRole = Arrays.stream(MessageRole.values())\n    .anyMatch(r -> r.getValue().equals(input));","typeGuard":"MessageRole safeRole(String v) {\n  try { return MessageRole.of(v); }\n  catch (IllegalArgumentException e) { return MessageRole.USER; }\n}","tryCatchPattern":"try {\n  role = MessageRole.of(value);\n} catch (IllegalArgumentException e) {\n  log.warn(\"Unknown message role: {}\", value);\n  role = MessageRole.USER;\n}","preventionTips":["Only pass role strings produced by MessageRole.getValue(), never raw model output.","Normalize/trim external role strings before conversion.","When persisting chat history, round-trip through the enum so unknown values surface early.","Wrap of() at deserialization boundaries with a default fallback."],"tags":["enum","illegal-argument","chat"],"backgroundTag":"invalid-enum-value","analyzedSha":"f82da0b50f35744c13968191be2b1cd2452ef550","analyzedAt":"2026-09-09T15:32:42.421Z","contentChangedAt":"2026-09-09T15:32:42.421Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}