Skip to content

fix(kafka): deserialize empty string record key instead of returning None - #8698

Merged
leandrodamascena merged 2 commits into
aws-powertools:developfrom
ASHMEET555:fix/kafka-consumer-empty-key
Nov 4, 2026
Merged

leandrodamascena merged 2 commits into
aws-powertools:developfrom
ASHMEET555:fix/kafka-consumer-empty-key

Conversation

@ASHMEET555

Copy link
Copy Markdown
Contributor

Issue number: closes #8696

Summary

Changes

  • In ConsumerRecordRecords.key, replace truthiness check if not key: with identity check if key is None:. This ensures Kafka events with empty string keys ("key": "") are deserialized properly rather than treated as absent and returning None.
  • Align behavior with KafkaEventRecord.decoded_key in aws_lambda_powertools.utilities.data_classes.kafka_event.
  • Add functional regression test test_kafka_consumer_default_deserializer_key_is_empty_string.

User experience

  • Before: When a Kafka consumer record contained an empty string key ("key": ""), accessing record.key returned None, making it indistinguishable from a null/missing key.
  • After: Missing or None keys return None. An empty string key is preserved and deserialized (returning "" by default, or passed to the configured deserializer).

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@ASHMEET555
ASHMEET555 requested a review from a team as a code owner October 28, 2026 22:04
@ASHMEET555
ASHMEET555 requested a review from hjgraca October 28, 2026 22:04
@boring-cyborg

boring-cyborg Bot commented Oct 28, 2026

Copy link
Copy Markdown

Thanks a lot for your first contribution! Please check out our contributing guidelines and don't hesitate to ask whatever you need.
In the meantime, check out the #python channel on our Powertools for AWS Lambda Discord: Invite link

@SumiRann1 SumiRann1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Viewed, I think this is correct!

@leandrodamascena leandrodamascena left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @ASHMEET555, confirmed. One thing I checked before approving: with a key schema configured, an empty key now raises KafkaConsumerDeserializationError instead of silently becoming None. That is exactly what value already does for an empty value with a schema, so the two are consistent now, and I'll call it out in the release notes.

I'm approving now. This goes out in 3.40.0, so it will sit here for a while before I merge it. No rebase needed in the meantime, and you'll be credited in the release notes.

@ASHMEET555

Copy link
Copy Markdown
Contributor Author

Thanks @leandrodamascena appreciate the careful check on the empty-key case. Agreed that it now matches how an empty value is handled with a schema, and I’m happy for it to be called out in the 3.40.0 release notes.

@sonarqubecloud

sonarqubecloud Bot commented Nov 4, 2026

Copy link
Copy Markdown

@codecov

codecov Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.01%. Comparing base (db2f8f0) to head (d3ce090).
⚠️ Report is 1 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #8698   +/-   ##
========================================
  Coverage    97.01%   97.01%           
========================================
  Files          321      321           
  Lines        16424    16424           
  Branches      1484     1484           
========================================
  Hits         15933    15933           
  Misses         350      350           
  Partials       141      141           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@leandrodamascena
leandrodamascena merged commit 8b5ec90 into aws-powertools:develop Nov 4, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/S Denotes a PR that changes 10-29 lines, ignoring generated files. tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

3 participants