From 78a856337527adf3e59269edeb434ef1137aec4a Mon Sep 17 00:00:00 2001 From: Sumedh Aerram Date: Sat, 28 Nov 2026 15:19:43 -0800 Subject: [PATCH] fix(streaming): keep line breaks inside quoted fields in CsvTransform --- .../references/streaming.md | 3 +++ .../utilities/streaming/transformations/csv.py | 3 ++- docs/utilities/streaming.md | 3 +++ tests/functional/streaming/_boto3/test_s3_object.py | 12 ++++++++++++ 4 files changed, 20 insertions(+), 1 deletion(-) diff --git a/.agents/skills/building-lambda-functions-with-powertools-python/references/streaming.md b/.agents/skills/building-lambda-functions-with-powertools-python/references/streaming.md index ae5a0dab15f..c8754497f5a 100644 --- a/.agents/skills/building-lambda-functions-with-powertools-python/references/streaming.md +++ b/.agents/skills/building-lambda-functions-with-powertools-python/references/streaming.md @@ -245,6 +245,9 @@ We will propagate additional options to the underlying implementation for each t | **ZipTransform** | [ZipFile constructor](https://docs.python.org/3/library/zipfile.html#zipfile.ZipFile) | | **CsvTransform** | [DictReader constructor](https://docs.python.org/3/library/csv.html#csv.DictReader) | +`CsvTransform` also accepts `encoding` (default `utf-8`) and `newline` (default `""`), which it passes to the [TextIOWrapper](https://docs.python.org/3/library/io.html#io.TextIOWrapper) that decodes the object. +With `newline=""`, line breaks inside quoted fields are kept as they are, as the [csv module](https://docs.python.org/3/library/csv.html#id4) expects. Pass `newline=None` if you want them translated to `\n`. + For instance, take `ZipTransform`. You can use the `compression` parameter if you want to unzip an S3 object compressed with `LZMA`. **Unzipping LZMA data** diff --git a/aws_lambda_powertools/utilities/streaming/transformations/csv.py b/aws_lambda_powertools/utilities/streaming/transformations/csv.py index ce9c1129eb8..5718ab1bcdc 100644 --- a/aws_lambda_powertools/utilities/streaming/transformations/csv.py +++ b/aws_lambda_powertools/utilities/streaming/transformations/csv.py @@ -52,7 +52,8 @@ def transform(self, input_stream: IO[bytes]) -> DictReader: # S3Object re-creates its transformed stream), and each call must see the original options. options = dict(self.transform_options) encoding = options.pop("encoding", "utf-8") - newline = options.pop("newline", None) + # csv expects newline="" so that line breaks inside quoted fields are kept as they are + newline = options.pop("newline", "") # csv module needs an Iterator[str], so we wrap the underlying stream into a TextIO iterator = io.TextIOWrapper(input_stream, encoding=encoding, newline=newline) diff --git a/docs/utilities/streaming.md b/docs/utilities/streaming.md index 5dd6adeed47..8e1900c05e8 100644 --- a/docs/utilities/streaming.md +++ b/docs/utilities/streaming.md @@ -137,6 +137,9 @@ We will propagate additional options to the underlying implementation for each t | **ZipTransform** | [ZipFile constructor](https://docs.python.org/3/library/zipfile.html#zipfile.ZipFile){target="_blank" rel="nofollow"} | | **CsvTransform** | [DictReader constructor](https://docs.python.org/3/library/csv.html#csv.DictReader){target="_blank" rel="nofollow"} | +`CsvTransform` also accepts `encoding` (default `utf-8`) and `newline` (default `""`), which it passes to the [TextIOWrapper](https://docs.python.org/3/library/io.html#io.TextIOWrapper){target="_blank" rel="nofollow"} that decodes the object. +With `newline=""`, line breaks inside quoted fields are kept as they are, as the [csv module](https://docs.python.org/3/library/csv.html#id4){target="_blank" rel="nofollow"} expects. Pass `newline=None` if you want them translated to `\n`. + For instance, take `ZipTransform`. You can use the `compression` parameter if you want to unzip an S3 object compressed with `LZMA`. === "Unzipping LZMA data" diff --git a/tests/functional/streaming/_boto3/test_s3_object.py b/tests/functional/streaming/_boto3/test_s3_object.py index 7358e54a6aa..80d88ca6ad8 100644 --- a/tests/functional/streaming/_boto3/test_s3_object.py +++ b/tests/functional/streaming/_boto3/test_s3_object.py @@ -136,3 +136,15 @@ def test_csv_transform_keeps_options_when_reused(s3_client, s3_client_stub): assert rows == [{"name": "café", "city": "Zürich"}] assert transform.transform_options == {"encoding": "latin-1", "delimiter": ";"} + + +def test_csv_transform_keeps_line_breaks_inside_quoted_fields(s3_client, s3_client_stub): + # GIVEN a CSV object whose quoted fields contain CRLF and a lone CR + payload = b'id,note\r\n1,"line one\r\nline two"\r\n2,"carriage\rreturn"\r\n' + _stub_get_object(s3_client_stub, payload) + + # WHEN it is read with the default CsvTransform + rows = list(S3Object(bucket="bucket", key="key", boto3_client=s3_client).transform(CsvTransform())) + + # THEN the quoted fields keep their original line breaks, as csv.DictReader does on a file opened with newline="" + assert rows == [{"id": "1", "note": "line one\r\nline two"}, {"id": "2", "note": "carriage\rreturn"}]