# Buggy code
def replace_floats_with_decimals(obj: Union[List, Dict], round_digits: int = 9) -> Any:
    """
    Utility method to recursively walk a dictionary or list converting all `float` to `Decimal` as required by DynamoDb.

    Args:
        obj: A `List` or `Dict` object.
        round_digits: A int value to set the rounding precision of Decimal values.

    Returns: An object with all matching values and `float` types replaced by `Decimal`s rounded to `round_digits` decimal places.

    """
    if isinstance(obj, list):
        for i in range(len(obj)):
            obj[i] = replace_floats_with_decimals(obj[i], round_digits)
        return obj
    elif isinstance(obj, dict):
        for j in obj:
            obj[j] = replace_floats_with_decimals(obj[j], round_digits)
        return obj
    elif isinstance(obj, float) or isinstance(obj, Decimal):
        return round(Decimal(obj), round_digits)
    else:
        return obj


# Traceback (TypeError)
2020-01-23 11:47:22 DEBUG    rasa.core.processor  - Object of type Decimal is not JSON serializable
Traceback (most recent call last):
File "/Users/dsammut/Repos/chatbot-rasa/venv/lib/python3.7/site-packages/rasa/core/processor.py", line 564, in _run_action
events = await action.run(output_channel, nlg, tracker, self.domain)
File "/Users/dsammut/Repos/chatbot-rasa/venv/lib/python3.7/site-packages/rasa/core/actions/action.py", line 541, in run
json=json_body, method="post", timeout=DEFAULT_REQUEST_TIMEOUT
File "/Users/dsammut/Repos/chatbot-rasa/venv/lib/python3.7/site-packages/rasa/utils/endpoints.py", line 146, in request
**kwargs,
File "/Users/dsammut/Repos/chatbot-rasa/venv/lib/python3.7/site-packages/aiohttp/client.py", line 1012, in __aenter__
self._resp = await self._coro
File "/Users/dsammut/Repos/chatbot-rasa/venv/lib/python3.7/site-packages/aiohttp/client.py", line 365, in _request
data = payload.JsonPayload(json, dumps=self._json_serialize)
File "/Users/dsammut/Repos/chatbot-rasa/venv/lib/python3.7/site-packages/aiohttp/payload.py", line 389, in __init__
dumps(value).encode(encoding),
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type Decimal is not JSON serializable

# Fixed code
def replace_floats_with_decimals(obj: Any, round_digits: int = 9) -> Any:
    """Convert all instances in `obj` of `float` to `Decimal`.

    Args:
        obj: Input object.
        round_digits: Rounding precision of `Decimal` values.

    Returns:
        Input `obj` with all `float` types replaced by `Decimal`s rounded to
        `round_digits` decimal places.
    """

    def _float_to_rounded_decimal(s: Text) -> Decimal:
        return Decimal(s).quantize(Decimal(10) ** -round_digits)

    return json.loads(json.dumps(obj), parse_float=_float_to_rounded_decimal)