{"record":{"id":"49d336cba5efb740","repo":"spring-projects/spring-ai","slug":"error-serializing-documentmap-to-json","errorCode":null,"errorMessage":"Error serializing documentMap to JSON.","messagePattern":"Error serializing documentMap to JSON\\.","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"spring-ai-vector-store/src/main/java/org/springframework/ai/vectorstore/SimpleVectorStore.java","lineNumber":247,"sourceCode":"\tpublic void load(Resource resource) {\n\t\tTypeReference<HashMap<String, SimpleVectorStoreContent>> typeRef = new TypeReference<>() {\n\n\t\t};\n\t\ttry {\n\t\t\tthis.store = this.jsonMapper.readValue(resource.getInputStream(), typeRef);\n\t\t}\n\t\tcatch (IOException ex) {\n\t\t\tthrow new RuntimeException(ex);\n\t\t}\n\t}\n\n\tprivate String getVectorDbAsJson() {\n\t\tObjectWriter objectWriter = this.jsonMapper.writerWithDefaultPrettyPrinter();\n\t\ttry {\n\t\t\treturn objectWriter.writeValueAsString(this.store);\n\t\t}\n\t\tcatch (JacksonException ex) {\n\t\t\tthrow new RuntimeException(\"Error serializing documentMap to JSON.\", ex);\n\t\t}\n\t}\n\n\tprivate float[] getUserQueryEmbedding(String query) {\n\t\treturn this.embeddingModel.embed(query);\n\t}\n\n\t@Override\n\tpublic VectorStoreObservationContext.Builder createObservationContextBuilder(String operationName) {\n\n\t\treturn VectorStoreObservationContext.builder(VectorStoreProvider.SIMPLE.value(), operationName)\n\t\t\t.dimensions(this.embeddingModel.dimensions())\n\t\t\t.collectionName(\"in-memory-map\")\n\t\t\t.similarityMetric(VectorStoreSimilarityMetric.COSINE.value());\n\t}\n\n\tpublic static final class EmbeddingMath {\n","sourceCodeStart":229,"sourceCodeEnd":265,"githubUrl":"https://github.com/spring-projects/spring-ai/blob/98a7beda4f29d80a71c5837eb4053b03a93a46f7/spring-ai-vector-store/src/main/java/org/springframework/ai/vectorstore/SimpleVectorStore.java#L229-L265","documentation":"SimpleVectorStore.getVectorDbAsJson() wraps Jackson serialization failures of the in-memory document map into a RuntimeException 'Error serializing documentMap to JSON.' The internal store maps IDs to documents/embeddings and Jackson fails if those objects are not serializable (e.g. a custom Document or embedding type Jackson cannot handle).","triggerScenarios":"Calling persist() (which calls json()) after inserting documents whose stored representation cannot be serialized by the ObjectMapper, or when the Jackson mapper encounters an incompatible type.","commonSituations":"Custom Document subclasses with non-serializable fields; mixing documents loaded from another store version; classpath Jackson version mismatch.","solutions":["Inspect the wrapped JacksonException cause to find the unserializable type.","Ensure stored Documents use serializable field types or add proper Jackson annotations/mixins.","Verify Jackson versions on the classpath are consistent with the Spring AI dependency requirements."],"exampleFix":"// before\nvectorStore.persist(myFile); // fails if a document has an unserializable field\n\n// after\n// remove/replace non-serializable fields, e.g.\npublic class MyDocument extends Document {\n    private transient OutputStream debugStream; // excluded from serialization\n}","handlingStrategy":"try-catch","validationCode":"// ensure all stored documents contain only Jackson-serializable fields\nobjects.forEach(d -> assertSerializable(d));","typeGuard":null,"tryCatchPattern":"try {\n    vectorStore.persist(file);\n} catch (RuntimeException e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"serializing documentMap\")) {\n        logger.error(\"Unserializable document in store\", e.getCause());\n    }\n    throw e;\n}","preventionTips":["Keep Document payloads to JSON-friendly types.","Mark transient/debug fields with transient or @JsonIgnore.","Keep Jackson versions aligned with the Spring AI BOM."],"tags":["jackson","serialization","vector-store"],"backgroundTag":"json-marshal-failed","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"}