redis/jedis · error · IllegalArgumentException

" + key.toString() + " is not a valid argument.

Error message

" + key.toString() + " is not a valid argument.

What it means

PrefixedKeyArgumentPreProcessor.prefixKey namespaces keys by prepending a prefix. It supports byte[] and String keys (via GSON JsonObject/JsonElement paths above); any other key type reaching it throws IllegalArgumentException("\"<key>\" is not a valid argument.").

Solutions

  1. Convert the key to String (or byte[]) before passing it, e.g. String.valueOf(id).
  2. Check the accepted key types in prefixKey and normalize your key-building code to one of them.
  3. If keys come from generic code, add a central toKey() helper that always produces String.

Example fix

// before
long userId = 42;
commands.get(userId); // not a valid argument
// after
commands.get(String.valueOf(userId));
Defensive patterns

Strategy: type-guard

Validate before calling

static Object toKey(Object k) {
  if (k instanceof String || k instanceof byte[]) return k;
  return String.valueOf(k);
}

Type guard

static boolean isValidPrefixedKey(Object k) {
  return k instanceof String || k instanceof byte[];
}

Prevention

When it happens

Trigger: Passing a key that is neither byte[] nor String (nor the supported JSON element types) to the prefixing pre-processor, e.g. a raw Long/UUID/custom object used as a Redis key with key-prefixing enabled.

Common situations: Passing numeric IDs or enum objects directly as keys instead of converting to String; custom key types after a version change of the API surface.

Understand the failure class

Background: "must be a positive integer", "cannot be empty", "invalid argument": how invalid-argument errors work across open-source libraries — this error's family across 33 libraries.

Related errors


AI-assisted analysis of redis/jedis@6dac31d4c2 (2026-09-08). Data as JSON: /api/errors/0c528ad40db050ef. Report an issue: GitHub.

Appendix: source

Thrown at src/main/java/redis/clients/jedis/util/PrefixedKeyArgumentPreProcessor.java:38

    this.prefixBytes = prefixBytes;
  }

  @Override
  public Object actualKey(Object paramKey) {
    return prefixKey(paramKey, prefixString, prefixBytes);
  }

  private static Object prefixKey(Object key, String prefixString, byte[] prefixBytes) {
    if (key instanceof Rawable) {
      byte[] raw = ((Rawable) key).getRaw();
      return RawableFactory.from(prefixKeyWithBytes(raw, prefixBytes));
    } else if (key instanceof byte[]) {
      return prefixKeyWithBytes((byte[]) key, prefixBytes);
    } else if (key instanceof String) {
      String raw = (String) key;
      return prefixString + raw;
    }
    throw new IllegalArgumentException("\"" + key.toString() + "\" is not a valid argument.");
  }

  private static byte[] prefixKeyWithBytes(byte[] key, byte[] prefixBytes) {
    byte[] namespaced = new byte[prefixBytes.length + key.length];
    System.arraycopy(prefixBytes, 0, namespaced, 0, prefixBytes.length);
    System.arraycopy(key, 0, namespaced, prefixBytes.length, key.length);
    return namespaced;
  }
}

View on GitHub (pinned to 6dac31d4c2)