Python / Boto3: Float Types Are Not Supported. Use Decimal Types Instead

I'm using Python 3.7 to store data in a DynamoDB database and encountering the following error message when I try and write an item to the database:

Float types are not supported. Use Decimal types instead.

My code:

ddb_table = my_client.Table(table_name)

with ddb_table.batch_writer() as batch:
    for item in items:
        item_to_put: dict = json.loads(json.dumps(item), parse_float=Decimal)

        # Send record to database.
        batch.put_item(Item=item_to_put)

"items" is a list of Python dicts. If I print out the types of the "item_to_put" dict, they are all of type str.

Thanks in advance for any assistance.

2

4 Answers

Convert all the float to the Decimal

import json
from decimal import Decimal
item = json.loads(json.dumps(item), parse_float=Decimal)

table.put_item(
    Item=item
)
2

Ran into the same issue and it turned out that Python was passing a string parameter as something else. The issue went away when I wrapped all of the items in str().

2

Same issue here. In my case it was because some of my records did not have a certain key, and that key was being auto-added with nan by default(I guess?). I added some simple logic to check if that key was a list and set it to an empty list if not before running batch.put_item which has seemingly satisfied the Dynamo beast.

Add: from decimal import Decimal above the expression. This should fix the problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

David Miller

David Miller

Executive Financial & Market Analyst

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.

Share this article
Twitter Facebook Pinterest