From d139eab04959c102fa48e0c50a83515aa5b491b8 Mon Sep 17 00:00:00 2001 From: Sumedh Aerram Date: Sat, 28 Nov 2026 15:55:25 -0800 Subject: [PATCH] fix(kafka): return None for the value of tombstone records --- .../references/kafka.md | 4 ++-- .../utilities/kafka/consumer_records.py | 12 ++++++++---- docs/utilities/kafka.md | 4 ++-- .../test_kafka_consumer.py | 19 +++++++++++++++++++ 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/.agents/skills/building-lambda-functions-with-powertools-python/references/kafka.md b/.agents/skills/building-lambda-functions-with-powertools-python/references/kafka.md index f1b7e0b16c3..167cbf2cc69 100644 --- a/.agents/skills/building-lambda-functions-with-powertools-python/references/kafka.md +++ b/.agents/skills/building-lambda-functions-with-powertools-python/references/kafka.md @@ -617,8 +617,8 @@ def lambda_handler(event: ConsumerRecords, context: LambdaContext): | `timestamp_type` | Timestamp type (CREATE_TIME or LOG_APPEND_TIME) | Data lineage verification | | `headers` | Key-value pairs attached to the message | Cross-cutting concerns like correlation IDs | | `key` | Deserialized message key | Customer ID or entity identifier | -| `value` | Deserialized message content | The actual business data | -| `original_value` | Base64-encoded original message value | Debugging or custom deserialization | +| `value` | Deserialized message content, or `None` for a tombstone record | The actual business data | +| `original_value` | Base64-encoded original message value, or `None` for a tombstone record | Debugging or custom deserialization | | `original_key` | Base64-encoded original message key | Debugging or custom deserialization | | `value_schema_metadata` | Metadata about the value schema like `schemaId` and `dataFormat` | Data format and schemaId propagated when integrating with Schema Registry | | `key_schema_metadata` | Metadata about the key schema like `schemaId` and `dataFormat` | Data format and schemaId propagated when integrating with Schema Registry | diff --git a/aws_lambda_powertools/utilities/kafka/consumer_records.py b/aws_lambda_powertools/utilities/kafka/consumer_records.py index 672f0d9f26c..2907b330a22 100644 --- a/aws_lambda_powertools/utilities/kafka/consumer_records.py +++ b/aws_lambda_powertools/utilities/kafka/consumer_records.py @@ -66,7 +66,11 @@ def key(self) -> Any: @cached_property def value(self) -> Any: - value = self["value"] + value = self.get("value") + + # Return None for a tombstone record, which Lambda delivers without a value or with a null one + if value is None: + return None # Determine schema type and schema string schema_type = None @@ -98,9 +102,9 @@ def value(self) -> Any: return deserialized_value @property - def original_value(self) -> str: - """The original (base64 encoded) Kafka record value.""" - return self["value"] + def original_value(self) -> str | None: + """The original (base64 encoded) Kafka record value, or None for a tombstone record.""" + return self.get("value") @property def original_key(self) -> str | None: diff --git a/docs/utilities/kafka.md b/docs/utilities/kafka.md index 5fafd65df26..fed3ce1084f 100644 --- a/docs/utilities/kafka.md +++ b/docs/utilities/kafka.md @@ -250,8 +250,8 @@ Each Kafka record contains important metadata that you can access alongside the | `timestamp_type` | Timestamp type (CREATE_TIME or LOG_APPEND_TIME) | Data lineage verification | | `headers` | Key-value pairs attached to the message | Cross-cutting concerns like correlation IDs | | `key` | Deserialized message key | Customer ID or entity identifier | -| `value` | Deserialized message content | The actual business data | -| `original_value` | Base64-encoded original message value | Debugging or custom deserialization | +| `value` | Deserialized message content, or `None` for a tombstone record | The actual business data | +| `original_value` | Base64-encoded original message value, or `None` for a tombstone record | Debugging or custom deserialization | | `original_key` | Base64-encoded original message key | Debugging or custom deserialization | | `value_schema_metadata` | Metadata about the value schema like `schemaId` and `dataFormat` | Data format and schemaId propagated when integrating with Schema Registry | | `key_schema_metadata` | Metadata about the key schema like `schemaId` and `dataFormat` | Data format and schemaId propagated when integrating with Schema Registry | diff --git a/tests/functional/kafka_consumer/required_dependencies/test_kafka_consumer.py b/tests/functional/kafka_consumer/required_dependencies/test_kafka_consumer.py index 74691b894fa..9b5e36ff4a3 100644 --- a/tests/functional/kafka_consumer/required_dependencies/test_kafka_consumer.py +++ b/tests/functional/kafka_consumer/required_dependencies/test_kafka_consumer.py @@ -422,3 +422,22 @@ def handler(event: ConsumerRecords, context): assert result.partition == kafka_event["records"]["my-topic-1"][0]["partition"] assert result.offset == kafka_event["records"]["my-topic-1"][0]["offset"] assert result.timestamp == kafka_event["records"]["my-topic-1"][0]["timestamp"] + + +@pytest.mark.parametrize("tombstone", [{}, {"value": None}], ids=["value_missing", "value_null"]) +def test_kafka_consumer_tombstone_record_has_no_value(kafka_event_with_json_data, lambda_context, tombstone): + # GIVEN a tombstone record, which Lambda delivers without a value or with a null one + event = deepcopy(kafka_event_with_json_data) + record = event["records"]["my-topic-1"][0] + del record["value"] + record.update(tombstone) + + @kafka_consumer(schema_config=SchemaConfig(value_schema_type="JSON", key_schema_type="JSON")) + def handler(event: ConsumerRecords, context): + return [(record.key, record.value, record.original_value) for record in event.records] + + # WHEN the handler reads the record + result = handler(event, lambda_context) + + # THEN the key is still deserialized and the value is None instead of raising + assert result == [({"user_id": "123"}, None, None)]