apache/cassandra · error · InvalidRequestException
Invalid map literal for %s: value %s is not of type %s
Error message
Invalid map literal for %s: value %s is not of type %s
What it means
Fired by Maps literal preparation when a map literal value element's type is not assignable to the declared value type of the target map column. It is a statement-preparation validation guard reached after key validation; the offending value expression fails type assignability against MapType.getValueType().
Source
Thrown at src/java/org/apache/cassandra/cql3/terms/Maps.java:221
MultiElements.DelayedValue value = new MultiElements.DelayedValue((MultiElementType<?>) receiver.type.unwrap(), values);
return allTerminal ? value.bind(QueryOptions.DEFAULT) : value;
}
private void validateAssignableTo(String keyspace, ColumnSpecification receiver) throws InvalidRequestException
{
AbstractType<?> type = receiver.type.unwrap();
if (!(type instanceof MapType))
throw new InvalidRequestException(String.format("Invalid map literal for %s of type %s", receiver.name, receiver.type.asCQL3Type()));
ColumnSpecification keySpec = Maps.keySpecOf(receiver);
ColumnSpecification valueSpec = Maps.valueSpecOf(receiver);
for (Pair<Term.Raw, Term.Raw> entry : entries)
{
if (!entry.left.testAssignment(keyspace, keySpec).isAssignable())
throw new InvalidRequestException(String.format("Invalid map literal for %s: key %s is not of type %s", receiver.name, entry.left, keySpec.type.asCQL3Type()));
if (!entry.right.testAssignment(keyspace, valueSpec).isAssignable())
throw new InvalidRequestException(String.format("Invalid map literal for %s: value %s is not of type %s", receiver.name, entry.right, valueSpec.type.asCQL3Type()));
}
}
public AssignmentTestable.TestResult testAssignment(String keyspace, ColumnSpecification receiver)
{
return testMapAssignment(receiver, entries);
}
@Override
public AbstractType<?> getExactTypeIfKnown(String keyspace)
{
return getExactMapTypeIfKnown(entries, p -> p.getExactTypeIfKnown(keyspace));
}
@Override
public AbstractType<?> getCompatibleTypeIfKnown(String keyspace)
{
return Maps.getPreferredCompatibleType(entries, p -> p.getCompatibleTypeIfKnown(keyspace));View on GitHub (pinned to 88fd0f6a0e)
Solutions
- Make literal values match the declared value type (e.g. `{'k': 42}` for map<text,int>).
- Check the value type in system_schema.columns and align the query.
- Bind the whole map as a typed parameter so the driver performs type conversion.
Example fix
// before (map<text,int>)
"UPDATE t SET m = {'k':'42'} WHERE k=?"
// after
"UPDATE t SET m = {'k':42} WHERE k=?" Defensive patterns
Strategy: validation
Validate before calling
// values must be literals assignable to the map value type, e.g. map<?,int>
Object v = entry.getValue();
if (!(v instanceof Integer)) throw new IllegalArgumentException("value must be int, got " + v.getClass()); Try / catch
try { session.prepare(sql); } catch (InvalidRequestException e) { if (e.getMessage().contains("value") && e.getMessage().contains("is not of type")) { /* fix literal value types */ } else throw e; } Prevention
- Match literal forms to declared value types (no quotes for numbers)
- Bind whole maps with typed codecs instead of string literals
- Run query validation against a test schema in CI
When it happens
Trigger: Preparing e.g. `SET m = {'k': 'not-an-int'}` where the value type is int (map<text,int>), or nested collection values of the wrong type.
Common situations: Sending string values for numeric/timestamp value types; schema migration changed the value type; templated queries injecting wrong literal forms.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- Invalid map literal for %s: key %s is not of type %s
- Function %s requires a map argument, but found argument %s o
- Invalid %s constant (%s) for "%s" of type %s
- Invalid map literal for %s of type %s
- Invalid tuple literal for %s: component %d is not of type %s
AI-assisted analysis of apache/cassandra@88fd0f6a0e (2026-09-10).
Data as JSON: /api/errors/f55a30db200e23fa.
Report an issue: GitHub.