alibaba/spring-ai-alibaba · error · IllegalArgumentException

key cannot be empty

Error message

key cannot be empty

What it means

BaseStore.validateGetItem rejects a key that is null or blank (key.trim().isEmpty()) when reading a store item. An empty key cannot identify an item, so the library treats it as an invalid argument. Whitespace-only keys are also rejected.

Source

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

		if (item.getKey() == null || item.getKey().trim().isEmpty()) {
			throw new IllegalArgumentException("key cannot be null or empty");
		}
	}

	/**
	 * Validates the getItem parameters.
	 * @param namespace namespace
	 * @param key key
	 */
	protected void validateGetItem(List<String> namespace, String key) {
		if (namespace == null) {
			throw new IllegalArgumentException("namespace cannot be null");
		}
		if (key == null) {
			throw new IllegalArgumentException("key cannot be null");
		}
		if (key.trim().isEmpty()) {
			throw new IllegalArgumentException("key cannot be empty");
		}
	}

	/**
	 * Validates the deleteItem parameters.
	 * @param namespace namespace
	 * @param key key
	 */
	protected void validateDeleteItem(List<String> namespace, String key) {
		if (namespace == null) {
			throw new IllegalArgumentException("namespace cannot be null");
		}
		if (key == null) {
			throw new IllegalArgumentException("key cannot be null");
		}
		if (key.trim().isEmpty()) {
			throw new IllegalArgumentException("key cannot be empty");
		}

View on GitHub (pinned to f82da0b50f)

Solutions

  1. Validate the key is non-blank before calling getItem and fail fast with a clear message.
  2. Trim user-supplied keys and reject empties at your API boundary.
  3. Provide a default key or return a not-found response instead of calling getItem with a blank key.

Example fix

// before
store.getItem(List.of("users"), key.trim());
// after
String trimmed = key == null ? "" : key.trim();
if (trimmed.isEmpty()) {
    return Optional.empty();
}
store.getItem(List.of("users"), trimmed);
Defensive patterns

Strategy: validation

Validate before calling

if (key == null || key.trim().isEmpty()) {
    throw new IllegalArgumentException("store item key must be non-blank");
}
store.getItem(namespace, key.trim());

Type guard

boolean isNotBlank(String s) { return s != null && !s.trim().isEmpty(); }

Try / catch

try {
    store.getItem(namespace, key);
} catch (IllegalArgumentException e) {
    return Optional.empty(); // treat blank key as not-found
}

Prevention

When it happens

Trigger: Calling store.getItem(namespace, "") or getItem(namespace, " ") with a blank/whitespace-only key string.

Common situations: Unset or defaulted string fields (e.g. String key = ""), template placeholders never substituted, empty form/query parameters forwarded directly to the store API.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


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