Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: DynamoDB Typeerror with AWS Bedrock #1163

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
35 changes: 34 additions & 1 deletion backend/chainlit/data/dynamodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
import os
import random
from decimal import Decimal
from dataclasses import asdict
from datetime import datetime
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union
Expand Down Expand Up @@ -60,17 +61,49 @@ def __init__(
def _get_current_timestamp(self) -> str:
return datetime.now().isoformat() + "Z"

def _convert_floats_to_decimal(self, obj):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add type hints here so that we can use type checking to ensure that what we think this is doing is actually what it's doing?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And a docstring, if possible.

if isinstance(obj, list):
return [self._convert_floats_to_decimal(i) for i in obj]

for key, value in obj.items():
if isinstance(value, float):
obj[key] = Decimal(str(value))
elif isinstance(value, dict):
self._convert_floats_to_decimal(value)
elif isinstance(value, list):
obj[key] = [self._convert_floats_to_decimal(i) for i in value]

return obj

def _convert_decimal_to_floats(self, obj):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type hints here as well please. 🙏🏼

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And a docstring, if possible.

if isinstance(obj, list):
return [self._convert_decimal_to_floats(i) for i in obj]

for key, value in obj.items():
if isinstance(value, Decimal):
obj[key] = float(value)
elif isinstance(value, dict):
self._convert_decimal_to_floats(value)
elif isinstance(value, list):
obj[key] = [self._convert_decimal_to_floats(i) for i in value]

return obj

def _serialize_item(self, item: Dict[str, Any]) -> Dict[str, Any]:
item = self._convert_floats_to_decimal(item)
return {
key: self._type_serializer.serialize(value) for key, value in item.items()
key: self._type_serializer.serialize(value)
for key, value in item.items()
}

def _deserialize_item(self, item: Dict[str, Any]) -> Dict[str, Any]:
item = self._convert_decimal_to_floats(item)
return {
key: self._type_deserializer.deserialize(value)
for key, value in item.items()
}


def _update_item(self, key: Dict[str, Any], updates: Dict[str, Any]):
update_expr: List[str] = []
expression_attribute_names = {}
Expand Down