Skip to content

Bug: ConsumerRecordRecords.key returns None when record key is an empty string #8696

Open

Description

@ASHMEET555

Expected Behaviour

When a Kafka event contains an empty key ("key": ""), ConsumerRecordRecords.key should deserialize the empty string and return an empty key (e.g. "" or b""), matching Kafka semantics and KafkaEventRecord.decoded_key behavior. Only missing or null keys (None) should evaluate to None.

Current Behaviour

In aws_lambda_powertools/utilities/kafka/consumer_records.py (line 35):

key = self.get("key")

# Return None if key doesn't exist
if not key:
    return None

Because if not key: performs a truthiness check rather than an identity check against None, any empty base64 string ("key": "") is treated as nonexistent and returns None.

In contrast, KafkaEventRecord.decoded_key in aws_lambda_powertools.utilities.data_classes.kafka_event correctly distinguishes between missing and empty keys:

return None if self.key is None else base64.b64decode(self.key)

Code snippet

from aws_lambda_powertools.utilities.kafka.consumer_records import ConsumerRecordRecords

data = {
    "topic": "test-topic",
    "partition": 0,
    "offset": 100,
    "timestamp": 1690000000000,
    "timestampType": "CREATE_TIME",
    "key": "",  # Empty key representation in Kafka event
    "value": "dGVzdC12YWx1ZQ==",
}

record = ConsumerRecordRecords(data=data)

# Fails: returns None instead of empty decoded key
assert record.key is not None

Possible Solution

Update line 35 of aws_lambda_powertools/utilities/kafka/consumer_records.py:

-        if not key:
+        if key is None:
             return None

And add unit regression tests verifying empty string key handling.

Steps to Reproduce

  1. Instantiate ConsumerRecordRecords with a dict containing "key": "".
  2. Access record.key.
  3. Observe it returns None instead of deserializing the empty key.

Powertools for AWS Lambda (Python) version

latest

AWS Lambda function runtime

3.12

Packaging format used

PyPi

Activity

  1. boring-cyborg commented on Oct 28, 2026

    @boring-cyborg

    Thanks for opening your first issue here! We'll come back to you as soon as we can.
    In the meantime, check out the #python channel on our Powertools for AWS Lambda Discord: Invite link

  2. powertools-for-aws-oss-automation commented on Oct 28, 2026

    @powertools-for-aws-oss-automation

    Thanks for opening your first issue here! We'll come back to you as soon as we can.
    In the meantime, check out our GitHub Discussions: Join the conversation

  3. leandrodamascena commented on Oct 29, 2026

    @leandrodamascena
    Contributor

    Thanks @ASHMEET555, confirmed: a record whose key is an empty string comes back as key=None, so a consumer can't tell "no key" from "empty key". It's yours, I'll review #8698. This goes into 3.40.0.

  4. added this to the 3.40.0 milestone on Oct 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingevent_sourcesEvent Source Data Class utility

Type

No type

Projects

Relationships

None yet

Development

No branches or pull requests

Issue actions