Dynamo Chatmessage history #26543
-
Dynamo chatmessage history gives an error like Nothing is storing in the Table , table has been created and here is how i am accessing
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
To resolve the error Here is a modified version of the from decimal import Decimal
def add_message(self, message: BaseMessage) -> None:
"""Append the message to the record in DynamoDB"""
try:
from botocore.exceptions import ClientError
except ImportError as e:
raise ImportError(
"Unable to import botocore, please install with `pip install botocore`."
) from e
messages = messages_to_dict(self.messages)
_message = message_to_dict(message)
messages.append(_message)
if self.history_size:
messages = messages[-self.history_size:]
# Convert float values to Decimal
def convert_floats(item):
if isinstance(item, list):
return [convert_floats(i) for i in item]
elif isinstance(item, dict):
return {k: convert_floats(v) for k, v in item.items()}
elif isinstance(item, float):
return Decimal(str(item))
return item
messages = convert_floats(messages)
try:
if self.ttl:
import time
expireAt = int(time.time()) + self.ttl
self.table.put_item(
Item={
**self.key,
self.history_messages_key: messages,
self.ttl_key_name: expireAt,
}
)
else:
self.table.put_item(
Item={**self.key, self.history_messages_key: messages}
)
except ClientError as err:
logger.error(err) This code includes a helper function |
Beta Was this translation helpful? Give feedback.
To resolve the error
TypeError('Float types are not supported. Use Decimal types instead.')
when usingDynamoDBChatMessageHistory
, you need to ensure that any float values being stored in DynamoDB are converted toDecimal
types. This is because DynamoDB does not support float types directly.Here is a modified version of the
add_message
method that converts float values toDecimal
before storing them in DynamoDB: