Repository navigation
Description
Expected Behaviour
an S3Object created with is_csv=True, or transformed in place with CsvTransform, can be closed like any other S3Object: close() and leaving a with block return normally, and closed returns False before closing and True after.
Current Behaviour
once at least one row has been read, close() raises AttributeError: 'DictReader' object has no attribute 'close'. leaving a with S3Object(..., is_csv=True) block raises the same error, because __exit__ calls close(). reading closed raises AttributeError: 'DictReader' object has no attribute 'closed'.
the error comes after the rows were already processed, so a handler that reads a csv file inside a with block does all its work and then fails the invocation.
the cause is in aws_lambda_powertools/utilities/streaming/s3_object.py. close() (line 232) calls self._transformed_stream.close() and closed (line 211) returns self._transformed_stream.closed. both assume the transformed stream is a file object, but CsvTransform.transform() returns a csv.DictReader, which has neither close() nor closed. it happens with is_csv=True, with is_gzip=True, is_csv=True, and with transform(CsvTransform(), in_place=True).
if the object is closed before any row is read, nothing fails, because the transformed stream has not been created yet.
Code snippet
import io
import boto3
from botocore import stub
from botocore.response import StreamingBody
from aws_lambda_powertools.utilities.streaming import S3Object
payload = b"id,name\n1,alice\n2,bob\n"
client = boto3.client("s3", region_name="us-east-1", aws_access_key_id="x", aws_secret_access_key="x")
s3_stub = stub.Stubber(client)
s3_stub.add_response("get_object", {"Body": StreamingBody(raw_stream=io.BytesIO(payload), content_length=len(payload))})
s3_stub.activate()
with S3Object(bucket="bucket", key="key", is_csv=True, boto3_client=client) as rows:
for row in rows:
print(row)Possible Solution
in S3Object.close(), only call close() on the transformed stream when it has one. the raw stream that the DictReader reads from is already closed a few lines above, so the s3 connection is still released. in S3Object.closed, fall back to self.raw_stream.closed when the transformed stream has no closed attribute.
i have this fix ready locally (11 lines in s3_object.py) with functional tests for is_csv=True, is_gzip=True, is_csv=True and transform(CsvTransform(), in_place=True). the tests fail on develop and pass with the fix, and the existing streaming tests still pass. happy to send the pr with tests if you agree with the approach.
disclosure: i found this while reviewing the streaming utility with the help of an ai assistant (claude code). the snippet above was run against develop.
Steps to Reproduce
- install
aws-lambda-powertoolsandboto3. - run the snippet above. it stubs
GetObject, so it needs no aws account. - both rows are printed, then the
withblock raisesAttributeErroron exit.
to see the closed error, read one row with next(s3_object) and then read s3_object.closed.
Did you run the reproduction yourself?
Yes, I ran it and saw the failure
Powertools for AWS Lambda (Python) version
3.41.0 (develop at be21802)
AWS Lambda function runtime
3.12
Packaging format used
PyPi
Debugging logs
{'id': '1', 'name': 'alice'}
{'id': '2', 'name': 'bob'}
Traceback (most recent call last):
File "repro_issue.py", line 16, in <module>
with S3Object(bucket="bucket", key="key", is_csv=True, boto3_client=client) as rows:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "aws_lambda_powertools/utilities/streaming/s3_object.py", line 217, in __exit__
self.close()
File "aws_lambda_powertools/utilities/streaming/s3_object.py", line 232, in close
self._transformed_stream.close()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'DictReader' object has no attribute 'close'Metadata
Metadata
Assignees
Type
Projects
- StatusShow more project fieldsTriage
Activity
boring-cyborg commentedon Nov 23, 2026
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
powertools-for-aws-oss-automation commentedon Nov 23, 2026
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
k0nnect commentedon Nov 24, 2026
i see #8898 was opened for this. for the maintainers: i still have the fix and functional tests ready locally, as offered above, and can open a pr once this is triaged if that's more useful. fine either way as long as it gets fixed.