{"record":{"id":"358525e70635976d","repo":"spring-projects/spring-ai","slug":"unexpected-value-type-s","errorCode":null,"errorMessage":"Unexpected value type %s!","messagePattern":"Unexpected value type (.+?)!","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"models/spring-ai-mistral-ai/src/main/java/org/springframework/ai/mistralai/api/MistralAiApi.java","lineNumber":1540,"sourceCode":"\t\t\t\t\tjsonGenerator.writeString(text);\n\t\t\t\t}\n\t\t\t\telse if (value instanceof List<?> list) {\n\t\t\t\t\tjsonGenerator.writeStartArray();\n\n\t\t\t\t\tfor (var object : list) {\n\t\t\t\t\t\tif (object instanceof ContentChunk contentChunk) {\n\t\t\t\t\t\t\tjsonGenerator.writePOJO(contentChunk);\n\t\t\t\t\t\t}\n\t\t\t\t\t\telse {\n\t\t\t\t\t\t\tthrow new IllegalArgumentException(\n\t\t\t\t\t\t\t\t\t\"Unexpected value type %s in the list!\".formatted(object.getClass()));\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tjsonGenerator.writeEndArray();\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tthrow new IllegalArgumentException(\"Unexpected value type %s!\".formatted(value.getClass()));\n\t\t\t\t}\n\t\t\t}\n\n\t\t}\n\n\t\tpublic static class ContentDeserializer extends ValueDeserializer<Object> {\n\n\t\t\t@Override\n\t\t\tpublic Object deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) {\n\t\t\t\tvar jsonToken = jsonParser.currentToken();\n\n\t\t\t\tif (jsonToken == JsonToken.VALUE_STRING) {\n\t\t\t\t\treturn jsonParser.getValueAsString();\n\t\t\t\t}\n\n\t\t\t\tif (jsonToken == JsonToken.START_ARRAY) {\n\t\t\t\t\tList<ContentChunk> contentChunks = new ArrayList<>();\n","sourceCodeStart":1522,"sourceCodeEnd":1558,"githubUrl":"https://github.com/spring-projects/spring-ai/blob/98a7beda4f29d80a71c5837eb4053b03a93a46f7/models/spring-ai-mistral-ai/src/main/java/org/springframework/ai/mistralai/api/MistralAiApi.java#L1522-L1558","documentation":"The content serializer in MistralAiApi only knows how to serialize content values that are String or List (of ContentChunk). If the message content field holds any other type, IllegalArgumentException('Unexpected value type %s!') is thrown.","triggerScenarios":"Setting a ChatMessage's content to an unsupported object type (e.g. a Map, byte[], or custom class) instead of String or List<ContentChunk> before calling the Mistral API.","commonSituations":"Custom Prompt converters or memory adapters passing arbitrary content objects; refactoring code that assumed content was always a String.","solutions":["Use String or List<ContentChunk> as the message content type.","Convert unsupported content objects (e.g. byte[] or Media) to String or ContentChunk before building the message.","Locate the code that assigns the offending type via the class name in the error message."],"exampleFix":"// before\nnew ChatUserMessage(Map.of(\"key\", \"value\"), null); // unsupported content type\n// after\nnew ChatUserMessage(\"hello\", null); // String content","handlingStrategy":"type-guard","validationCode":"Object content = message.getContent();\nif (!(content instanceof String) && !(content instanceof List<?>)) {\n    throw new IllegalArgumentException(\"Content must be String or List<ContentChunk>: \" + content.getClass());\n}","typeGuard":"boolean isValidContent(Object c) { return c instanceof String || (c instanceof List<?> l && l.stream().allMatch(x -> x instanceof MistralAiApi.ContentChunk)); }","tryCatchPattern":"try {\n    chatModel.call(prompt);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().startsWith(\"Unexpected value type\")) { /* convert content to String and rebuild prompt */ }\n}","preventionTips":["Only assign String or List<ContentChunk> values to message content.","Convert Media/byte[]/Map content to a supported representation before message construction.","Cover message building with unit tests that serialize a sample message."],"tags":["serialization","type-mismatch","mistral"],"backgroundTag":"incompatible-source-type","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"}