spring-projects/spring-ai · error · RuntimeException
Error serializing documentMap to JSON.
Error message
Error serializing documentMap to JSON.
What it means
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).
Source
Thrown at spring-ai-vector-store/src/main/java/org/springframework/ai/vectorstore/SimpleVectorStore.java:247
public void load(Resource resource) {
TypeReference<HashMap<String, SimpleVectorStoreContent>> typeRef = new TypeReference<>() {
};
try {
this.store = this.jsonMapper.readValue(resource.getInputStream(), typeRef);
}
catch (IOException ex) {
throw new RuntimeException(ex);
}
}
private String getVectorDbAsJson() {
ObjectWriter objectWriter = this.jsonMapper.writerWithDefaultPrettyPrinter();
try {
return objectWriter.writeValueAsString(this.store);
}
catch (JacksonException ex) {
throw new RuntimeException("Error serializing documentMap to JSON.", ex);
}
}
private float[] getUserQueryEmbedding(String query) {
return this.embeddingModel.embed(query);
}
@Override
public VectorStoreObservationContext.Builder createObservationContextBuilder(String operationName) {
return VectorStoreObservationContext.builder(VectorStoreProvider.SIMPLE.value(), operationName)
.dimensions(this.embeddingModel.dimensions())
.collectionName("in-memory-map")
.similarityMetric(VectorStoreSimilarityMetric.COSINE.value());
}
public static final class EmbeddingMath {
View on GitHub (pinned to 98a7beda4f)
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.
Example fix
// before
vectorStore.persist(myFile); // fails if a document has an unserializable field
// after
// remove/replace non-serializable fields, e.g.
public class MyDocument extends Document {
private transient OutputStream debugStream; // excluded from serialization
} Defensive patterns
Strategy: try-catch
Validate before calling
// ensure all stored documents contain only Jackson-serializable fields objects.forEach(d -> assertSerializable(d));
Try / catch
try {
vectorStore.persist(file);
} catch (RuntimeException e) {
if (e.getMessage() != null && e.getMessage().contains("serializing documentMap")) {
logger.error("Unserializable document in store", e.getCause());
}
throw e;
} Prevention
- Keep Document payloads to JSON-friendly types.
- Mark transient/debug fields with transient or @JsonIgnore.
- Keep Jackson versions aligned with the Spring AI BOM.
When it happens
Trigger: 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.
Common situations: Custom Document subclasses with non-serializable fields; mixing documents loaded from another store version; classpath Jackson version mismatch.
Understand the failure class
Background: json.Marshal / "failed to marshal" errors in Go: why "unsupported type" happens and how to fix it — this error's family across 22 libraries.
Related errors
- Invalid JSON format for the input request:
- Failed to serialize the Document metadata:
- Unknown message type:
- 'spring.ai.chat.client.tool-search-advisor.tool-index-type=v
- Failed to convert JsonValue to string
AI-assisted analysis of spring-projects/spring-ai@98a7beda4f (2026-09-11).
Data as JSON: /api/errors/49d336cba5efb740.
Report an issue: GitHub.