Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 883
GarlicBread Avatar asked Aug 31 '25 10:08

GarlicBread


2 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
)
like image 199
M S R Avatar answered Sep 03 '25 00:09

M S R


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().

Example from comment:

item_to_put: dict = {} 
for item_key in item: 
    item_to_put[item_key] = str(item[item_key])
like image 26
P Marx Avatar answered Sep 02 '25 22:09

P Marx