Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid Type error for PutItem AWS DynamoDB

We have a DynamoDB table with a partition key and sort key. Both the keys are Number type. I am trying to insert items using the PutItem API of the boto3 DynamoDB client. I am getting the below error;

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\satis\Anaconda3\envs\py369\lib\site-packages\botocore\client.py", line 316, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "C:\Users\satis\Anaconda3\envs\py369\lib\site-packages\botocore\client.py", line 599, in _make_api_call
    api_params, operation_model, context=request_context)
  File "C:\Users\satis\Anaconda3\envs\py369\lib\site-packages\botocore\client.py", line 647, in _convert_to_request_dict
    api_params, operation_model)
  File "C:\Users\satis\Anaconda3\envs\py369\lib\site-packages\botocore\validate.py", line 297, in serialize_to_request
    raise ParamValidationError(report=report.generate_report())
botocore.exceptions.ParamValidationError: Parameter validation failed:
Invalid type for parameter Item.doc_id.N, value: 1234, type: <class 'int'>, valid types: <class 'str'>
Invalid type for parameter Item.passage_id.N, value: 1, type: <class 'int'>, valid types: <class 'str'>

The error does not make sense as the value for the sort key I am inserting is an integer and matches with the Number type of the dynamodb. The error clearly states that the value type is an integer but the required type is a string. Not sure what I am missing here.

Following is the data I am trying to insert;

{'doc_id': {'N': 1234}, 'passage_id': {'N': 1}, 'text': {'S': 'some text to insert'}, 'urls': {'S': '[]'}, 'html': {'S': '<p>this is para</p>'}}

Following is the code I am using to insert above dictionary;

import boto3
dynamodb_client=boto3.client('dynamodb',region_name='region_name')
dynamodb_client.put_item(TableName='table_name',Item=data)

To reiterate, both the key viz. doc_id and passage_id are Number type in dynamodb.

Appreciate the help.

like image 841
satish silveri Avatar asked Dec 30 '25 12:12

satish silveri


1 Answers

Even though it is a number, you still need to put a string value. The "N" keyword identifies it as a number.

"PutRequest": { 
    "Item": { 
        "string" : { 
            "B": blob,
            ...
            "N": "string",
            "NS": [ "string" ],
            "NULL": boolean,
            "S": "string",
            "SS": [ "string" ]
        }
    }
}

You can refer to the official BatchWriteItem documentation here: https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_BatchWriteItem.html

In your case, you need to specify the doc ID "1234" as a string, with "N" (Number) as the data type.

like image 78
user14365066 Avatar answered Jan 01 '26 03:01

user14365066



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!