boto/boto3 · error · TypeError

Infinity and NaN not supported

Error message

Infinity and NaN not supported

What it means

TypeSerializer._serialize_n converts the value to a Decimal via DYNAMODB_CONTEXT.create_decimal and stringifies it. DynamoDB rejects the special decimal values Infinity and NaN, so boto3 raises TypeError('Infinity and NaN not supported') when the stringified form is one of those.

Solutions

  1. Filter or replace inf/NaN before storage (e.g. use None for missing, or a sentinel Decimal('0')).
  2. Guard numeric inputs with math.isfinite or Decimal.is_finite().
  3. Validate at the boundary: reject rows whose numeric fields are not finite.

Example fix

# before
from decimal import Decimal
table.put_item(Item={'ratio': Decimal('NaN')})
# after
import math
from decimal import Decimal
val = Decimal('NaN')
safe = val if val.is_finite() else None
table.put_item(Item={'ratio': safe})
Defensive patterns

Strategy: validation

Validate before calling

from decimal import Decimal
import math
def finite_decimal(v):
    d = v if isinstance(v, Decimal) else Decimal(str(v))
    if not d.is_finite():
        raise ValueError(f'non-finite numeric: {v!r}')
    return d

Type guard

from decimal import Decimal
def is_finite_decimal(v) -> bool:
    try:
        d = v if isinstance(v, Decimal) else Decimal(str(v))
    except Exception:
        return False
    return d.is_finite()

Try / catch

try:
    table.put_item(Item=item)
except TypeError as e:
    if 'Infinity and NaN' in str(e):
        item = {k: (None if (isinstance(v, Decimal) and not v.is_finite()) else v)
                for k, v in item.items()}
        table.put_item(Item=item)
    else:
        raise

Prevention

When it happens

Trigger: Storing float('inf'), float('nan'), Decimal('Infinity'), Decimal('NaN'), or arithmetic that yields them (e.g. division by zero with Decimal context allowing it).

Common situations: Aggregations/statistics producing NaN from divide-by-zero; sensor data with inf; migrating NaN values from pandas.

Related errors


AI-assisted analysis of boto/boto3@6e10b029c1 (2026-08-11). Data as JSON: /api/errors/c6acedf2576d2cf6. Report an issue: GitHub.

Appendix: source

Thrown at boto3/dynamodb/types.py:216

        if isinstance(value, collections_abc.Mapping):
            return True
        return False

    def _is_listlike(self, value):
        if isinstance(value, (list, tuple)):
            return True
        return False

    def _serialize_null(self, value):
        return True

    def _serialize_bool(self, value):
        return value

    def _serialize_n(self, value):
        number = str(DYNAMODB_CONTEXT.create_decimal(value))
        if number in ['Infinity', 'NaN']:
            raise TypeError('Infinity and NaN not supported')
        return number

    def _serialize_s(self, value):
        return value

    def _serialize_b(self, value):
        if isinstance(value, Binary):
            value = value.value
        return value

    def _serialize_ss(self, value):
        return [self._serialize_s(s) for s in value]

    def _serialize_ns(self, value):
        return [self._serialize_n(n) for n in value]

    def _serialize_bs(self, value):
        return [self._serialize_b(b) for b in value]

View on GitHub (pinned to 6e10b029c1)