ValidationException: The provided key element does not match the schema
TL;DR — The key you passed doesn't line up with the table's declared key schema: wrong attribute name, wrong type (string vs number), or a missing sort key. Match the request's key exactly to KeySchema + AttributeDefinitions.
What it means
Every DynamoDB item is addressed by its primary key — a partition key, optionally plus a sort key — with fixed names and types set at table creation. GetItem, DeleteItem, UpdateItem and each Key in a batch must supply exactly that key. This error fires when the supplied key doesn't match.
Why it happens
- Wrong attribute name — you passed
idbut the table's key ispk. - Wrong type — the key is defined as Number (
N) but you sent a String ("123"), or vice-versa."123"and123are different keys to DynamoDB. - Missing the sort key — the table has a composite key but your
Keyonly has the partition key (or an extra sort key on a partition-only table). - Extra attributes in
Key— theKeymap must contain only the key attributes, nothing else.
How to fix it
- Check the table's key schema (
DescribeTable→KeySchema+AttributeDefinitions), then make the request'sKeymatch name-for-name and type-for-type. - Fix number/string mismatches. If the key is
N, pass a JS number (the Document Client marshals it); with the low-level client use{N: '123'}, not{S: '123'}. - Supply the full composite key. Composite-key tables need both partition and sort key on every item-based call.
Example
// Table: Users, key = { pk (S) HASH, sk (S) RANGE }
import {DynamoDBClient} from '@aws-sdk/client-dynamodb';
import {DynamoDBDocumentClient, GetCommand} from '@aws-sdk/lib-dynamodb';
const doc = DynamoDBDocumentClient.from(new DynamoDBClient({}));
// both key parts, correct names/types
await doc.send(new GetCommand({TableName: 'Users', Key: {pk: 'USER#1', sk: 'PROFILE'}}));
// missing sort key → "provided key element does not match the schema"
// await doc.send(new GetCommand({TableName: 'Users', Key: {pk: 'USER#1'}}));