Skip to content

Bug: SQSMessageAttribute.binary_value raises KeyError for String attributes #8712

Open

Description

@anishmehta24

Expected Behaviour

SQSMessageAttribute.string_value and binary_value are typed str | None, so reading the value an attribute doesn't carry should return None.

Current Behaviour

Both properties index the dict directly (self["stringValue"], self["binaryValue"]). Lambda only sends the field that matches the attribute's type; the SQS example event in the Lambda docs has {"stringValue": "myValue", "stringListValues": [], "binaryListValues": [], "dataType": "String"} with no binaryValue. So binary_value on any String or Number attribute, and string_value on any Binary attribute, raise KeyError.

The test event tests/events/sqsEvent.json puts both stringValue and binaryValue on one attribute, which is why the existing tests don't hit this. The Pydantic SqsMsgAttributeModel already declares both fields optional.

Code snippet

from aws_lambda_powertools.utilities.data_classes import SQSEvent

event = {"Records": [{
    "messageId": "1", "receiptHandle": "r", "body": "b", "attributes": {}, "md5OfBody": "m",
    "eventSource": "aws:sqs", "eventSourceARN": "arn:aws:sqs:us-east-2:123456789012:q", "awsRegion": "us-east-2",
    "messageAttributes": {
        "text": {"stringValue": "myValue", "stringListValues": [], "binaryListValues": [], "dataType": "String"},
    },
}]}
record = next(SQSEvent(event).records)
record.message_attributes["text"].binary_value  # KeyError: 'binaryValue'

Steps to Reproduce

  1. Send an SQS message with a String message attribute to a Lambda function.
  2. Read binary_value on that attribute (or string_value on a Binary one).
  3. KeyError is raised instead of None.

Possible Solution

Use self.get(...) in both properties. I can open a PR.

Powertools for AWS Lambda (Python) version

latest (develop, 3.38.0)

AWS Lambda function runtime

3.12

Packaging format used

PyPi

Debugging logs

No response

Activity

  1. leandrodamascena commented on Oct 30, 2026

    @leandrodamascena
    Contributor

    Thanks @anishmehta24, confirmed. Lambda only sends the field matching the attribute type, our fixture has both on the same attribute, so the tests never caught it. I'll review #8713. Goes into 3.40.0.

  2. added this to the 3.40.0 milestone on Oct 30, 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