{"record":{"id":"3614ab5464d8d4ae","repo":"boto/boto3","slug":"value-must-be-a-nonempty-dictionary-whose-key-is-a","errorCode":null,"errorMessage":"Value must be a nonempty dictionary whose key is a valid dynamodb type.","messagePattern":"Value must be a nonempty dictionary whose key is a valid dynamodb type\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"boto3/dynamodb/types.py","lineNumber":269,"sourceCode":"\n            DynamoDB                                Python\n            --------                                ------\n            {'NULL': True}                          None\n            {'BOOL': True/False}                    True/False\n            {'N': str(value)}                       Decimal(str(value))\n            {'S': string}                           string\n            {'B': bytes}                            Binary(bytes)\n            {'NS': [str(value)]}                    set([Decimal(str(value))])\n            {'SS': [string]}                        set([string])\n            {'BS': [bytes]}                         set([bytes])\n            {'L': list}                             list\n            {'M': dict}                             dict\n\n        :returns: The pythonic value of the DynamoDB type.\n        \"\"\"\n\n        if not value:\n            raise TypeError(\n                'Value must be a nonempty dictionary whose key '\n                'is a valid dynamodb type.'\n            )\n        dynamodb_type = list(value.keys())[0]\n        try:\n            deserializer = getattr(\n                self, f'_deserialize_{dynamodb_type}'.lower()\n            )\n        except AttributeError:\n            raise TypeError(f'Dynamodb type {dynamodb_type} is not supported')\n        return deserializer(value[dynamodb_type])\n\n    def _deserialize_null(self, value):\n        return None\n\n    def _deserialize_bool(self, value):\n        return value\n","sourceCodeStart":251,"sourceCodeEnd":287,"githubUrl":"https://github.com/boto/boto3/blob/6e10b029c1326a437932a8b24f38af69fd986e15/boto3/dynamodb/types.py#L251-L287","documentation":"TypeDeserializer.deserialize expects a nonempty dict shaped like {'S': ...}, {'N': ...}, etc. The first check 'if not value' catches None, {}, and other falsy inputs and raises TypeError, because an empty dictionary gives no type key to dispatch on.","triggerScenarios":"Calling TypeDeserializer().deserialize({}) or deserialize(None); receiving a malformed attribute value from a non-AWS source or a hand-built dict where the value was popped.","commonSituations":"Iterating over item attributes and accidentally passing an empty dict; integrating DynamoDB Streams data that has been filtered; testing with placeholder empty values.","solutions":["Skip or default None/empty dict inputs before calling deserialize.","Validate that the value is a dict with exactly one key that is a known DynamoDB type tag.","Build a small helper that returns None for empty/None payloads instead of forwarding them."],"exampleFix":"# before\nd = TypeDeserializer()\nval = d.deserialize({})\n# after\nd = TypeDeserializer()\nraw = {}\nval = d.deserialize(raw) if raw else None","handlingStrategy":"validation","validationCode":"VALID = {'S','N','B','SS','NS','BS','BOOL','NULL','L','M'}\ndef safe_deserialize(d, TypeDeserializer):\n    if not isinstance(d, dict) or not d:\n        return None\n    assert set(d).issubset(VALID), f'unexpected type tags: {set(d) - VALID}'\n    return TypeDeserializer().deserialize(d)","typeGuard":"VALID = {'S','N','B','SS','NS','BS','BOOL','NULL','L','M'}\ndef is_dynamo_value(v) -> bool:\n    return isinstance(v, dict) and len(v) == 1 and next(iter(v)) in VALID","tryCatchPattern":"from boto3.dynamodb.types import TypeDeserializer\nd = TypeDeserializer()\ntry:\n    val = d.deserialize(raw)\nexcept TypeError:\n    val = None","preventionTips":["Skip empty/None payloads before deserialize.","Validate single-key dict shape at integration boundaries.","Prefer the high-level resource API which deserializes for you."],"tags":["dynamodb","types","deserialization","boto3"],"backgroundTag":null,"analyzedSha":"6e10b029c1326a437932a8b24f38af69fd986e15","analyzedAt":"2026-08-11T20:52:47.213Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}