alibaba/spring-ai-alibaba · error · RuntimeException

Failed to store item in MongoDB-like storage

Error message

Failed to store item in MongoDB-like storage

What it means

MongoStore.putItem serializes the StoreItem into a document map and inserts it into the in-memory mongoLikeCollection; any exception is wrapped in RuntimeException('Failed to store item in MongoDB-like storage'). It executes under the store write lock.

Solutions

  1. Read getCause() to identify whether it is id-construction or serialization
  2. Validate namespace/key and value are non-null before putItem
  3. Ensure stored values are JSON-serializable POJOs/maps
  4. If backed by a real MongoDB, verify connection settings and availability

Example fix

// before
store.putItem(ns, key, someObjectWithCycles);
// after
String json = objectMapper.writeValueAsString(someObjectWithCycles);
store.putItem(ns, key, objectMapper.readValue(json, Map.class));
Defensive patterns

Strategy: try-catch

Validate before calling

if (key == null || key.isBlank()) throw new IllegalArgumentException("key required");
Objects.requireNonNull(value, "value required");

Try / catch

try {
    store.putItem(namespace, key, value);
} catch (RuntimeException e) {
    logger.error("put failed: {}", e.getCause());
    throw new StoreException("persist failed", e);
}

Prevention

When it happens

Trigger: store.putItem(namespace, key, value) throws — e.g. createDocumentId fails on invalid namespace/key, serialization of the value fails, or the backing collection rejects the entry.

Common situations: Non-serializable or cyclic objects stored as values; null/blank keys bubbling up from document-id construction; swapping MongoStore for a real MongoDB client whose connection fails inside put.

Related errors


AI-assisted analysis of alibaba/spring-ai-alibaba@f82da0b50f (2026-09-09). Data as JSON: /api/errors/2b3492d489bb8f80. Report an issue: GitHub.

Appendix: source

Thrown at spring-ai-alibaba-graph-core/src/main/java/com/alibaba/cloud/ai/graph/store/stores/MongoStore.java:94

	public void putItem(StoreItem item) {
		validatePutItem(item);

		lock.writeLock().lock();
		try {
			String documentId = createDocumentId(item.getNamespace(), item.getKey());

			Map<String, Object> doc = new HashMap<>();
			doc.put("_id", documentId);
			doc.put("namespace", item.getNamespace());
			doc.put("key", item.getKey());
			doc.put("value", item.getValue());
			doc.put("createdAt", item.getCreatedAt());
			doc.put("updatedAt", item.getUpdatedAt());

			mongoLikeCollection.put(documentId, doc);
		}
		catch (Exception e) {
			throw new RuntimeException("Failed to store item in MongoDB-like storage", e);
		}
		finally {
			lock.writeLock().unlock();
		}
	}

	@Override
	public Optional<StoreItem> getItem(List<String> namespace, String key) {
		validateGetItem(namespace, key);

		lock.readLock().lock();
		try {
			String documentId = createDocumentId(namespace, key);
			Map<String, Object> doc = mongoLikeCollection.get(documentId);

			if (doc == null) {
				return Optional.empty();
			}

View on GitHub (pinned to f82da0b50f)