{"record":{"id":"f13ea81979ac6cbe","repo":"spring-projects/spring-ai","slug":"unexpected-value-type-s-in-the-list","errorCode":null,"errorMessage":"Unexpected value type %s in the list!","messagePattern":"Unexpected value type (.+?) in the list!","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":1532,"sourceCode":"\t\t}\n\n\t\tpublic static class ContentSerializer extends ValueSerializer<Object> {\n\n\t\t\t@Override\n\t\t\tpublic void serialize(Object value, JsonGenerator jsonGenerator,\n\t\t\t\t\tSerializationContext serializationContext) {\n\t\t\t\tif (value instanceof String text) {\n\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();","sourceCodeStart":1514,"sourceCodeEnd":1550,"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#L1514-L1550","documentation":"During serialization of message content lists (MistralAiApi ContentChunkSerializer), each element must be a ContentChunk instance. If any element of the list is another type, IllegalArgumentException('Unexpected value type %s in the list!') is thrown to prevent writing invalid JSON to the Mistral API.","triggerScenarios":"Building a ChatMessage whose content is a List containing objects other than ContentChunk (e.g. raw Strings, Media, or custom DTOs) and sending it through MistralAiApi.","commonSituations":"Users upgrading Spring AI versions where message content changed from String to List<Object> and they put arbitrary objects in the list; custom ChatMemory implementations storing non-ContentChunk items.","solutions":["Ensure every element of the content list is a MistralAiApi.ContentChunk (wrap strings in a ContentChunk).","Convert legacy String content with the library's content-chunk constructors before building the message.","Audit custom message-history/memory code to guarantee the list only contains ContentChunk instances."],"exampleFix":"// before\nList<Object> content = List.of(\"hello\"); // plain String element\n// after\nList<Object> content = List.of(new MistralAiApi.ContentChunk.TextChunk(\"hello\"));","handlingStrategy":"type-guard","validationCode":"for (Object o : contentList) {\n    if (!(o instanceof MistralAiApi.ContentChunk)) {\n        throw new IllegalArgumentException(\"Non-ContentChunk element: \" + o.getClass());\n    }\n}","typeGuard":"boolean isContentChunkList(List<Object> l) { return l == null || l.stream().allMatch(o -> o instanceof MistralAiApi.ContentChunk); }","tryCatchPattern":"try {\n    chatModel.call(prompt);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().contains(\"Unexpected value type\")) { /* sanitize content list and retry once */ }\n}","preventionTips":["Type content lists as List<ContentChunk> instead of List<Object> in your code.","Add unit tests asserting message content shape before sending to the API.","Centralize message construction in one utility that enforces ContentChunk elements."],"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"}