Repository navigation
fix(parser): accept plain-text string payloads - #8766
zhanfan-818 wants to merge 1 commit into
Conversation
CloudWatch Logs subscriptions forward Lambda runtime lines such as "START RequestId: ...", which are not JSON. _parse_and_validate_event called validate_json unconditionally for str payloads and raised ValidationError before the model could see the value. Try validate_json first, then fall back to validate_python, and re-raise the original error when the fallback fails so existing callers keep the same error semantics. Adds regression tests for a single plain-text message and for a mix of JSON and plain-text messages.
|
Thanks a lot for your first contribution! Please check out our contributing guidelines and don't hesitate to ask whatever you need. |
|
|
Closing as a duplicate: #8699 landed the same fallback ( The two regression tests here (single plain-text message, and a mix of JSON and plain-text |



Issue number: closes #8695
Summary
Changes
CloudWatchLogsEnvelopeparses everylogEvents[*].messagethrough_parse_and_validate_event, which forstrpayloads calledadapter.validate_json(data)unconditionally (aws_lambda_powertools/utilities/parser/functions.py:74). CloudWatch Logs subscriptions forward every event in the log group, including Lambda runtime lines such asSTART RequestId: ..., which are plain text rather than JSON.This change keeps
validate_jsonas the first attempt and falls back tovalidate_pythonwhen it raisesValidationError; if the fallback also fails, the originaljson_invaliderror is re-raised so callers that expectValidationErrorkeep the same error and message:Nothing changes for JSON payloads (
"message": json.dumps(...)), which still take the fast path.User experience
Before, a subscription that included any runtime log line failed for the whole batch:
After, with
model=str, plain-text messages are returned verbatim and JSON messages are still parsed into the model:Verification
Reproduced and verified locally on
develop(3b26155) with Python 3.13.9 and thepydantic==2.14.2pinned byuv.lock— the report mentions 3.14.10, which I did not have available.Repro script builds a real
awslogspayload (base64 + zlib) whose single message is aSTART ...line and callsparse(..., model=str, envelope=CloudWatchLogsEnvelope):Focused tests, including the two regression tests added in this PR:
The new file covers one plain-text message and a mix of JSON and plain-text messages; the existing six tests in
test_cloudwatch.pystill pass unchanged, including the ones that assertValidationErrorfor non-conforming payloads.I used an AI assistant to draft this patch; I reviewed it, ran the reproduction and the test command above myself, and the output quoted here is from those runs.
Two notes from reading the code:
envelopes/functions.py; theisinstance(data, str)branch is inutilities/parser/functions.py:71-74, reached fromenvelopes/base.py:44.test_cloudwatch_envelope_handles_non_json_messages,test_cloudwatch_envelope_handles_mixed_json_and_plain_text) are not in the repository, so they are added here.By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.