From b08f9465a82208938b30e5f31e023c0725ae7ac0 Mon Sep 17 00:00:00 2001 From: Azmi Muwahid Date: Thu, 29 Oct 2026 19:33:57 +0700 Subject: [PATCH 1/2] fix(parser): fall back to Python validation for non-JSON string payloads --- .../utilities/parser/functions.py | 12 +++- .../unit/parser/_pydantic/test_cloudwatch.py | 60 +++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) diff --git a/aws_lambda_powertools/utilities/parser/functions.py b/aws_lambda_powertools/utilities/parser/functions.py index d2acb9a7965..6389d1b4647 100644 --- a/aws_lambda_powertools/utilities/parser/functions.py +++ b/aws_lambda_powertools/utilities/parser/functions.py @@ -4,7 +4,7 @@ import logging from typing import TYPE_CHECKING, Any -from pydantic import IPvAnyNetwork, TypeAdapter +from pydantic import IPvAnyNetwork, TypeAdapter, ValidationError from aws_lambda_powertools.shared.cache_dict import LRUDict @@ -80,6 +80,16 @@ def _parse_and_validate_event(data: dict[str, Any] | Any, adapter: TypeAdapter[T "See issue: https://github.com/aws-powertools/powertools-lambda-python/issues/5303", ) data = json.loads(data) + except ValidationError as exc: + # CloudWatch Logs subscriptions forward plain-text Lambda runtime messages + # (START/END/REPORT) that are not valid JSON. Fall back to Python validation + # so string-compatible models still receive the raw payload; otherwise surface + # the original error. + # See: https://github.com/aws-powertools/powertools-lambda-python/issues/8695 + try: + return adapter.validate_python(data) + except ValidationError: + raise exc from None return adapter.validate_python(data) diff --git a/tests/unit/parser/_pydantic/test_cloudwatch.py b/tests/unit/parser/_pydantic/test_cloudwatch.py index b7cf1801865..0a1a253d23a 100644 --- a/tests/unit/parser/_pydantic/test_cloudwatch.py +++ b/tests/unit/parser/_pydantic/test_cloudwatch.py @@ -126,3 +126,63 @@ def test_handle_invalid_event_with_envelope(): empty_dict = {} with pytest.raises(ValidationError): CloudWatchLogsModel(**empty_dict) + + +def _make_cloudwatch_event(messages: list[str]) -> dict: + inner_event_dict = { + "messageType": "DATA_MESSAGE", + "owner": "123456789012", + "logGroup": "/aws/lambda/my-function", + "logStream": "2024/10/21/[$LATEST]abcdef1234567890", + "subscriptionFilters": ["my-filter"], + "logEvents": [ + {"id": f"event-{i}", "timestamp": 1729520895158 + i, "message": message} + for i, message in enumerate(messages) + ], + } + compressed = zlib.compress(json.dumps(inner_event_dict).encode("utf-8"), -1) + return {"awslogs": {"data": base64.b64encode(compressed)}} + + +def test_cloudwatch_envelope_handles_non_json_messages(): + plain_text_messages = [ + "START RequestId: eadc91c8-8273-4a2d-b170-b0a7883b619e Version: $LATEST\n", + "2024-10-21T14:28:15.158Z\teadc91c8-8273-4a2d-b170-b0a7883b619e\tINFO\tHello from handler\n", + "END RequestId: eadc91c8-8273-4a2d-b170-b0a7883b619e\n", + ( + "REPORT RequestId: eadc91c8-8273-4a2d-b170-b0a7883b619e\tDuration: 2.07 ms\t" + "Billed Duration: 3 ms\tMemory Size: 128 MB\tMax Memory Used: 67 MB\t\n" + ), + ] + + parsed = parse( + event=_make_cloudwatch_event(plain_text_messages), + model=str, + envelope=envelopes.CloudWatchLogsEnvelope, + ) + + assert parsed == plain_text_messages + + +def test_cloudwatch_envelope_handles_mixed_json_and_plain_text_messages(): + messages = [ + json.dumps({"my_message": "hello", "user": "test"}), + "START RequestId: eadc91c8-8273-4a2d-b170-b0a7883b619e Version: $LATEST\n", + ] + + parsed = parse( + event=_make_cloudwatch_event(messages), + model=str, + envelope=envelopes.CloudWatchLogsEnvelope, + ) + + assert parsed == messages + + +def test_cloudwatch_envelope_plain_text_still_fails_for_object_model(): + with pytest.raises(ValidationError): + parse( + event=_make_cloudwatch_event(["START RequestId: eadc91c8-8273-4a2d-b170-b0a7883b619e\n"]), + model=MyCloudWatchBusiness, + envelope=envelopes.CloudWatchLogsEnvelope, + ) From 5eb80f25867a2bd1ceb9cd0fdc2e8371be572935 Mon Sep 17 00:00:00 2001 From: Leandro Damascena Date: Wed, 4 Nov 2026 17:19:08 +0000 Subject: [PATCH 2/2] refactor(parser): move the string payload fallback into a helper to keep the module complexity rank --- .../utilities/parser/functions.py | 43 ++++++++++--------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/aws_lambda_powertools/utilities/parser/functions.py b/aws_lambda_powertools/utilities/parser/functions.py index 6389d1b4647..ff70a946214 100644 --- a/aws_lambda_powertools/utilities/parser/functions.py +++ b/aws_lambda_powertools/utilities/parser/functions.py @@ -49,6 +49,28 @@ def _retrieve_or_set_model_from_cache(model: type[T]) -> TypeAdapter[T]: return CACHE_TYPE_ADAPTER[id_model] +def _parse_string_payload(data: str, adapter: TypeAdapter[T]): + """Validate a string payload as JSON first, then as a plain Python value. + + Falls back to Python validation when Pydantic cannot validate the type from JSON + (see https://github.com/aws-powertools/powertools-lambda-python/issues/5303) and when + the string is not JSON at all, e.g. the plain-text Lambda runtime lines that CloudWatch Logs + subscriptions forward (see https://github.com/aws-powertools/powertools-lambda-python/issues/8695). + If the fallback fails too, the original JSON error is raised so callers see the same message. + """ + try: + return adapter.validate_json(data) + except NotImplementedError: + # See: https://github.com/pydantic/pydantic/issues/8890 + logger.debug("Falling back to Python validation due to Pydantic implementation.") + return adapter.validate_python(json.loads(data)) + except ValidationError as exc: + try: + return adapter.validate_python(data) + except ValidationError: + raise exc from None + + def _parse_and_validate_event(data: dict[str, Any] | Any, adapter: TypeAdapter[T]): """ Parse and validate the event data using the provided adapter. @@ -70,26 +92,7 @@ def _parse_and_validate_event(data: dict[str, Any] | Any, adapter: TypeAdapter[T if isinstance(data, str): logger.debug("Parsing event as string") - try: - return adapter.validate_json(data) - except NotImplementedError: - # See: https://github.com/aws-powertools/powertools-lambda-python/issues/5303 - # See: https://github.com/pydantic/pydantic/issues/8890 - logger.debug( - "Falling back to Python validation due to Pydantic implementation." - "See issue: https://github.com/aws-powertools/powertools-lambda-python/issues/5303", - ) - data = json.loads(data) - except ValidationError as exc: - # CloudWatch Logs subscriptions forward plain-text Lambda runtime messages - # (START/END/REPORT) that are not valid JSON. Fall back to Python validation - # so string-compatible models still receive the raw payload; otherwise surface - # the original error. - # See: https://github.com/aws-powertools/powertools-lambda-python/issues/8695 - try: - return adapter.validate_python(data) - except ValidationError: - raise exc from None + return _parse_string_payload(data, adapter) return adapter.validate_python(data)